question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Invalid regex (iso8601Pattern) when building with ajv ^8.8.2

See original GitHub issue

Describe the bug When I run sls deploy I a regex error:

SyntaxError: Invalid regular expression: /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/: Invalid escape
      at new RegExp (<anonymous>)
      at defaultRegExp (C:\dev\test-slic-watch\nexus\node_modules\ajv\lib\core.ts:66:53)
      

I think it’s this regex: https://github.com/fourTheorem/slic-watch/blob/main/serverless-plugin/config-schema.js#L6

To Reproduce sls deploy

Expected behavior

Environment:

  • OS: Windows
  • Node.js Version: 14.18.0
  • NPM version: 6.14.15
  • Serverless Framework Version: Framework Core: 2.69.1 (local) Plugin: 5.5.1 SDK: 4.3.0 Components: 3.18.1

Additional context We are using serverless-esbuild plugin which uses esbuild to build the code. https://github.com/floydspace/serverless-esbuild

Issue Analytics

  • State:closed
  • Created 2 years ago
  • Reactions:3
  • Comments:5 (5 by maintainers)

github_iconTop GitHub Comments

1reaction
jakejscottcommented, Jan 2, 2022

Narrowed it down to this unicode flag that Ajv adds when creating the regex:

const iso8601Pattern = '^([\\+-]?\\d{4}(?!\\d{2}\\b))((-?)((0[1-9]|1[0-2])(\\3([12]\\d|0[1-9]|3[01]))?|W([0-4]\\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\\d|[12]\\d{2}|3([0-5]\\d|6[1-6])))([T\\s]((([01]\\d|2[0-3])((:?)[0-5]\\d)?|24\\:?00)([\\.,]\\d+(?!:))?)?(\\17[0-5]\\d([\\.,]\\d+)?)?([zZ]|([\\+-])([01]\\d|2[0-3]):?([0-5]\\d)?)?)?)?$';

const date = new Date().toISOString()
console.log('isValid', new RegExp(iso8601Pattern, 'u').test(date))

Error:

console.log('isValid', new RegExp(iso8601Pattern, 'u').test(date))
                       ^

SyntaxError: Invalid regular expression: /^([\+-]?\d{4}(?!\d{2}\b))((-?)((0[1-9]|1[0-2])(\3([12]\d|0[1-9]|3[01]))?|W([0-4]\d|5[0-2])(-?[1-7])?|(00[1-9]|0[1-9]\d|[12]\d{2}|3([0-5]\d|6[1-6])))([T\s]((([01]\d|2[0-3])((:?)[0-5]\d)?|24\:?00)([\.,]\d+(?!:))?)?(\17[0-5]\d([\.,]\d+)?)?([zZ]|([\+-])([01]\d|2[0-3]):?([0-5]\d)?)?)?)?$/: Invalid escape
    at new RegExp (<anonymous>)
    at Object.<anonymous> (/mnt/c/dev/jakejscott/slic-watch/serverless-test-project/test.js:11:24)
    ```


1reaction
jakejscottcommented, Jan 2, 2022

Hey @eoinsha I’ve managed to reproduce the issue 😃 Our project is using "ajv": "^8.8.2". If you upgrade the test project to that version and run npm run test (which calls sls package) it will throw that regex error.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Regex pattern not validated correctly · Issue #1304 - GitHub
Problem: A pattern [0-9]{5} with the input 123456 (one char too long) does not lead to a validation error. What version of Ajv...
Read more >
How do I validate a string using a regular expression with Ajv?
The documentation about format state that : regex: tests whether a string is a valid regular expression by passing it to RegExp constructor....
Read more >
Ajv JSON schema validator - npm
Ajv JSON schema validator. The fastest JSON validator for Node.js and browser. Supports JSON Schema draft-04/06/07/2019-09/2020-12 (draft-04 ...
Read more >
API Reference | Ajv JSON schema validator
The schema passed to this method will be validated against meta-schema unless validateSchema option is false. If schema is invalid, an error will...
Read more >
ajv-formats - Ajv JSON schema validator
ipv6: IP address v6. regex: tests whether a string is a valid regular expression by passing it to RegExp constructor. uuid: Universally Unique...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found