Skip to content

MDEV-30124: Add format validation for JSON Schema - #4750

Open
varundeepsaini wants to merge 1 commit into
MariaDB:mainfrom
varundeepsaini:MDEV-30124-json-schema-format-validation
Open

MDEV-30124: Add format validation for JSON Schema#4750
varundeepsaini wants to merge 1 commit into
MariaDB:mainfrom
varundeepsaini:MDEV-30124-json-schema-format-validation

Conversation

@varundeepsaini

Copy link
Copy Markdown
Contributor

Summary

Implements optional format validation for the JSON_SCHEMA_VALID() function per JSON Schema Draft 2020-12.

  • Adds a new session variable json_schema_format_validation (default OFF)
  • When OFF, the format keyword is treated as annotation only (existing behavior)
  • When 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, regex
  • Unknown format values always pass validation

Test plan

  • Existing format annotation tests continue to pass unchanged
  • New tests cover all 18 formats with valid/invalid inputs when json_schema_format_validation=ON
  • Verified non-string types always pass regardless of format
  • Verified unknown format values are treated as annotation

@varundeepsaini
varundeepsaini marked this pull request as draft March 7, 2026 05:22
@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch 3 times, most recently from 293b984 to e5142f5 Compare March 7, 2026 08:22
@varundeepsaini
varundeepsaini marked this pull request as ready for review March 7, 2026 09:34
@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch 2 times, most recently from 5a6ab32 to 602a323 Compare March 7, 2026 16:45
@gkodinov gkodinov added the External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements. label Mar 9, 2026

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sql/json_schema.cc Outdated
Comment thread sql/json_schema.cc Outdated
Comment thread sql/json_schema.cc Outdated
Comment thread sql/json_schema.cc
Comment thread sql/json_schema.cc
@CLAassistant

CLAassistant commented Mar 30, 2026

Copy link
Copy Markdown

CLA assistant check
All committers have signed the CLA.

@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch from 602a323 to 1524d38 Compare April 1, 2026 11:50
@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch from 1524d38 to b88e9c4 Compare April 21, 2026 09:53
@vuvova
vuvova marked this pull request as draft April 21, 2026 09:54
@vuvova

vuvova commented Apr 21, 2026

Copy link
Copy Markdown
Member

this is GSoC project, as far as I remember, let's change it to draft until at least the coding period starts

@varundeepsaini

Copy link
Copy Markdown
Contributor Author

@vuvova
yeah my bad, this PR was raised before I drafted the proposal and got to know about MDEV-30219 (the broader ticket)

@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch from b88e9c4 to ceef2ab Compare April 21, 2026 10:29
@vuvova

vuvova commented Apr 21, 2026

Copy link
Copy Markdown
Member

@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.

@varundeepsaini
varundeepsaini requested a review from gkodinov July 19, 2026 17:59
@varundeepsaini
varundeepsaini marked this pull request as ready for review July 19, 2026 17:59
@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch from ceef2ab to c2c0068 Compare July 19, 2026 18:10

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sql/json_schema.cc Outdated
@varundeepsaini

varundeepsaini commented Jul 23, 2026

Copy link
Copy Markdown
Contributor Author

A quick overview

New session var json_schema_format_validation (default OFF). ON = validate format keyword (date, email, uuid, ipv4/6, regex, etc, 17 types) per Draft 2020-12. OFF = format stays annotation-only, old behavior.

Data structures:

  • json_schema_format_type enum, Json_schema_format class (subclass of Json_schema_keyword)
  • json_schema_format_hash: name->validator lookup, built once at startup (mirrors existing all_keywords_hash)
  • validator resolved once at parse time, validate() calls it direct (no switch, per reviewer ask)
  • regex format caches last value+result, skips pcre2_compile on repeat

Two-phase flow (same as every other keyword):

  • handle_keyword() = parse time, once per query
  • validate() = per document/row

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.

@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch 2 times, most recently from 4d6e1a0 to 7e68eaa Compare July 23, 2026 05:58
@varundeepsaini
varundeepsaini requested a review from gkodinov July 23, 2026 05:59

@gkodinov gkodinov left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread sql/json_schema.h Outdated
Comment thread sql/json_schema.h Outdated
Comment thread sql/json_schema.cc Outdated
static HASH all_keywords_hash;
static HASH json_schema_format_hash;

/* Needed by json_schema_format_array below. */

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

all of these can be private instance methods.

Comment thread sql/json_schema.cc
Comment thread sql/json_schema.cc
static bool validate_format_regex(Json_schema_format *self,
const char *val, int len);

struct st_json_schema_format_map

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these two can be local variables into setup_json_schema_format_hash

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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

Comment thread sql/json_schema.cc Outdated
Comment thread sql/json_schema.cc
@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch from 7e68eaa to 5163d5e Compare July 29, 2026 14:43
@varundeepsaini

varundeepsaini commented Jul 29, 2026

Copy link
Copy Markdown
Contributor Author

@gkodinov tried removing all overkill parts of the patch

why you couldn't use subclasses and stay within the original keyword hash

The keyword hash maps keyword names, and the 18 format names live in the keyword's value
so 18 subclasses would still need their own name lookup somewhere.
A separate small hash felt like the smaller change

@varundeepsaini
varundeepsaini requested a review from gkodinov July 29, 2026 14:44
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.
@varundeepsaini
varundeepsaini force-pushed the MDEV-30124-json-schema-format-validation branch from 5163d5e to cd929bf Compare July 29, 2026 14:53
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

External Contribution All PRs from entities outside of MariaDB Foundation, Corporation, Codership agreements.

Development

Successfully merging this pull request may close these issues.

4 participants