Debian Codegraph @

errors/validation_error.go

raw
1 // Copyright 2023 Princess B33f Heavy Industries / Dave Shanley
2 // SPDX-License-Identifier: MIT
3
4 package errors
5
6 import (
7 "fmt"
8
9 "github.com/pb33f/libopenapi-validator/helpers"
10 "github.com/santhosh-tekuri/jsonschema/v6"
11 )
12
13 // SchemaValidationFailure describes any failure that occurs when validating data
14 // against either an OpenAPI or JSON Schema. It aims to be a more user-friendly
15 // representation of the error than what is provided by the jsonschema library.
16 type SchemaValidationFailure struct {
17 // Reason is a human-readable message describing the reason for the error.
18 Reason string `json:"reason,omitempty" yaml:"reason,omitempty"`
19
20 // InstancePath is the raw path segments from the root to the failing field
21 InstancePath []string `json:"instancePath,omitempty" yaml:"instancePath,omitempty"`
22
23 // FieldName is the name of the specific field that failed validation (last segment of the path)
24 FieldName string `json:"fieldName,omitempty" yaml:"fieldName,omitempty"`
25
26 // FieldPath is the JSONPath representation of the field location that failed validation (e.g., "$.user.email")
27 FieldPath string `json:"fieldPath,omitempty" yaml:"fieldPath,omitempty"`
28
29 // KeywordLocation is the JSON Pointer (RFC 6901) path to the schema keyword that failed validation
30 // (e.g., "/properties/age/minimum")
31 KeywordLocation string `json:"keywordLocation,omitempty" yaml:"keywordLocation,omitempty"`
32
33 // Line is the line number where the violation occurred. This may a local line number
34 // if the validation is a schema (only schemas are validated locally, so the line number will be relative to
35 // the Context object held by the ValidationError object).
36 Line int `json:"line,omitempty" yaml:"line,omitempty"`
37
38 // Column is the column number where the violation occurred. This may a local column number
39 // if the validation is a schema (only schemas are validated locally, so the column number will be relative to
40 // the Context object held by the ValidationError object).
41 Column int `json:"column,omitempty" yaml:"column,omitempty"`
42
43 // ReferenceSchema is the schema that was referenced in the validation failure.
44 ReferenceSchema string `json:"referenceSchema,omitempty" yaml:"referenceSchema,omitempty"`
45
46 // ReferenceObject is the object that failed schema validation
47 ReferenceObject string `json:"referenceObject,omitempty" yaml:"referenceObject,omitempty"`
48
49 // ReferenceExample is an example object generated from the schema that was referenced in the validation failure.
50 ReferenceExample string `json:"referenceExample,omitempty" yaml:"referenceExample,omitempty"`
51
52 // The original jsonschema.ValidationError object, if the schema failure originated from the jsonschema library.
53 OriginalJsonSchemaError *jsonschema.ValidationError `json:"-" yaml:"-"`
54
55 // Context is the raw schema object that failed validation (for programmatic access)
56 Context interface{} `json:"-" yaml:"-"`
57 }
58
59 // Error returns a string representation of the error
60 func (s *SchemaValidationFailure) Error() string {
61 if s.FieldPath != "" {
62 return fmt.Sprintf("Reason: %s, FieldPath: %s", s.Reason, s.FieldPath)
63 }
64 return fmt.Sprintf("Reason: %s", s.Reason)
65 }
66
67 // ValidationError is a struct that contains all the information about a validation error.
68 type ValidationError struct {
69 // Message is a human-readable message describing the error.
70 Message string `json:"message" yaml:"message"`
71
72 // Reason is a human-readable message describing the reason for the error.
73 Reason string `json:"reason" yaml:"reason"`
74
75 // ValidationType is a string that describes the type of validation that failed.
76 ValidationType string `json:"validationType" yaml:"validationType"`
77
78 // ValidationSubType is a string that describes the subtype of validation that failed.
79 ValidationSubType string `json:"validationSubType" yaml:"validationSubType"`
80
81 // SpecLine is the line number in the spec where the error occurred.
82 SpecLine int `json:"specLine" yaml:"specLine"`
83
84 // SpecCol is the column number in the spec where the error occurred.
85 SpecCol int `json:"specColumn" yaml:"specColumn"`
86
87 // HowToFix is a human-readable message describing how to fix the error.
88 HowToFix string `json:"howToFix" yaml:"howToFix"`
89
90 // RequestPath is the path of the request
91 RequestPath string `json:"requestPath" yaml:"requestPath"`
92
93 // SpecPath is the path from the specification that corresponds to the request
94 SpecPath string `json:"specPath" yaml:"specPath"`
95
96 // RequestMethod is the HTTP method of the request
97 RequestMethod string `json:"requestMethod" yaml:"requestMethod"`
98
99 // ParameterName is the name of the parameter that failed validation (for parameter validation errors)
100 ParameterName string `json:"parameterName,omitempty" yaml:"parameterName,omitempty"`
101
102 // SchemaValidationErrors is a slice of SchemaValidationFailure objects that describe the validation errors
103 // This is only populated when the validation type is against a schema.
104 SchemaValidationErrors []*SchemaValidationFailure `json:"validationErrors,omitempty" yaml:"validationErrors,omitempty"`
105
106 // Context is the object that the validation error occurred on. This is usually a pointer to a schema
107 // or a parameter object.
108 Context interface{} `json:"-" yaml:"-"`
109 }
110
111 // Error returns a string representation of the error
112 func (v *ValidationError) Error() string {
113 if v.SchemaValidationErrors != nil {
114 if v.SpecLine > 0 && v.SpecCol > 0 {
115 return fmt.Sprintf("Error: %s, Reason: %s, Validation Errors: %s, Line: %d, Column: %d",
116 v.Message, v.Reason, v.SchemaValidationErrors, v.SpecLine, v.SpecCol)
117 } else {
118 return fmt.Sprintf("Error: %s, Reason: %s, Validation Errors: %s",
119 v.Message, v.Reason, v.SchemaValidationErrors)
120 }
121 } else {
122 if v.SpecLine > 0 && v.SpecCol > 0 {
123 return fmt.Sprintf("Error: %s, Reason: %s, Line: %d, Column: %d",
124 v.Message, v.Reason, v.SpecLine, v.SpecCol)
125 } else {
126 return fmt.Sprintf("Error: %s, Reason: %s",
127 v.Message, v.Reason)
128 }
129 }
130 }
131
132 // IsPathMissingError returns true if the error has a ValidationType of "path" and a ValidationSubType of "missing"
133 func (v *ValidationError) IsPathMissingError() bool {
134 return v.ValidationType == helpers.PathValidation && v.ValidationSubType == helpers.ValidationMissing
135 }
136
137 // IsOperationMissingError returns true if the error has a ValidationType of "request" and a ValidationSubType of "missingOperation"
138 func (v *ValidationError) IsOperationMissingError() bool {
139 return v.ValidationType == helpers.PathValidation && v.ValidationSubType == helpers.ValidationMissingOperation
140 }