2.5 KiB
Gemini Project Overview: jspg
This document outlines the purpose of the jspg project and the specific modifications made to the vendored boon JSON schema validator crate.
What is jspg?
jspg is a PostgreSQL extension written in Rust using the pgrx framework. Its primary function is to provide fast, in-database JSON schema validation against the 2020-12 draft of the JSON Schema specification.
It works by:
- Exposing a SQL function,
cache_json_schemas(...), which takes arrays of schema objects, compiles them, and caches them in memory. - Exposing a SQL validation function,
validate_json_schema(schema_id, instance), which validates a JSONB instance against one of the pre-cached schemas. - Using a locally modified (vendored) version of the
booncrate to perform the validation, allowing for custom enhancements to its core logic.
boon Crate Modifications
The version of boon located in the validator/ directory has been modified to address specific requirements of the jspg project. The key deviations from the upstream boon crate are as follows:
1. Recursive Runtime Strictness Control
-
Problem: The
jspgproject requires that certain schemas enforce a strict "no extra properties" policy (specifically, schemas for publicpuncsand globaltypes). This strictness needs to cascade through the entire validation hierarchy, including all nested objects and$refchains. A compile-time flag was unsuitable because it would incorrectly apply strictness to shared, reusable schemas. -
Solution: A runtime validation option was implemented to enforce strictness recursively.
- A
ValidationOptions { be_strict: bool }struct was added. Thejspgcode insrc/lib.rsdetermines whether a validation run should be strict (based on thepunc'spublicflag or if validating a globaltype) and passes the appropriate option to the validator. - The
be_strictoption is propagated through the entire recursive validation process. A bug was fixed in_validate_self(which handles$refs) to ensure that the sub-validator is always initialized to track unevaluated properties whenbe_strictis enabled. Previously, tracking was only initiated if the parent was already tracking unevaluated properties, causing strictness to be dropped across certain$refboundaries. - At any time, if
unevaluatedPropertiesoradditionalPropertiesis found in the schema, it should override the strict (or non-strict) validation at that level.
- A