Improve schema validation error messages with actionable context#355
Open
holodorum wants to merge 8 commits intokson-org:mainfrom
Open
Improve schema validation error messages with actionable context#355holodorum wants to merge 8 commits intokson-org:mainfrom
holodorum wants to merge 8 commits intokson-org:mainfrom
Conversation
The generic "Additional properties are not allowed" message gave no indication of which property was the problem or which schema rejected it. The message now reads "Additional property 'foo' is not allowed in Bar" (with the "in Bar" suffix appearing only when the schema has a title), making it immediately actionable.
Two changes make sub-schema validation errors (anyOf/oneOf) more actionable: 1. descriptionWithDefault() now falls back to schema title before the generic "JSON Object Schema", so sub-schema error groups are labeled distinctively when the schema author provides a title. 2. Per-sub-schema error strings now include the source location (line.column) of each error, so you can tell which field each error refers to even though the errors are rolled up into a single summary message.
When additionalProperties specifies a schema (e.g. a $ref to a model), validation errors like "Expected one of: object, but got: string" give no indication of which property failed or what schema was expected. The validator now emits a contextual message before the schema's own errors: "Property 'integration' must conform to 'OperationMetadataModel'" — giving the reader both the property name and the expected schema, so the dict-of-models pattern is immediately clear.
The generic "Value is not one of the allowed enum values" gave no indication of what the valid choices were, forcing the reader to go look up the schema. The message now reads "Value must be one of: \"active\", \"inactive\", \"pending\"" — listing the allowed values directly in the error. Extracts a shared KsonValue.toDisplayString() extension (in ValueFormatting.kt) used by both EnumValidator and ConstValidator, replacing the duplicated inline when blocks that had already diverged (strings quoted vs. unquoted, different placeholder styles).
Type mismatch errors like "Expected one of: number, but got: string" gave no indication of which property had the wrong type. When a schema is parsed as a property schema, the property name is now threaded through JsonObjectSchema to TypeValidator, producing messages like "Property 'value': Expected one of: number, but got: string". This is especially valuable in sub-schema error rollups (anyOf/oneOf) where multiple type mismatches are listed together and the property name is the key to understanding which alternative was intended.
The generic "Additional items are not allowed" gave no indication of how many items the schema expected vs. how many were present. The message now reads "Expected at most 3 items, but found 5" — making the tuple length constraint immediately clear.
"must be a strings" → "must be strings" in both SCHEMA_STRING_ARRAY_ENTRY_ERROR and SCHEMA_TYPE_ARRAY_ENTRY_ERROR.
SCHEMA_MISSING_REQUIRED_DEPENDENCIES used mixed backtick/single-quote
delimiters (\`foo'). SCHEMA_DEPENDENCIES_SCHEMA_ERROR had the same
issue. Both now use consistent single quotes ('foo') matching the
rest of the error messages.
646fba2 to
39cd100
Compare
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.
KsonValue.toDisplayString()to eliminate divergent formatting inEnumValidatorandConstValidatorDetail
Schema validation errors were generic — "Value is not one of the allowed enum values", "Additional properties are not allowed", "Expected one of: number, but got: string" — forcing the reader to cross-reference the schema to understand what went wrong.
This branch threads contextual information (property names, schema titles, item counts, allowed values, source locations) through the validators so each error message stands on its own. Eight focused commits, each with tests: