Skip to content

Conversation

@mjbonifacio
Copy link
Owner

No description provided.

Michael Bonifacio added 7 commits October 1, 2025 08:31
- Add ErrorCategory field to ValidationError struct for consistent error classification
- Create comprehensive constants system for error categories, validation sources, parameter types, and validation types/subtypes
- Add helper methods: IsSchemaError(), IsRetrievalError(), IsStructuralError(), SetErrorCategory()
- Add comprehensive test coverage for new error categorization functionality
- Support three error categories: schema (JSON schema validation), retrieval (missing resources), structural (format/encoding issues)

This establishes the foundation for consistent error handling and enables better conditional logic for API consumers.
- Add ValidationSource field to SchemaValidationFailure struct to identify validation context
- Add SetValidationSource() method to set source on all schema validation errors
- Add DetermineValidationSource() method to automatically determine source from validation type
- Support validation sources: requestBody, responseBody, parameter, document
- Add comprehensive test coverage for ValidationSource functionality

This enables better error context identification for schema validation failures across different validation scenarios.
…rization

- Replace magic strings with type-safe constants in error creation functions
- Update RequestContentTypeNotFound, OperationNotFound, ResponseCodeNotFound, QueryParameterMissing, HeaderParameterMissing, PathParameterMissing functions
- Add automatic error categorization via SetErrorCategory() calls
- Add ParameterName field population for parameter errors
- Update all corresponding tests to verify new constants usage and error categorization
- Ensure ErrorCategory field is properly set (retrieval for missing resources, structural for format issues)

This improves type safety and enables consistent error classification across all error creation functions.
…orization

- Replace magic strings with type-safe constants in FindPath function
- Update path missing and operation missing error creation to use ValidationTypePath and ValidationSubTypeMissing/ValidationSubTypeMissingOperation
- Add automatic error categorization via SetErrorCategory() calls
- Ensure consistent error structure across path validation failures
- All existing tests continue to pass with enhanced error information

This improves consistency between path validation and other error creation functions.
…r information

- Replace magic strings with type-safe constants in ValidateRequestSchema and ValidateResponseSchema functions
- Update ValidationError creation to use ValidationTypeRequest/ValidationTypeResponse and ValidationSubTypeSchema
- Add automatic error categorization via SetErrorCategory() calls
- Add ValidationSource population via SetValidationSource() calls for schema validation errors
- Ensure SchemaValidationFailure objects have proper ValidationSource context (requestBody/responseBody)
- All existing tests continue to pass with enhanced error information

This provides consistent error structure and better context for schema validation failures in request/response bodies.
…sistency

- Convert IncorrectQueryParamEnum and IncorrectQueryParamEnumArray to use SchemaValidationErrors
- Convert IncorrectPathParamNumber and IncorrectPathParamInteger to use SchemaValidationErrors
- Update ValidationTypes to use specific constants (ValidationTypeQuery, ValidationTypePath)
- Update ValidationSubTypes to use semantic constants (ValidationSubTypeEnum, ValidationSubTypeType)
- Add automatic error categorization via SetErrorCategory() calls
- Add ValidationSource population for all SchemaValidationFailure objects
- Update corresponding tests to expect new error structure and validate SchemaValidationErrors presence
- Fix example test output to match new ValidationTypes

This ensures ALL parameter validation failures are consistently reported as SchemaValidationErrors,
providing uniform error structure for downstream consumers like the Ads API.
- Create comprehensive integration test demonstrating all error categories (schema, retrieval, structural)
- Test ValidationSource assignment for all validation contexts (requestBody, responseBody, parameter, document)
- Validate automatic error categorization via SetErrorCategory() method
- Test ValidationSource determination via DetermineValidationSource() method
- Ensure all JIRA requirements are fully met:
  * ✅ Unified error reporting with consistent categorization
  * ✅ Enhanced error location identification with ValidationSource context
  * ✅ Structured data ready for Ads API error mapping
  * ✅ Comprehensive enum-like constants system
  * ✅ Breaking changes implemented with enhanced functionality

This completes the validation error improvement implementation with full test coverage
and validation of all requirements. The system now provides consistent, structured
error information for downstream consumers like the Ads API.
Comment on lines +81 to +82
ValidationType: ValidationTypeQuery,
ValidationSubType: ValidationSubTypeMissing,

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It looks like this is changing the values and you're bringing the type of error up as a sub-type?

// Before
{
  ValidationType: "parameter",
  ValidationSubType: "query"
}

// After
{
  ValidationType: "query",
  ValidationSubType: "missing"
}

I don't think this is quite right though because going off of the description (or at least what we talked about), this error has a schema. The schema said the query param was required, but it wasn't there. This should be as SchemaValidationError right?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The major change here is to add a new field ErrorCategory on ValidationError which can be one of 3 types:

  • Schema
  • Structural
  • Retrieval

But you're right -- it's potentially obscured because it's set by calling SetErrorCategory at the end of each of these functions. I'll consider a way to improve this

return &ValidationError{
ValidationType: helpers.ParameterValidation,
ValidationSubType: helpers.ParameterValidationHeader,
ve := &ValidationError{

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Same thing here, if the schema said the field is required then it's a SchemaValidationError yeah?

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to fix - query/header as an example need schemaValidationErrors set on them to be classified as schemavalidation errors

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On ValidationError, how he currently does it:

  • ValidationType - "parameter"
  • SubType - "query param"
  • message - should be at a "higher" level + reason
  • message/reason - could be a pattern for SchemaValidationErrors - because a parameter can be invalid for multiple reasons
    • but can be more specific for the others, because each will only be for one reason
  • bundle different potential reasons for it being invalid

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Currently:

  • multiple ValidationErrors for diff query param violations
  • but not for request body - this is the only thing that goes through JsonSchema library

Comment on lines +334 to +337
FieldName: param.Name,
FieldPath: param.Name,
InstancePath: []string{param.Name},

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For this case, I think FieldPath should be empty since there's no "traversal" to it, you've already provided the FieldName.

Also, I may find this answer later on as I read the PR, but it seems weird to me that we have FieldPath/FieldName + InstancePath. Is there a difference between the two or can we simplify this a bit by choosing one?

Comment on lines +341 to +347
if sch != nil {
rendered, err := sch.RenderInline()
if err == nil && rendered != nil {
schemaFailure.ReferenceSchema = string(rendered)
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What's this for? I think the underlying libopenapi library already handles inlining all of the references? Assuming it didn't though, this should come at the start of the function since we're accessing sch.Enum right?

I don't think it's necessary, but if we want to protect against schemas that are referenced, we should do it first before running any validation.

Comment on lines +351 to +353
Message: fmt.Sprintf("Query parameter '%s' does not match allowed values", param.Name),
Reason: fmt.Sprintf("The query parameter '%s' has pre-defined values set via an enum. The value '%s' is not one of those values.", param.Name, ef),

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IMO I think we need to do some organizing here. For example, what if this query param has a number of SchemaValidationErrors that need to be raised. With this current structure we're going to give back N validation errors with a single schema validation error.

Maybe this is too nitpicky, but if a parameter fails, it should be a ValidationError with "This parameter failed" and a slice of reasons for why it failed.

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.

3 participants