ref: improve error handling in extended schema validation

This commit is contained in:
ameersohel45
2025-12-14 23:07:58 +05:30
parent cd49b7dda9
commit 3b59507f15
2 changed files with 18 additions and 8 deletions

View File

@@ -11,6 +11,7 @@ import (
"time"
"github.com/beckn-one/beckn-onix/pkg/log"
"github.com/beckn-one/beckn-onix/pkg/model"
"github.com/getkin/kin-openapi/openapi3"
)
@@ -97,7 +98,20 @@ func (v *schemav2Validator) validateExtendedSchemas(ctx context.Context, body in
obj.Path, obj.Context, obj.Type)
if err := v.schemaCache.validateReferencedObject(ctx, obj, ttl, timeout, allowedDomains); err != nil {
return err
// Extract and prefix error paths
var schemaErrors []model.Error
v.extractSchemaErrors(err, &schemaErrors)
// Prefix all paths with object path
for i := range schemaErrors {
if schemaErrors[i].Paths != "" {
schemaErrors[i].Paths = obj.Path + "." + schemaErrors[i].Paths
} else {
schemaErrors[i].Paths = obj.Path
}
}
return &model.SchemaValidationErr{Errors: schemaErrors}
}
}
@@ -384,7 +398,7 @@ func (c *schemaCache) validateReferencedObject(
}
if err := schema.Value.VisitJSON(domainData, opts...); err != nil {
log.Debugf(ctx, "Validation failed for @type: %s at path: %s: %v", obj.Type, obj.Path, err)
return fmt.Errorf("at %s: %w", obj.Path, err)
return err
}
log.Debugf(ctx, "Validation passed for @type: %s at path: %s", obj.Type, obj.Path)

View File

@@ -48,8 +48,6 @@ type Config struct {
ExtendedSchemaConfig ExtendedSchemaConfig
}
// New creates a new Schemav2Validator instance.
func New(ctx context.Context, config *Config) (*schemav2Validator, func() error, error) {
if config == nil {
@@ -142,9 +140,9 @@ func (v *schemav2Validator) Validate(ctx context.Context, reqURL *url.URL, data
if v.config.EnableExtendedSchema && v.schemaCache != nil {
log.Debugf(ctx, "Starting Extended Schema validation for action: %s", action)
if err := v.validateExtendedSchemas(ctx, jsonData); err != nil {
// Extended Schema failure - return error.
// Extended Schema failure - return error
log.Debugf(ctx, "Extended Schema validation failed for action %s: %v", action, err)
return v.formatValidationError(err)
return err
}
log.Debugf(ctx, "Extended Schema validation passed for action: %s", action)
}
@@ -411,5 +409,3 @@ func (v *schemav2Validator) getActionValue(contextSchema *openapi3.Schema) strin
return ""
}