-
Notifications
You must be signed in to change notification settings - Fork 38
Refactor SchemaValidationFailure Struct Fields for Clarity #188
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
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #188 +/- ##
=======================================
Coverage 97.08% 97.09%
=======================================
Files 40 41 +1
Lines 4397 4403 +6
=======================================
+ Hits 4269 4275 +6
Misses 128 128
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
| // ReferenceExample is an example object generated from the schema that was referenced in the validation failure. | ||
| ReferenceExample string `json:"referenceExample,omitempty" yaml:"referenceExample,omitempty"` | ||
| // The original jsonschema.ValidationError object, if the schema failure originated from the jsonschema library. | ||
| OriginalJsonSchemaError *jsonschema.ValidationError `json:"-" yaml:"-"` |
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.
Why remove the ReferenceExample?
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.
Why change the name of the OriginalError to OriginalJsonSchemaError? it's the same type, I think just updating the docs is sufficient here.
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.
Thanks for the feedback! Sure, let me explain.
The overall goal here is to clarify the responsibility of the SchemaValidationFailure type as much as possible. In this case, I believe that its responsibility will be broadened a bit.
- Before: A thin wrapper around
jsonschema.ValidationError, focused only onjsonschemaissues. - After: A more comprehensive object that can report on any validation failure, whether it's a JSON Schema constraint or a different, OpenAPI-specific constraint.
-
On renaming
OriginalErrortoOriginalJsonSchemaError: I made this change to make the field's purpose more explicit. It clarifies that this field will only be populated when the failure is due to a JSON Schema rule. For other OpenAPI-specific validation failures, this field will be nil. -
On removing
ReferenceExample: I didn't see it referenced anywhere else in the code so I thought it should be removed for clarity -- It was introduced here but it wasn't clear to me if the intended use was ever carried out. Would you mind explaining its intended use by library users? Happy to add it back if that makes sense
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.
-
OK. I can buy that.
-
I need it.
https://github.com/pb33f/wiretap/blob/main/ui/src/components/violation/violation-details.ts#L165
I have not yet built what I need, but I need that property when I do.
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 will need ReferenceExample to remain for downstream purposes, other than that I like the changes.
3d9224f to
5e58242
Compare
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.
LGTM
tl;dr - I'm working with @its-hammer-time to the following end goal:
In order to do this, I propose some changes to struct fields on
SchemaValidationFailure. Then I will make changes so that each existing validation is consistent with those definitions.Once that's done, I'd like to merge this into a working branch as the changes will be breaking. Then, I'd like to implement the changes in error mapping/handling in another branch of my fork (and create another PR).
Full details below:
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
ReferenceExampleReferenceExample- DELETED4. Improved Documentation
ReferenceSchemaCommentReferenceObjectComment5. Prepared
Locationfor deletionLocation- 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)ValidationType/SubType will tell us what part of the HTTP request failed
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.