1package gojsonschema 2 3import ( 4 "fmt" 5 "strings" 6) 7 8type ( 9 // RequiredError. ErrorDetails: property string 10 RequiredError struct { 11 ResultErrorFields 12 } 13 14 // InvalidTypeError. ErrorDetails: expected, given 15 InvalidTypeError struct { 16 ResultErrorFields 17 } 18 19 // NumberAnyOfError. ErrorDetails: - 20 NumberAnyOfError struct { 21 ResultErrorFields 22 } 23 24 // NumberOneOfError. ErrorDetails: - 25 NumberOneOfError struct { 26 ResultErrorFields 27 } 28 29 // NumberAllOfError. ErrorDetails: - 30 NumberAllOfError struct { 31 ResultErrorFields 32 } 33 34 // NumberNotError. ErrorDetails: - 35 NumberNotError struct { 36 ResultErrorFields 37 } 38 39 // MissingDependencyError. ErrorDetails: dependency 40 MissingDependencyError struct { 41 ResultErrorFields 42 } 43 44 // InternalError. ErrorDetails: error 45 InternalError struct { 46 ResultErrorFields 47 } 48 49 // EnumError. ErrorDetails: allowed 50 EnumError struct { 51 ResultErrorFields 52 } 53 54 // ArrayNoAdditionalItemsError. ErrorDetails: - 55 ArrayNoAdditionalItemsError struct { 56 ResultErrorFields 57 } 58 59 // ArrayMinItemsError. ErrorDetails: min 60 ArrayMinItemsError struct { 61 ResultErrorFields 62 } 63 64 // ArrayMaxItemsError. ErrorDetails: max 65 ArrayMaxItemsError struct { 66 ResultErrorFields 67 } 68 69 // ItemsMustBeUniqueError. ErrorDetails: type 70 ItemsMustBeUniqueError struct { 71 ResultErrorFields 72 } 73 74 // ArrayMinPropertiesError. ErrorDetails: min 75 ArrayMinPropertiesError struct { 76 ResultErrorFields 77 } 78 79 // ArrayMaxPropertiesError. ErrorDetails: max 80 ArrayMaxPropertiesError struct { 81 ResultErrorFields 82 } 83 84 // AdditionalPropertyNotAllowedError. ErrorDetails: property 85 AdditionalPropertyNotAllowedError struct { 86 ResultErrorFields 87 } 88 89 // InvalidPropertyPatternError. ErrorDetails: property, pattern 90 InvalidPropertyPatternError struct { 91 ResultErrorFields 92 } 93 94 // StringLengthGTEError. ErrorDetails: min 95 StringLengthGTEError struct { 96 ResultErrorFields 97 } 98 99 // StringLengthLTEError. ErrorDetails: max 100 StringLengthLTEError struct { 101 ResultErrorFields 102 } 103 104 // DoesNotMatchPatternError. ErrorDetails: pattern 105 DoesNotMatchPatternError struct { 106 ResultErrorFields 107 } 108 109 // DoesNotMatchFormatError. ErrorDetails: format 110 DoesNotMatchFormatError struct { 111 ResultErrorFields 112 } 113 114 // MultipleOfError. ErrorDetails: multiple 115 MultipleOfError struct { 116 ResultErrorFields 117 } 118 119 // NumberGTEError. ErrorDetails: min 120 NumberGTEError struct { 121 ResultErrorFields 122 } 123 124 // NumberGTError. ErrorDetails: min 125 NumberGTError struct { 126 ResultErrorFields 127 } 128 129 // NumberLTEError. ErrorDetails: max 130 NumberLTEError struct { 131 ResultErrorFields 132 } 133 134 // NumberLTError. ErrorDetails: max 135 NumberLTError struct { 136 ResultErrorFields 137 } 138) 139 140// newError takes a ResultError type and sets the type, context, description, details, value, and field 141func newError(err ResultError, context *jsonContext, value interface{}, locale locale, details ErrorDetails) { 142 var t string 143 var d string 144 switch err.(type) { 145 case *RequiredError: 146 t = "required" 147 d = locale.Required() 148 case *InvalidTypeError: 149 t = "invalid_type" 150 d = locale.InvalidType() 151 case *NumberAnyOfError: 152 t = "number_any_of" 153 d = locale.NumberAnyOf() 154 case *NumberOneOfError: 155 t = "number_one_of" 156 d = locale.NumberOneOf() 157 case *NumberAllOfError: 158 t = "number_all_of" 159 d = locale.NumberAllOf() 160 case *NumberNotError: 161 t = "number_not" 162 d = locale.NumberNot() 163 case *MissingDependencyError: 164 t = "missing_dependency" 165 d = locale.MissingDependency() 166 case *InternalError: 167 t = "internal" 168 d = locale.Internal() 169 case *EnumError: 170 t = "enum" 171 d = locale.Enum() 172 case *ArrayNoAdditionalItemsError: 173 t = "array_no_additional_items" 174 d = locale.ArrayNoAdditionalItems() 175 case *ArrayMinItemsError: 176 t = "array_min_items" 177 d = locale.ArrayMinItems() 178 case *ArrayMaxItemsError: 179 t = "array_max_items" 180 d = locale.ArrayMaxItems() 181 case *ItemsMustBeUniqueError: 182 t = "unique" 183 d = locale.Unique() 184 case *ArrayMinPropertiesError: 185 t = "array_min_properties" 186 d = locale.ArrayMinProperties() 187 case *ArrayMaxPropertiesError: 188 t = "array_max_properties" 189 d = locale.ArrayMaxProperties() 190 case *AdditionalPropertyNotAllowedError: 191 t = "additional_property_not_allowed" 192 d = locale.AdditionalPropertyNotAllowed() 193 case *InvalidPropertyPatternError: 194 t = "invalid_property_pattern" 195 d = locale.InvalidPropertyPattern() 196 case *StringLengthGTEError: 197 t = "string_gte" 198 d = locale.StringGTE() 199 case *StringLengthLTEError: 200 t = "string_lte" 201 d = locale.StringLTE() 202 case *DoesNotMatchPatternError: 203 t = "pattern" 204 d = locale.DoesNotMatchPattern() 205 case *DoesNotMatchFormatError: 206 t = "format" 207 d = locale.DoesNotMatchFormat() 208 case *MultipleOfError: 209 t = "multiple_of" 210 d = locale.MultipleOf() 211 case *NumberGTEError: 212 t = "number_gte" 213 d = locale.NumberGTE() 214 case *NumberGTError: 215 t = "number_gt" 216 d = locale.NumberGT() 217 case *NumberLTEError: 218 t = "number_lte" 219 d = locale.NumberLTE() 220 case *NumberLTError: 221 t = "number_lt" 222 d = locale.NumberLT() 223 } 224 225 err.SetType(t) 226 err.SetContext(context) 227 err.SetValue(value) 228 err.SetDetails(details) 229 details["field"] = err.Field() 230 err.SetDescription(formatErrorDescription(d, details)) 231} 232 233// formatErrorDescription takes a string in this format: %field% is required 234// and converts it to a string with replacements. The fields come from 235// the ErrorDetails struct and vary for each type of error. 236func formatErrorDescription(s string, details ErrorDetails) string { 237 for name, val := range details { 238 s = strings.Replace(s, "%"+strings.ToLower(name)+"%", fmt.Sprintf("%v", val), -1) 239 } 240 241 return s 242} 243