Skip to content

Add validation support for JSON Schema 'const' keyword#1897

Open
lahma wants to merge 1 commit intoRicoSuter:masterfrom
lahma:fix/1857-const-validation
Open

Add validation support for JSON Schema 'const' keyword#1897
lahma wants to merge 1 commit intoRicoSuter:masterfrom
lahma:fix/1857-const-validation

Conversation

@lahma
Copy link
Copy Markdown
Collaborator

@lahma lahma commented Mar 7, 2026

Summary

  • Adds support for the JSON Schema const keyword validation, which was previously completely ignored by Validate()
  • When a schema defines "const": "someValue", validation now correctly fails if the input value doesn't match
  • Adds Const and HasConst properties to JsonSchema for programmatic access
  • Adds ConstMismatch to ValidationErrorKind enum
  • The const value is extracted from ExtensionData during deserialization, preserving full serialization roundtrip

Test plan

  • 11 new tests in ConstValidationTests.cs covering:
    • String const matching and mismatching
    • Integer const matching and mismatching
    • Boolean const validation
    • Null const validation
    • Object const validation (deep equality)
    • Schema without const (no false positives)
    • Schema roundtrip (serialize/deserialize preserves const)
    • HasConst flag behavior
    • Integration with additionalProperties: false
  • All 459 existing + new tests pass with no regressions

Fixes #1857

🤖 Generated with Claude Code

The Validate() function did not check the 'const' constraint defined in
JSON Schema specification. This adds support by:
- Adding Const/HasConst properties to JsonSchema
- Extracting 'const' from ExtensionData during deserialization
- Adding ConstMismatch to ValidationErrorKind
- Adding ValidateConst to JsonSchemaValidator
- 11 tests covering string, integer, boolean, null, object const values

Fixes RicoSuter#1857

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
@lahma lahma requested a review from RicoSuter March 7, 2026 13:17
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.

Validate() function does not correctly validate the const keyword

1 participant