MDEV-30124: Add format validation for JSON Schema - #4750
Conversation
293b984 to
e5142f5
Compare
5a6ab32 to
602a323
Compare
gkodinov
left a comment
There was a problem hiding this comment.
Thank you for your contribution! This is a preliminary review.
In general I would strive to reuse as much of the existing infrastructure in the server as possible: charset handling, parsers for various data types, 3d party libraries etc.
The declarations of the formats are far from simple: some have quoting, some escaping etc.
I've jotted down some of the issues that I find at a first glance. It looks like this can benefit from some sort of standardized testing of the parser too. I'm sure test sets exist for most of these parsers.
602a323 to
1524d38
Compare
1524d38 to
b88e9c4
Compare
|
this is GSoC project, as far as I remember, let's change it to draft until at least the coding period starts |
|
@vuvova |
b88e9c4 to
ceef2ab
Compare
|
@varundeepsaini no problem, you can still work on this draft if you'd like. I just don't think we can promise anything until we know the status of your GSoC proposal, which depends on Google's decision. |
ceef2ab to
c2c0068
Compare
gkodinov
left a comment
There was a problem hiding this comment.
One implementation question below.
FWIW, for more complex tasks as the one below (involving data structures etc), I'd always start with a quick design sketch: what new data structures are added, what is being done at each pass: validation, parsing etc.
Helps a lot understand the big picture and be more effective in self-review.
|
A quick overview New session var json_schema_format_validation (default OFF). ON = validate Data structures:
Two-phase flow (same as every other keyword):
Validators: ipv4/ipv6 via inet_pton, regex via pcre2, rest (date/email/uri/hostname/uuid/json-pointer etc) hand-rolled RFC grammar checks. 15 hand-written parsers. |
4d6e1a0 to
7e68eaa
Compare
gkodinov
left a comment
There was a problem hiding this comment.
Looking much better with the new hash! Not sure why you couldn't use subclasses and stay within the original keyword hash. But OK: no harm no faul. Some further comments below.
| static HASH all_keywords_hash; | ||
| static HASH json_schema_format_hash; | ||
|
|
||
| /* Needed by json_schema_format_array below. */ |
There was a problem hiding this comment.
all of these can be private instance methods.
| static bool validate_format_regex(Json_schema_format *self, | ||
| const char *val, int len); | ||
|
|
||
| struct st_json_schema_format_map |
There was a problem hiding this comment.
these two can be local variables into setup_json_schema_format_hash
There was a problem hiding this comment.
Moved the map into setup_format_hash(), but kept it a static local
the hash stores pointers into the array rather than copies, so a plain local would leave dangling pointers after setup returns
7e68eaa to
5163d5e
Compare
|
@gkodinov tried removing all overkill parts of the patch
The keyword hash maps keyword names, and the 18 format names live in the keyword's value |
JSON_SCHEMA_VALID() now validates the format keyword per JSON Schema Draft 2020-12 when the new session variable json_schema_format_validation is ON. By default it stays an annotation, matching the previous behaviour. The 18 Draft 2020-12 formats are supported: date-time, date, time, duration, email, idn-email, hostname, idn-hostname, ipv4, ipv6, uri, uri-reference, iri, iri-reference, uuid, json-pointer, relative-json-pointer, regex. Each format has a small syntactic validator, a private method of Json_schema_format. IP addresses are parsed with inet_pton, regex with pcre2_compile, and the rest with hand-written checks that follow the relevant RFC grammar. The format name is resolved through Json_schema_format::format_hash, built once at server startup (mirrors the existing all_keywords_hash used for the other schema keywords). Each hash entry carries a member pointer to the format's validator, set on Json_schema_format at schema parse time, so validate() dispatches with a single indirect call per document. An unknown format name raises ER_JSON_INVALID_VALUE_FOR_KEYWORD at schema parse time when json_schema_format_validation is ON (2020-12 7.2.2: assertion mode must refuse unknown formats). When OFF it stays a legal annotation (7.2.1), preserving the previous behaviour.
5163d5e to
cd929bf
Compare
Summary
Implements optional format validation for the
JSON_SCHEMA_VALID()function per JSON Schema Draft 2020-12.json_schema_format_validation(defaultOFF)OFF, theformatkeyword is treated as annotation only (existing behavior)ON, validates strings against 18 format types:date-time,date,time,duration,email,idn-email,hostname,idn-hostname,ipv4,ipv6,uri,uri-reference,iri,iri-reference,uuid,json-pointer,relative-json-pointer,regexTest plan
json_schema_format_validation=ON