feat(schema): distinguish input vs output JSON schemas (closes #2508) - #2563
Open
genisis0x wants to merge 1 commit into
Open
feat(schema): distinguish input vs output JSON schemas (closes #2508)#2563genisis0x wants to merge 1 commit into
genisis0x wants to merge 1 commit into
Conversation
…2508) Glaze previously emitted a single JSON Schema for every type even though the serialization and parsing contracts differ. Output guarantees every non-skipped key is written and that tuples are emitted at full length; input permits partial objects (no `required`) and partial tuples (no `minItems`). The mixed behavior — emitting `required` on objects but omitting `minItems` on tuples (see stephenberry#2461) — described neither contract fully. Introduce `schema_purpose { input, output }` and thread it through `to_json_schema` via an `opts_with_schema_purpose` wrapper that carries the tag in the existing constexpr Opts. New entry points: - `write_json_input_schema<T>()` — partial-parse friendly: no `required` on objects, no `minItems` on tuples. - `write_json_output_schema<T>()` — serialization contract: `required` listed for guaranteed-present keys, `minItems == maxItems` on tuples. `write_json_schema` keeps its existing signature and defaults to `output`, which preserves the existing `required` behavior on objects and additionally emits `minItems` on tuples so output schemas now fully describe what glaze serializes. User-provided `meta<V>::required` is always honored regardless of purpose, since it expresses a developer-asserted parsing requirement. Variant discriminator tags also remain in `required` for input schemas because the tag must be present for the parser to select an alternative.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Closes #2508.
Glaze previously emitted a single JSON Schema for every type even though serialization and parsing contracts differ. The mixed behavior —
requiredon objects but nominItemson tuples after #2461 — described neither contract fully.This PR introduces
schema_purpose { input, output }and threads it throughto_json_schemavia anopts_with_schema_purposewrapper that carries the tag in the existing constexprOpts.New entry points:
glz::write_json_input_schema<T>()— partial-parse friendly: norequiredon objects, nominItemson tuples.glz::write_json_output_schema<T>()— serialization contract:requiredlisted for guaranteed-present keys,minItems == maxItemson tuples.glz::write_json_schema<T>()keeps its existing signature and now defaults tooutputpurpose. For objects this preserves the existingrequiredemission; for tuples this newly emitsminItems == maxItems, completing the output-schema contract.User-provided
meta<V>::requiredis always honored regardless of purpose, since it expresses a developer-asserted parsing requirement. Variant discriminator tags also stay inrequiredfor input schemas because the tag must be present to select an alternative.Test plan
jsonschema_testbuilds cleanly.tuple schema uses prefixItemsexpected string to includeminItems.schema_purpose_testsuite covers:minItems,minItems == maxItems,required(witherror_on_missing_keys),required,write_json_schemamatches output purpose.