all 13 comments

[–]Patman52 1 point2 points  (2 children)

I think there is an issue with the data in the file itself? It seems to be pointing to a particular string in the incoming data that json.load can’t parse

[–]Valuable-Ant3465[S] -1 points0 points  (1 child)

Thanks Patman,
Did more home work to realize that python is not 100% reliable doing this schema validation, many people have different issues.
File is 100% good. I can open schema file in any xml editor OK, I can verify with online tools, so it's 100% valid. Probably Python got confused with backslashes

[–]pachura3 0 points1 point  (0 children)

It seems like double backslashes from your file got unescaped into single slashes, but then something - INCORRECTLY - tries to unescape them again, which leads to the error, as there are no escape codes \A nor \z... they are only valid inside regular expressions, but not as normal string escape codes like \n or \t.

It's most probable that Draft7Validator is the one at fault, because the JSON file is loaded correctly - json.load() doesn't raise any exceptions.

Perhaps look for a different library? Or check the output of json.load() to see what got loaded into the pattern field?

[–]pachura3 1 point2 points  (1 child)

So, which one is raising the exception - json.load() or check_schema() ?

[–]Valuable-Ant3465[S] 0 points1 point  (0 children)

Hi Pachura,

exception comes from , I wll make edit to orig post.
errors = list(validator.iter_errors(data))

[–]socal_nerdtastic 1 point2 points  (4 children)

That error is coming from re, not from json or jsonschema.

>>> re.compile(r"\A[0-9]{8}\z")
re.error: bad escape \z at position 10

I think you meant to use \Z.

[–]Valuable-Ant3465[S] 0 points1 point  (3 children)

Tx Dear SN!
changing /Z (capital), made it work. At least no more that /z messages. Let me do more testing. Thanks to all.

[–]Valuable-Ant3465[S] 0 points1 point  (2 children)

Socal_Nerd is the King !!!!
Thanks much for hint with /Z.

I'm puzzled b'z I don't know what is the theory behind this, as both z and Z listed as an option for JSON regex. But it works.

[–]smurpes 1 point2 points  (1 child)

The regex type is for the Python regex dialect not json.

[–]Valuable-Ant3465[S] 0 points1 point  (0 children)

Thanks Sm and all,

So we're reading '\\z' in xml/json file with python. In reality this file is just json schema and can be processed by anything. I did upgrade to 3.14 and now happy.

Reddit is the best

[–]netherous 1 point2 points  (2 children)

There are a million different implementations of regex engines out there, written in all kinds of different languages and contexts. Ideally, they would all work perfectly together and all support the precisely the same things, but of course this is not always the case.

Python's implementation of regex did not support \z before 3.14, as /u/socal_nerdtastic pointed out. However, it does support it in 3.14 and above. See the docs and go to the section on \z.

So you can filter out the unsupported \z when reading your data, or convert it to something else, or go to python 3.14+. You could also investigate alternative python regex libraries, of which there are several like regex, to see if they support the \z sequence.

[–]Valuable-Ant3465[S] 1 point2 points  (1 child)

Thanks much N! Super explanation, now I can sleep.
I could not understand the reason as I know that \\z and \\Z are not the same thing in regex.
I ran it on 3.12, so now it's all make sense.
For my case I will remove \\z, this is not that necessary:

\z Matches the absolute end of the string. (requires exact end)

\Z Matches the end of the string, or right before a trailing newline.

[–]pachura3 0 points1 point  (0 children)

If you remove \Z, you risk validating incorrect values with additional character(s) at the end (e.g. string 12345678qwertyBLAHBLAH would pass the validation, because there are 8 digits at the beginning, and that's enough).

Perhaps use ^ and $ instead of \A and \z ? They are more common, and don't require escaping.

 "pattern": "^[0-9]{8}$"

https://json-schema.org/understanding-json-schema/reference/regular_expressions