fix(schema): emit min/max constraints for applicable type kinds#2975
Merged
Conversation
The applicability guard in MinMaxConstraintTypeAttributeKind.addToSchema was inverted: it skipped emitting minimum/maximum/minLength/maxLength exactly when the type's kind WAS applicable, so schema-to-schema round-trips silently dropped these constraints (pattern and description were unaffected since PatternTypeAttributeKind used the correct polarity). Added a schema-schema fixture (test/fixtures/schema/main.js) that validates round-tripped JSON Schema output with AJV, plus test/inputs/schema/schema-constraints.schema with pass/fail samples exercising minLength/maxLength/minimum/maximum, which fail before this fix and pass after it. Co-Authored-By: gpt-5.6-sol via pi <noreply@openai.com>
cjson declares the minmax/minmaxlength features so the fail samples run, but its generated code does not enforce min/max/length constraints — which is why it already skips minmax.schema, minmaxlength.schema, pattern.schema and optional-constraints.schema. The new schema-constraints.schema is the same kind of constraint fixture and was never added to the skip list, so its expected-failure samples were not rejected and schema-cjson failed. Scope it out for cjson alongside the existing constraint schemas. Co-Authored-By: Claude <noreply@anthropic.com>
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.
Bug
Reported in #2557: converting a JSON Schema through quicktype's
--src-lang schema ... --lang schemaround trip silently droppedminLength/maxLength(on strings) andminimum/maximum(onnumbers/integers) from the output schema, while
patternanddescriptionsurvived correctly. Since the schema-to-schema pass is thefirst stage of every other output language too, this also meant the
constraints never reached downstream generators (TypeScript, zod, etc.).
Root cause
MinMaxConstraintTypeAttributeKind.addToSchemainpackages/quicktype-core/src/attributes/Constraints.tshad an invertedapplicability guard:
_typeKindsholds the type kinds a given constraint kind actuallyapplies to (
{"integer", "double"}for min/max value,{"string"}formin/max length). As written, the method bailed out and skipped emitting
the constraint into the schema exactly when the type's kind was
applicable — the opposite of the intended behavior. Compare with
PatternTypeAttributeKind.addToSchema, which has the correct polarity(
if (t.kind !== "string") return;) and is whypatternround-trippedfine while
minLength/maxLength/minimum/maximumdid not.Fix
Inverted the guard:
This is a one-line change; the zod/TypeScript renderers not emitting
these constraints into generated validation code is a separate, unrelated
missing feature and is out of scope for this PR.
Test coverage
schema-schemafixture (test/fixtures/schema/main.js)that validates the round-tripped JSON Schema output against the
original sample using AJV, registered in
test/languages.ts/test/fixtures.ts.test/inputs/schema/schema-constraints.schemawith:schema-constraints.1.json— a sample that satisfiesminLength/maxLength/minimum/maximum.schema-constraints.{1,2}.fail.minmaxlength.json— samples thatviolate
minLength/maxLengthand must be rejected by thegenerated schema.
schema-constraints.{3,4}.fail.minmax.json— samples that violateminimum/maximumand must be rejected by the generated schema.These fail samples only pass once the generated schema actually contains
the constraints, so they reproduce the bug on unfixed code and verify the
fix.
Verification (local)
npm run build— passes.QUICKTEST=true FIXTURE=schema-schema script/test— 68/68 passed(including the new
schema-constraints.schemacases).npm run test:unit— 163/163 tests passed across 25 files.(
node dist/index.js --src-lang schema bad-schema.json --lang schema)and confirmed
minLength/maxLength/minimum/maximumare nowpresent in the generated schema.
FIXTURE=schema, allschema-<lang>targets) is left to CI, since several target-languagetoolchains (e.g.
dotnet) aren't available in this environment.Fixes #2557
🤖 Generated with Claude Code