Skip to content

Add JSON Schema validation to JsonInput via AJV#4398

Open
lbwexler wants to merge 1 commit into
developfrom
jsonInput-ajv-schema
Open

Add JSON Schema validation to JsonInput via AJV#4398
lbwexler wants to merge 1 commit into
developfrom
jsonInput-ajv-schema

Conversation

@lbwexler

Copy link
Copy Markdown
Member

Summary

Adds optional schema validation to JsonInput via AJV. Split out of #4116 (CodeMirror v6 migration) so each PR has a focused scope.

  • New props on JsonInput:
    • ajvSchema — JSON Schema object passed to AJV for content validation
    • ajvOptions — options forwarded to the AJV constructor
  • When ajvSchema is provided, validation errors are surfaced as CodeMirror lint annotations alongside the existing jsonlint syntax checks. Error highlights point at the offending property in the source text.
  • When ajvSchema is omitted, behavior is unchanged from the v6 baseline (jsonlint-only).
  • New direct dep: ajv ~8.17.1.

Known limitations / follow-ups

  • The AJV validator is currently rebuilt on every JsonInput render (new Ajv() + ajv.compile()). Consumers passing a stable schema reference still pay this cost. Worth memoizing.
  • Error positioning uses text.indexOf(\"key"`)`, which finds the first occurrence of the property name. If the same key appears in multiple objects, the highlight can land on the wrong one.

Both are acceptable for a first cut but worth tracking.

Test plan

  • npx tsc --noEmit clean
  • yarn lint clean
  • Manual smoke: JsonInput without ajvSchema lints syntax errors as before
  • Manual smoke: JsonInput with an ajvSchema flags schema violations (missing required props, type mismatches, additionalProperties: false)

🤖 Generated with Claude Code

Introduces optional ajvSchema and ajvOptions props on JsonInput, layering
AJV-driven schema validation on top of the existing jsonlint syntax
checks. When ajvSchema is provided, validation errors are reported as
CodeMirror lint annotations with offsets pointing at the offending
property in the source text.

Split out of the CodeMirror v6 migration (#4116) to keep that PR focused
on the framework upgrade.
@lbwexler

Copy link
Copy Markdown
Member Author

this is work done originally by Ryan and Pete --- I just split it off from the (orthogonal) CodeMirror upgrade it was bundled with

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant