-
Notifications
You must be signed in to change notification settings - Fork 0
Refactor SchemaValidationFailure struct fields #2
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
base: main
Are you sure you want to change the base?
Conversation
| // DeepLocation is the path to the validation failure as exposed by the jsonschema library. | ||
| DeepLocation string `json:"deepLocation,omitempty" yaml:"deepLocation,omitempty"` | ||
| // KeywordLocation is the relative path to the JsonSchema keyword that failed validation | ||
| KeywordLocation string `json:"keywordLocation,omitempty" yaml:"keywordLocation,omitempty"` |
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.
What about just doing SchemaLocation and AbsoluteSchemaLocation? Although that may be somewhat confusing if someone reads it as "where was my schema located" rather than "where did it fail in my schema"?
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.
My idea was that they match up to this: https://json-schema.org/understanding-json-schema/keywords
I stole it directly from the jsonschema error
| ReferenceObject string `json:"referenceObject,omitempty" yaml:"referenceObject,omitempty"` | ||
|
|
||
| // ReferenceExample is an example object generated from the schema that was referenced in the validation failure. | ||
| ReferenceExample string `json:"referenceExample,omitempty" yaml:"referenceExample,omitempty"` |
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.
Looks like this example was removed?
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.
Yeah -- this field isn't written to anywhere in the library, so it would only be read/written to by users of the library. IMO should be deleted since it's effectively a custom user field and just adds confusion
| OriginalError *jsonschema.ValidationError `json:"-" yaml:"-"` | ||
| // DEPRECATED in favor of explicit use of FieldPath & InstancePath | ||
| // Location is the XPath-like location of the validation failure | ||
| Location string `json:"location,omitempty" yaml:"location,omitempty"` |
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.
IMO if we're already making a breaking change, we should just delete this rather than calling it deprecated.
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.
i like
| // Location is the XPath-like location of the validation failure | ||
| Location string `json:"location,omitempty" yaml:"location,omitempty"` | ||
| // InstancePath is the raw path segments from the root to the failing field | ||
| InstancePath []string `json:"instancePath,omitempty" yaml:"instancePath,omitempty"` |
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.
Should this include the actual field itself?
Example:
{
"InstancePath": [ "user" ],
"FieldName": "email",
"FieldPath": "$.user.email"
}
Additionally, I think it's a bit confusing when someone should use InstancePath vs FieldPath. What do you think about consolidating this a bit to something like
type SchemaValidationFailure {
InstancePath[] string // Inclusive of the field name
}
func (svf *SchemaValidationFailure) GetJSONPathToField() string {
// Build the JSONPath string from the InstancePath slice
return ....
}
func (svf *SchemaValidationFailure) GetField() string {
// Have some additional length checks...
return svf.InstancePath[:-1]
}
failure := &SchemaValidationFailure{
InstancePath: [ "user", "email" ]
}
fmt.Println(failure.GetJSONPathToField()) // $.user.email
fmt.Println(failure.GetField()) // emailThere 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.
didn't change anything here, just moved this struct field up to group stuff that's on the incoming payload
3d9224f to
5e58242
Compare
SchemaValidationFailure Struct Refactoring
tl;dr I wanted to get full clarity on how things will change before making all the changes to re-map fields, locations, values, etc.
This PR clarifies the purpose of each struct field on
SchemaValidationFailure, and get buy in on that before making a huge batch of changes.Summary
Refactored
SchemaValidationFailurestruct to align with JSON Schema specification terminology, improve clarity, and eliminate ambiguity in field naming.Changes Made
1. Renamed Fields for JSON Schema Alignment
DeepLocation→KeywordLocationDeepLocation string- Ambiguous name, unclear what "deep" referred toKeywordLocation string- Matches JSON Schema spec terminologyKeywordLocationis the official JSON Schema term from thejsonschema/v6library/properties/email/pattern)er.KeywordLocationfromjsonschema.OutputUnitAbsoluteLocation→AbsoluteKeywordLocationAbsoluteLocation string- Lost the "Keyword" contextAbsoluteKeywordLocation string- Full, unambiguous nameKeywordLocation(relative vs absolute)er.AbsoluteKeywordLocationfromjsonschema.OutputUnit2. Field Organization Improvements
Moved to Better Logical Grouping
This separation makes it crystal clear:
InstancePath,FieldPath,FieldName→ Where in the DATAKeywordLocation,AbsoluteKeywordLocation→ Where in the SCHEMA3. Removed Unused Field
ReferenceExample- DELETED4. Improved Documentation
ReferenceSchemaCommentReferenceObjectComment5. Deprecated Ambiguous Field
Location- DEPRECATED// DEPRECATED in favor of explicit use of FieldPath & InstancePather.InstanceLocation(data location)er.KeywordLocation(schema location)"unavailable","schema compilation","/required"FieldPath/InstancePathfor data,KeywordLocationfor schema6. Fixed Comment Typo
Line 99 in
ValidationErrorstructVerification
Compilation Check
DeepLocation→KeywordLocationupdatedAbsoluteLocation→AbsoluteKeywordLocationupdatedUpdated Files
errors/validation_error.go- Struct definitionsschema_validation/validate_schema.go- SetsKeywordLocationandAbsoluteKeywordLocationschema_validation/validate_document.go- SetsKeywordLocationandAbsoluteKeywordLocationBackward Compatibility
Locationfield still exists (deprecated but functional)keywordLocation,absoluteKeywordLocation)Key Insights Discovered
HTTP Component Context Already Exists
During this refactoring, we discovered that
ValidationErroralready provides clear HTTP component context via:ValidationType: "parameter", "requestBody", "response"ValidationSubType: "path", "query", "header", "cookie", "schema"This means consumers can easily determine if an error is from:
ValidationType == "parameter" && ValidationSubType == "path"ValidationType == "parameter" && ValidationSubType == "query"ValidationType == "parameter" && ValidationSubType == "header"ValidationType == "parameter" && ValidationSubType == "cookie"ValidationType == "requestBody"ValidationType == "response"The
SchemaValidationFailure.Locationfield was never meant to indicate HTTP component - it was for within-field location.Future Work (Separate Commit)
1. Add Tests for KeywordLocation Invariant
After the schema refactoring is complete, add tests to validate the following invariant:
OriginalJsonSchemaErrorisnil, bothKeywordLocationandAbsoluteKeywordLocationshould be empty stringsOriginalJsonSchemaErroris notnil, both fields may be populated (when the error originates from JSON Schema validation)Rationale: This documents the expected behavior that keyword location fields are only relevant when the validation failure originated from JSON Schema validation, not from other types of validation (e.g., parameter encoding errors, schema compilation errors).
Test scenarios:
2. Remove Deprecated
LocationFieldCompletely remove the deprecated
Locationfield fromSchemaValidationFailurein favor of its more specific counterparts:FieldNamefor the specific field that failedFieldPathfor the JSONPath representationCurrent state:
Actions required:
Locationfield from the structLocationto useFieldPathorFieldNameinsteadError()method to use non-deprecated fields3. Update
SchemaValidationFailure.Error()MethodThe
Error()method currently uses the deprecatedLocationfield:Should be updated to use non-deprecated fields:
Note: This change should be done in conjunction with removing the
Locationfield to ensure all error messages remain informative.Benefits
jsonschema.OutputUnit)LocationfieldReferenceExamplefieldBreaking Changes
DeepLocationfield (nowKeywordLocation)AbsoluteLocationfield (nowAbsoluteKeywordLocation)However, this is justified because:
Locationanyway (now deprecated)Related Context
Ongoing Work: Path Parameter Validation Errors
This refactoring is part of a larger effort to ensure all validation errors include comprehensive
SchemaValidationErrors. We're systematically reviewing all error paths inValidatePathParamsWithPathItemto populate schema validation details consistently.