1// Copyright 2015 xeipuuv ( https://github.com/xeipuuv ) 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15// author xeipuuv 16// author-github https://github.com/xeipuuv 17// author-mail xeipuuv@gmail.com 18// 19// repository-name gojsonschema 20// repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language. 21// 22// description Contains const string and messages. 23// 24// created 01-01-2015 25 26package gojsonschema 27 28type ( 29 // locale is an interface for defining custom error strings 30 locale interface { 31 32 // False returns a format-string for "false" schema validation errors 33 False() string 34 35 // Required returns a format-string for "required" schema validation errors 36 Required() string 37 38 // InvalidType returns a format-string for "invalid type" schema validation errors 39 InvalidType() string 40 41 // NumberAnyOf returns a format-string for "anyOf" schema validation errors 42 NumberAnyOf() string 43 44 // NumberOneOf returns a format-string for "oneOf" schema validation errors 45 NumberOneOf() string 46 47 // NumberAllOf returns a format-string for "allOf" schema validation errors 48 NumberAllOf() string 49 50 // NumberNot returns a format-string to format a NumberNotError 51 NumberNot() string 52 53 // MissingDependency returns a format-string for "missing dependency" schema validation errors 54 MissingDependency() string 55 56 // Internal returns a format-string for internal errors 57 Internal() string 58 59 // Const returns a format-string to format a ConstError 60 Const() string 61 62 // Enum returns a format-string to format an EnumError 63 Enum() string 64 65 // ArrayNotEnoughItems returns a format-string to format an error for arrays having not enough items to match positional list of schema 66 ArrayNotEnoughItems() string 67 68 // ArrayNoAdditionalItems returns a format-string to format an ArrayNoAdditionalItemsError 69 ArrayNoAdditionalItems() string 70 71 // ArrayMinItems returns a format-string to format an ArrayMinItemsError 72 ArrayMinItems() string 73 74 // ArrayMaxItems returns a format-string to format an ArrayMaxItemsError 75 ArrayMaxItems() string 76 77 // Unique returns a format-string to format an ItemsMustBeUniqueError 78 Unique() string 79 80 // ArrayContains returns a format-string to format an ArrayContainsError 81 ArrayContains() string 82 83 // ArrayMinProperties returns a format-string to format an ArrayMinPropertiesError 84 ArrayMinProperties() string 85 86 // ArrayMaxProperties returns a format-string to format an ArrayMaxPropertiesError 87 ArrayMaxProperties() string 88 89 // AdditionalPropertyNotAllowed returns a format-string to format an AdditionalPropertyNotAllowedError 90 AdditionalPropertyNotAllowed() string 91 92 // InvalidPropertyPattern returns a format-string to format an InvalidPropertyPatternError 93 InvalidPropertyPattern() string 94 95 // InvalidPropertyName returns a format-string to format an InvalidPropertyNameError 96 InvalidPropertyName() string 97 98 // StringGTE returns a format-string to format an StringLengthGTEError 99 StringGTE() string 100 101 // StringLTE returns a format-string to format an StringLengthLTEError 102 StringLTE() string 103 104 // DoesNotMatchPattern returns a format-string to format an DoesNotMatchPatternError 105 DoesNotMatchPattern() string 106 107 // DoesNotMatchFormat returns a format-string to format an DoesNotMatchFormatError 108 DoesNotMatchFormat() string 109 110 // MultipleOf returns a format-string to format an MultipleOfError 111 MultipleOf() string 112 113 // NumberGTE returns a format-string to format an NumberGTEError 114 NumberGTE() string 115 116 // NumberGT returns a format-string to format an NumberGTError 117 NumberGT() string 118 119 // NumberLTE returns a format-string to format an NumberLTEError 120 NumberLTE() string 121 122 // NumberLT returns a format-string to format an NumberLTError 123 NumberLT() string 124 125 // Schema validations 126 127 // RegexPattern returns a format-string to format a regex-pattern error 128 RegexPattern() string 129 130 // GreaterThanZero returns a format-string to format an error where a number must be greater than zero 131 GreaterThanZero() string 132 133 // MustBeOfA returns a format-string to format an error where a value is of the wrong type 134 MustBeOfA() string 135 136 // MustBeOfAn returns a format-string to format an error where a value is of the wrong type 137 MustBeOfAn() string 138 139 // CannotBeUsedWithout returns a format-string to format a "cannot be used without" error 140 CannotBeUsedWithout() string 141 142 // CannotBeGT returns a format-string to format an error where a value are greater than allowed 143 CannotBeGT() string 144 145 // MustBeOfType returns a format-string to format an error where a value does not match the required type 146 MustBeOfType() string 147 148 // MustBeValidRegex returns a format-string to format an error where a regex is invalid 149 MustBeValidRegex() string 150 151 // MustBeValidFormat returns a format-string to format an error where a value does not match the expected format 152 MustBeValidFormat() string 153 154 // MustBeGTEZero returns a format-string to format an error where a value must be greater or equal than 0 155 MustBeGTEZero() string 156 157 // KeyCannotBeGreaterThan returns a format-string to format an error where a key is greater than the maximum allowed 158 KeyCannotBeGreaterThan() string 159 160 // KeyItemsMustBeOfType returns a format-string to format an error where a key is of the wrong type 161 KeyItemsMustBeOfType() string 162 163 // KeyItemsMustBeUnique returns a format-string to format an error where keys are not unique 164 KeyItemsMustBeUnique() string 165 166 // ReferenceMustBeCanonical returns a format-string to format a "reference must be canonical" error 167 ReferenceMustBeCanonical() string 168 169 // NotAValidType returns a format-string to format an invalid type error 170 NotAValidType() string 171 172 // Duplicated returns a format-string to format an error where types are duplicated 173 Duplicated() string 174 175 // HttpBadStatus returns a format-string for errors when loading a schema using HTTP 176 HttpBadStatus() string 177 178 // ParseError returns a format-string for JSON parsing errors 179 ParseError() string 180 181 // ConditionThen returns a format-string for ConditionThenError errors 182 ConditionThen() string 183 184 // ConditionElse returns a format-string for ConditionElseError errors 185 ConditionElse() string 186 187 // ErrorFormat returns a format string for errors 188 ErrorFormat() string 189 } 190 191 // DefaultLocale is the default locale for this package 192 DefaultLocale struct{} 193) 194 195// False returns a format-string for "false" schema validation errors 196func (l DefaultLocale) False() string { 197 return "False always fails validation" 198} 199 200// Required returns a format-string for "required" schema validation errors 201func (l DefaultLocale) Required() string { 202 return `{{.property}} is required` 203} 204 205// InvalidType returns a format-string for "invalid type" schema validation errors 206func (l DefaultLocale) InvalidType() string { 207 return `Invalid type. Expected: {{.expected}}, given: {{.given}}` 208} 209 210// NumberAnyOf returns a format-string for "anyOf" schema validation errors 211func (l DefaultLocale) NumberAnyOf() string { 212 return `Must validate at least one schema (anyOf)` 213} 214 215// NumberOneOf returns a format-string for "oneOf" schema validation errors 216func (l DefaultLocale) NumberOneOf() string { 217 return `Must validate one and only one schema (oneOf)` 218} 219 220// NumberAllOf returns a format-string for "allOf" schema validation errors 221func (l DefaultLocale) NumberAllOf() string { 222 return `Must validate all the schemas (allOf)` 223} 224 225// NumberNot returns a format-string to format a NumberNotError 226func (l DefaultLocale) NumberNot() string { 227 return `Must not validate the schema (not)` 228} 229 230// MissingDependency returns a format-string for "missing dependency" schema validation errors 231func (l DefaultLocale) MissingDependency() string { 232 return `Has a dependency on {{.dependency}}` 233} 234 235// Internal returns a format-string for internal errors 236func (l DefaultLocale) Internal() string { 237 return `Internal Error {{.error}}` 238} 239 240// Const returns a format-string to format a ConstError 241func (l DefaultLocale) Const() string { 242 return `{{.field}} does not match: {{.allowed}}` 243} 244 245// Enum returns a format-string to format an EnumError 246func (l DefaultLocale) Enum() string { 247 return `{{.field}} must be one of the following: {{.allowed}}` 248} 249 250// ArrayNoAdditionalItems returns a format-string to format an ArrayNoAdditionalItemsError 251func (l DefaultLocale) ArrayNoAdditionalItems() string { 252 return `No additional items allowed on array` 253} 254 255// ArrayNotEnoughItems returns a format-string to format an error for arrays having not enough items to match positional list of schema 256func (l DefaultLocale) ArrayNotEnoughItems() string { 257 return `Not enough items on array to match positional list of schema` 258} 259 260// ArrayMinItems returns a format-string to format an ArrayMinItemsError 261func (l DefaultLocale) ArrayMinItems() string { 262 return `Array must have at least {{.min}} items` 263} 264 265// ArrayMaxItems returns a format-string to format an ArrayMaxItemsError 266func (l DefaultLocale) ArrayMaxItems() string { 267 return `Array must have at most {{.max}} items` 268} 269 270// Unique returns a format-string to format an ItemsMustBeUniqueError 271func (l DefaultLocale) Unique() string { 272 return `{{.type}} items[{{.i}},{{.j}}] must be unique` 273} 274 275// ArrayContains returns a format-string to format an ArrayContainsError 276func (l DefaultLocale) ArrayContains() string { 277 return `At least one of the items must match` 278} 279 280// ArrayMinProperties returns a format-string to format an ArrayMinPropertiesError 281func (l DefaultLocale) ArrayMinProperties() string { 282 return `Must have at least {{.min}} properties` 283} 284 285// ArrayMaxProperties returns a format-string to format an ArrayMaxPropertiesError 286func (l DefaultLocale) ArrayMaxProperties() string { 287 return `Must have at most {{.max}} properties` 288} 289 290// AdditionalPropertyNotAllowed returns a format-string to format an AdditionalPropertyNotAllowedError 291func (l DefaultLocale) AdditionalPropertyNotAllowed() string { 292 return `Additional property {{.property}} is not allowed` 293} 294 295// InvalidPropertyPattern returns a format-string to format an InvalidPropertyPatternError 296func (l DefaultLocale) InvalidPropertyPattern() string { 297 return `Property "{{.property}}" does not match pattern {{.pattern}}` 298} 299 300// InvalidPropertyName returns a format-string to format an InvalidPropertyNameError 301func (l DefaultLocale) InvalidPropertyName() string { 302 return `Property name of "{{.property}}" does not match` 303} 304 305// StringGTE returns a format-string to format an StringLengthGTEError 306func (l DefaultLocale) StringGTE() string { 307 return `String length must be greater than or equal to {{.min}}` 308} 309 310// StringLTE returns a format-string to format an StringLengthLTEError 311func (l DefaultLocale) StringLTE() string { 312 return `String length must be less than or equal to {{.max}}` 313} 314 315// DoesNotMatchPattern returns a format-string to format an DoesNotMatchPatternError 316func (l DefaultLocale) DoesNotMatchPattern() string { 317 return `Does not match pattern '{{.pattern}}'` 318} 319 320// DoesNotMatchFormat returns a format-string to format an DoesNotMatchFormatError 321func (l DefaultLocale) DoesNotMatchFormat() string { 322 return `Does not match format '{{.format}}'` 323} 324 325// MultipleOf returns a format-string to format an MultipleOfError 326func (l DefaultLocale) MultipleOf() string { 327 return `Must be a multiple of {{.multiple}}` 328} 329 330// NumberGTE returns the format string to format a NumberGTEError 331func (l DefaultLocale) NumberGTE() string { 332 return `Must be greater than or equal to {{.min}}` 333} 334 335// NumberGT returns the format string to format a NumberGTError 336func (l DefaultLocale) NumberGT() string { 337 return `Must be greater than {{.min}}` 338} 339 340// NumberLTE returns the format string to format a NumberLTEError 341func (l DefaultLocale) NumberLTE() string { 342 return `Must be less than or equal to {{.max}}` 343} 344 345// NumberLT returns the format string to format a NumberLTError 346func (l DefaultLocale) NumberLT() string { 347 return `Must be less than {{.max}}` 348} 349 350// Schema validators 351 352// RegexPattern returns a format-string to format a regex-pattern error 353func (l DefaultLocale) RegexPattern() string { 354 return `Invalid regex pattern '{{.pattern}}'` 355} 356 357// GreaterThanZero returns a format-string to format an error where a number must be greater than zero 358func (l DefaultLocale) GreaterThanZero() string { 359 return `{{.number}} must be strictly greater than 0` 360} 361 362// MustBeOfA returns a format-string to format an error where a value is of the wrong type 363func (l DefaultLocale) MustBeOfA() string { 364 return `{{.x}} must be of a {{.y}}` 365} 366 367// MustBeOfAn returns a format-string to format an error where a value is of the wrong type 368func (l DefaultLocale) MustBeOfAn() string { 369 return `{{.x}} must be of an {{.y}}` 370} 371 372// CannotBeUsedWithout returns a format-string to format a "cannot be used without" error 373func (l DefaultLocale) CannotBeUsedWithout() string { 374 return `{{.x}} cannot be used without {{.y}}` 375} 376 377// CannotBeGT returns a format-string to format an error where a value are greater than allowed 378func (l DefaultLocale) CannotBeGT() string { 379 return `{{.x}} cannot be greater than {{.y}}` 380} 381 382// MustBeOfType returns a format-string to format an error where a value does not match the required type 383func (l DefaultLocale) MustBeOfType() string { 384 return `{{.key}} must be of type {{.type}}` 385} 386 387// MustBeValidRegex returns a format-string to format an error where a regex is invalid 388func (l DefaultLocale) MustBeValidRegex() string { 389 return `{{.key}} must be a valid regex` 390} 391 392// MustBeValidFormat returns a format-string to format an error where a value does not match the expected format 393func (l DefaultLocale) MustBeValidFormat() string { 394 return `{{.key}} must be a valid format {{.given}}` 395} 396 397// MustBeGTEZero returns a format-string to format an error where a value must be greater or equal than 0 398func (l DefaultLocale) MustBeGTEZero() string { 399 return `{{.key}} must be greater than or equal to 0` 400} 401 402// KeyCannotBeGreaterThan returns a format-string to format an error where a value is greater than the maximum allowed 403func (l DefaultLocale) KeyCannotBeGreaterThan() string { 404 return `{{.key}} cannot be greater than {{.y}}` 405} 406 407// KeyItemsMustBeOfType returns a format-string to format an error where a key is of the wrong type 408func (l DefaultLocale) KeyItemsMustBeOfType() string { 409 return `{{.key}} items must be {{.type}}` 410} 411 412// KeyItemsMustBeUnique returns a format-string to format an error where keys are not unique 413func (l DefaultLocale) KeyItemsMustBeUnique() string { 414 return `{{.key}} items must be unique` 415} 416 417// ReferenceMustBeCanonical returns a format-string to format a "reference must be canonical" error 418func (l DefaultLocale) ReferenceMustBeCanonical() string { 419 return `Reference {{.reference}} must be canonical` 420} 421 422// NotAValidType returns a format-string to format an invalid type error 423func (l DefaultLocale) NotAValidType() string { 424 return `has a primitive type that is NOT VALID -- given: {{.given}} Expected valid values are:{{.expected}}` 425} 426 427// Duplicated returns a format-string to format an error where types are duplicated 428func (l DefaultLocale) Duplicated() string { 429 return `{{.type}} type is duplicated` 430} 431 432// HttpBadStatus returns a format-string for errors when loading a schema using HTTP 433func (l DefaultLocale) HttpBadStatus() string { 434 return `Could not read schema from HTTP, response status is {{.status}}` 435} 436 437// ErrorFormat returns a format string for errors 438// Replacement options: field, description, context, value 439func (l DefaultLocale) ErrorFormat() string { 440 return `{{.field}}: {{.description}}` 441} 442 443// ParseError returns a format-string for JSON parsing errors 444func (l DefaultLocale) ParseError() string { 445 return `Expected: {{.expected}}, given: Invalid JSON` 446} 447 448// ConditionThen returns a format-string for ConditionThenError errors 449// If/Else 450func (l DefaultLocale) ConditionThen() string { 451 return `Must validate "then" as "if" was valid` 452} 453 454// ConditionElse returns a format-string for ConditionElseError errors 455func (l DefaultLocale) ConditionElse() string { 456 return `Must validate "else" as "if" was not valid` 457} 458 459// constants 460const ( 461 STRING_NUMBER = "number" 462 STRING_ARRAY_OF_STRINGS = "array of strings" 463 STRING_ARRAY_OF_SCHEMAS = "array of schemas" 464 STRING_SCHEMA = "valid schema" 465 STRING_SCHEMA_OR_ARRAY_OF_STRINGS = "schema or array of strings" 466 STRING_PROPERTIES = "properties" 467 STRING_DEPENDENCY = "dependency" 468 STRING_PROPERTY = "property" 469 STRING_UNDEFINED = "undefined" 470 STRING_CONTEXT_ROOT = "(root)" 471 STRING_ROOT_SCHEMA_PROPERTY = "(root)" 472) 473