-
Notifications
You must be signed in to change notification settings - Fork 2k
fix(validators): honor declared draft-07/06 JSON Schema dialects instead of rejecting them #2534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
felixweinberger
wants to merge
1
commit into
main
Choose a base branch
from
fweinberger/validator-dialect-tolerance
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| --- | ||
| '@modelcontextprotocol/server': patch | ||
| '@modelcontextprotocol/client': patch | ||
| --- | ||
|
|
||
| The default validator now honors declared 2019-09 and draft-07/06 dialects instead of rejecting them: a schema stamped `"$schema": "http://json-schema.org/draft-07/schema#"` (zod-to-json-schema's default output) validates with draft-07 semantics, and a 2019-09 stamp (zod-to-json-schema's `2019-09`/`openAi` targets) with 2019-09 semantics, on both the Ajv and Cloudflare Workers providers (one documented engine difference: classic Ajv evaluates keywords alongside `$ref`, which draft-07 says to ignore — see the migration guide). Schemas with no `$schema` still validate as 2020-12, and unknown dialects still produce the typed error (now listing the supported dialects: 2020-12, 2019-09, draft-07, draft-06). |
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
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
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
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🟡 A second, undocumented draft-07 engine divergence: @cfworker/json-schema's dereference() doesn't treat draft-07
dependenciesas a name→subschema map, so an entry whose key collides with the engine's ignored-keyword set (type,default,format,required,pattern,id, …) and contains a$refis skipped during ref registration — the returned validator then throwsUnresolved $refdata-dependently when the dependency triggers, surfacing as InvalidParams incallToolon browser/Workers while the byte-identical schema validates correctly on the Node (classic Ajv) path. Consider documenting it alongside the $ref-siblings deviation (JSDoc + migration guide) and adding a cfworker engine-leg pinning test; longer-term, pre-normalizing draft-07dependenciesintodependentSchemas/dependentRequiredbefore handing the schema to cfworker would close the gap.Extended reasoning...
What the bug is. Routing declared draft-07/06 schemas to
@cfworker/json-schemadraft'7'(_draftFor,cfWorkerProvider.ts:63-71) makes draft-07dependenciesreachable through the default browser/Workers validator for the first time — but the pinned engine (4.1.1) has a gap the SDK now surfaces. Itsdereference()does not listdependenciesin itsschemaMapKeywordset (only$defs/definitions/properties/patternProperties/dependentSchemas), so thedependenciesmap is walked in keyword position: any entry whose author-chosen property-name key collides with the engine'signoredKeywordset (type,default,format,required,pattern,enum,const,id,$id,minimum,maxLength, …) is skipped entirely, and a$refinside it is never registered. At validate time, when the dependency triggers, the engine throwsError('Unresolved $ref "#/definitions/…"')instead of returning a result.Step-by-step proof (verified against the pinned
@cfworker/json-schema4.1.1 with the provider's exact construction,new Validator(schema, '7', true)):The same throw reproduces for entry keys
default,format,required,pattern,id,minimum; a non-colliding key (e.g.creditCard) validates correctly — confirming the dereference-skip mechanism. The engine's known-schemas map registers#/dependenciesbut not#/dependencies/type, sovalidate.jsfalls back to the raw pointer, which is not a lookup key, and throws.Why existing safeguards don't catch it. The
Validatorconstructor succeeds, so this is not caught by the per-tool compile-error isolation (client:jsonschema:bad-schema-isolates-tool) — the throw happens inside the returned validator closure, data-dependently. InClient.callTool(client.ts~2449-2467) it is caught and converted toProtocolError(InvalidParams, 'Failed to validate structured content: Unresolved $ref …'). The shareddeclaredDialectclassifier guarantees both platforms pick the same dialect, but cannot make the cfworker engine's draft-7 keyword coverage complete. And the new engine-leg tests are Ajv-only — thevalidators.test.tscomment assumes cfworker dispatch differences are benign leniency ("applies both keyword sets in either draft mode"), which this shape disproves: it is a hard throw, not leniency. Notably,dependenciesis exactly the keyword this PR deliberately supported in the legacyWrap rewriter — including a test for an entry keyeddefaultwith a$refinside — yet that shape never ran through the cfworker engine.Impact. A spec-valid draft-07 tool with spec-valid
structuredContentworks on a default Node client but fails intermittently (only on results where the dependency triggers) withInvalidParamson the byte-identical browser/Cloudflare Workers client. The same asymmetry applies server-side viafromJsonSchemainput validation on Workers runtimes. This is a second engine divergence beyond the $ref-siblings one the PR documents — in the opposite direction (hard failure rather than Ajv-side strictness) — so the changeset's "one documented engine difference" framing and its "validates with draft-07 semantics on both … providers" claim are incomplete.Why nit rather than blocking. Nothing that worked pre-PR regresses: before this change, any draft-07-declared schema was rejected pre-wire with the typed error on both platforms (100% failure), so the divergence is newly created surface, not a regression. The trigger intersection is narrow — schema-form
dependencies+ an entry key colliding with cfworker's ignored-keyword list + a$refinside + a Workers/browser runtime + data that actually fires the dependency (zod-to-json-schemadoes not emitdependencies). And the root cause is an upstream engine limitation the SDK surfaces, not wrong SDK dispatch logic.How to fix. Match the treatment the PR gave the $ref-siblings divergence: document it in the
CfWorkerJsonSchemaValidatorJSDoc and the migration guide's "known draft-07 engine difference" paragraph, and add a cfworker engine-leg pinning test (so a dependency bump that fixes or shifts the behavior is caught). Longer-term options: pre-normalize draft-07dependenciesintodependentSchemas/dependentRequiredbefore constructing the cfworkerValidator, or fix upstream by addingdependenciestoschemaMapKeywordin@cfworker/json-schema's dereference.