chris48s / v8r

✔️ A command-line JSON and YAML validator that's on your wavelength
https://www.npmjs.com/package/v8r
MIT License
28 stars 6 forks source link

Azure pipelines invalid escape #328

Open bdovaz opened 1 year ago

bdovaz commented 1 year ago

@chris48s I don't understand what is going on, I don't know if the error is in my file or in the schema itself (the url you mention).

My azure-pipelines.yml file should not be because it validates and runs correctly in Azure DevOps.

ℹ Processing .\azure-pipelines.yml
ℹ Found schema in .v8rrc.yml ...
ℹ Validating .\azure-pipelines.yml against schema from https://raw.githubusercontent.com/microsoft/azure-pipelines-vscode/master/service-schema.json ...
✖ Invalid regular expression: /^[^\/~\^\: \[\]\\]+(\/[^\/~\^\: \[\]\\]+)*$/: Invalid escape
chris48s commented 1 year ago

It is an error thrown by ajv trying to load the schema.

This can be reproduced independently of v8r, just using ajv-cli:

$ curl "https://raw.githubusercontent.com/microsoft/azure-pipelines-vscode/master/service-schema.json" --output service-schema.json

...

$ npx ajv-cli@latest compile -s service-schema.json
schema service-schema.json is invalid

It is hitting the error on this line of the schema https://github.com/microsoft/azure-pipelines-vscode/blob/97f39ff988232860b35556dde725d448151ec5a0/service-schema.json#L1076 I'm not sure if its an issue with the schema itself or AJV.

bdovaz commented 1 year ago

Created an issue: https://github.com/ajv-validator/ajv/issues/2296

chris48s commented 10 months ago

I was working on something else recently that made me realise what is going on here.

AJV constructs regular expressions with the unicode u flag, as required by the spec: https://json-schema.org/understanding-json-schema/reference/string.html#regular-expressions JSON Schema regular expressions are unicode-aware.

In javascript, Unnecessary escape sequences are invalid with the u flag.

So this

var regex = new RegExp("^[^\\/~\\^\\: \\[\\]\\\\]+(\\/[^\\/~\\^\\: \\[\\]\\\\]+)*$", "u")

throws a

Uncaught:
SyntaxError: Invalid regular expression: /^[^\/~\^\: \[\]\\]+(\/[^\/~\^\: \[\]\\]+)*$/: Invalid escape

I guess the reason this schema works with some validators is that maybe there are some JSON schema tools that either don't construct with the unicode flag, or this constraint (no unnecessary escape sequences when using unicode) is not there in other regexp engines. I've not really dug into it. Note that

var regex = new RegExp("^[^\\/~\\^\\: \\[\\]\\\\]+(\\/[^\\/~\\^\\: \\[\\]\\\\]+)*$")

constructs a regex without error.

I think the solution to this - if you wanted to submit a PR upstream to https://github.com/microsoft/azure-pipelines-vscode/ - would be to remove the unnecessary escapes on the : characters.

var regex = new RegExp("^[^\\/~\\^: \\[\\]\\\\]+(\\/[^\\/~\\^: \\[\\]\\\\]+)*$", "u")

is valid with the unicode flag.