Invalid regex (iso8601Pattern) when building with ajv ^8.8.2
jakejscott opened this issue · comments
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
Thanks for reporting @jakejscott. I tried to reproduce with the test project:
https://github.com/fourTheorem/slic-watch/tree/fix-esbuild-regex/serverless-test-project
and haven't seen this yet. Can you check this test project and compare to your setup?
I'm also using the plugin in other projects with serverless-esbuild
and haven't seen it yet.
Yeah I'll have a go with adding esbuild in my fork of the test project add see if that's what is causing it. It might end up being something else
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.
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)
```