-
-
Notifications
You must be signed in to change notification settings - Fork 591
Pattern match does not seem to work with newlines, possible unescaping issue #1023
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
I was able to use workaround of |
In fact the workaround is not really applicable because AJV, another major JSON Schema validator, not only that it does not support it, but it also closed the bug report as wontfix at ajv-validator/ajv#101 So as a JSON schema author, I am stuck between two broken libraries, cannot write pattern validator that would work with both. 🤷🏽♂️ |
After reading ECMA-262 and also testing with https://regex101.com/ I conclude that in JavaScript world it use of This means that the only possible way to make a match using the current JSON Schema specification is to correctly implement |
The library doesn't do any loading, so whatever string you loaded is the one being used. But you're confusing str with repr, which is what's shown in error messages:
|
@Julian I wonder do I need to write inside the JSON schema file in order to be able to match a multiline string that can start with something and end with something else? I currently have As use of flags/modifiers is not possible because the spec failed to specify them and made use of ECMA-262, which does not include support for embedded modifiers, we are forced not to use them. As others noted a common workaround to make That means that a solution without modifiers/flags should be Now the challenge is how to correctly encode the above regex for JSON. Online encoders report
Update few hours later...After digging a little bit inside content from schemastore, I was able to find one example of regex that was supposed to match a multiline string, one that finally worked: To be honest I do not know why this syntax was used because |
This doesn't sound much like a JSON Schema question. The only JSON Schema relevant piece is that the specification doesn't specify what Regex flavor implementations MUST support (it only recommends ECMA 262, and specifically says you the schema author should stick to syntax common across engines). Indeed this library uses Python regexes, since that's the only flavor really accessible to Python.
As usual, the way you file issues leaves a lot to be desired. There's nothing broken about the library for this particular case.
This of course has nothing to do with JSON Schema nor this library, but here's how you answer your own question. First check that I assume that you're checking against: >>> re.search(r"^{{[\s\S]*}}$", "{{foo}}")
<re.Match object; span=(0, 7), match='{{foo}}'>
>>> re.search(r"^{{[\s\S]*}}$", "{{foo\nbar\nbaz}}")
<re.Match object; span=(0, 15), match='{{foo\nbar\nbaz}}'> and that indeed that means you're getting the behavior you want. The way you answer how to check what a string is in JSON is by dumping it. Specifically: >>> import json
>>> json.dumps(r"^{{[\s\S]*}}$")
'"^{{[\\\\s\\\\S]*}}$"' That string is the representation of that pattern in JSON. The escaping will of course need adjusting if you say, paste it as is (since you're looking at a repr again, not a str), so the real easiest way if you're unsure is to |
Apparently
^{{[.\s]*}}$
regex pattern is not correctly applied by the library. Inside JSON this pattern should be represented like"pattern": "^{{[.\\s]*}}$"
due to JSON escaping.Still, when using it I still get validation error that prints the pattern using the unescaped value, which makes me believe that the string was not loaded correctly.
Python does not add extra escapes for
\s
, so it should be a loading issue?The text was updated successfully, but these errors were encountered: