2017-02-07 21:33:23 +00:00
|
|
|
// Copyright 2015 xeipuuv ( https://github.com/xeipuuv )
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
// author xeipuuv
|
|
|
|
// author-github https://github.com/xeipuuv
|
|
|
|
// author-mail xeipuuv@gmail.com
|
|
|
|
//
|
|
|
|
// repository-name gojsonschema
|
|
|
|
// repository-desc An implementation of JSON Schema, based on IETF's draft v4 - Go language.
|
|
|
|
//
|
|
|
|
// description Contains const string and messages.
|
|
|
|
//
|
|
|
|
// created 01-01-2015
|
|
|
|
|
|
|
|
package gojsonschema
|
|
|
|
|
|
|
|
type (
|
2017-07-03 09:53:31 +00:00
|
|
|
// locale is an interface for defining custom error strings
|
2017-02-07 21:33:23 +00:00
|
|
|
locale interface {
|
|
|
|
Required() string
|
|
|
|
InvalidType() string
|
|
|
|
NumberAnyOf() string
|
|
|
|
NumberOneOf() string
|
|
|
|
NumberAllOf() string
|
|
|
|
NumberNot() string
|
|
|
|
MissingDependency() string
|
|
|
|
Internal() string
|
|
|
|
Enum() string
|
|
|
|
ArrayNotEnoughItems() string
|
|
|
|
ArrayNoAdditionalItems() string
|
|
|
|
ArrayMinItems() string
|
|
|
|
ArrayMaxItems() string
|
|
|
|
Unique() string
|
|
|
|
ArrayMinProperties() string
|
|
|
|
ArrayMaxProperties() string
|
|
|
|
AdditionalPropertyNotAllowed() string
|
|
|
|
InvalidPropertyPattern() string
|
|
|
|
StringGTE() string
|
|
|
|
StringLTE() string
|
|
|
|
DoesNotMatchPattern() string
|
|
|
|
DoesNotMatchFormat() string
|
|
|
|
MultipleOf() string
|
|
|
|
NumberGTE() string
|
|
|
|
NumberGT() string
|
|
|
|
NumberLTE() string
|
|
|
|
NumberLT() string
|
|
|
|
|
|
|
|
// Schema validations
|
|
|
|
RegexPattern() string
|
|
|
|
GreaterThanZero() string
|
|
|
|
MustBeOfA() string
|
|
|
|
MustBeOfAn() string
|
|
|
|
CannotBeUsedWithout() string
|
|
|
|
CannotBeGT() string
|
|
|
|
MustBeOfType() string
|
|
|
|
MustBeValidRegex() string
|
|
|
|
MustBeValidFormat() string
|
|
|
|
MustBeGTEZero() string
|
|
|
|
KeyCannotBeGreaterThan() string
|
|
|
|
KeyItemsMustBeOfType() string
|
|
|
|
KeyItemsMustBeUnique() string
|
|
|
|
ReferenceMustBeCanonical() string
|
|
|
|
NotAValidType() string
|
|
|
|
Duplicated() string
|
2017-07-03 09:53:31 +00:00
|
|
|
HttpBadStatus() string
|
|
|
|
ParseError() string
|
2017-02-07 21:33:23 +00:00
|
|
|
|
|
|
|
// ErrorFormat
|
|
|
|
ErrorFormat() string
|
|
|
|
}
|
|
|
|
|
|
|
|
// DefaultLocale is the default locale for this package
|
|
|
|
DefaultLocale struct{}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (l DefaultLocale) Required() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.property}} is required`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) InvalidType() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Invalid type. Expected: {{.expected}}, given: {{.given}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberAnyOf() string {
|
|
|
|
return `Must validate at least one schema (anyOf)`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberOneOf() string {
|
|
|
|
return `Must validate one and only one schema (oneOf)`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberAllOf() string {
|
|
|
|
return `Must validate all the schemas (allOf)`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberNot() string {
|
|
|
|
return `Must not validate the schema (not)`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MissingDependency() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Has a dependency on {{.dependency}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) Internal() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Internal Error {{.error}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) Enum() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.field}} must be one of the following: {{.allowed}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) ArrayNoAdditionalItems() string {
|
|
|
|
return `No additional items allowed on array`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) ArrayNotEnoughItems() string {
|
|
|
|
return `Not enough items on array to match positional list of schema`
|
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) ArrayMinItems() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Array must have at least {{.min}} items`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) ArrayMaxItems() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Array must have at most {{.max}} items`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) Unique() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.type}} items must be unique`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) ArrayMinProperties() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Must have at least {{.min}} properties`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) ArrayMaxProperties() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Must have at most {{.max}} properties`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) AdditionalPropertyNotAllowed() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Additional property {{.property}} is not allowed`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) InvalidPropertyPattern() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Property "{{.property}}" does not match pattern {{.pattern}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) StringGTE() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `String length must be greater than or equal to {{.min}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) StringLTE() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `String length must be less than or equal to {{.max}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) DoesNotMatchPattern() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Does not match pattern '{{.pattern}}'`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) DoesNotMatchFormat() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Does not match format '{{.format}}'`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MultipleOf() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Must be a multiple of {{.multiple}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberGTE() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Must be greater than or equal to {{.min}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberGT() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Must be greater than {{.min}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberLTE() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Must be less than or equal to {{.max}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NumberLT() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Must be less than {{.max}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Schema validators
|
|
|
|
func (l DefaultLocale) RegexPattern() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Invalid regex pattern '{{.pattern}}'`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) GreaterThanZero() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.number}} must be strictly greater than 0`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MustBeOfA() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.x}} must be of a {{.y}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MustBeOfAn() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.x}} must be of an {{.y}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) CannotBeUsedWithout() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.x}} cannot be used without {{.y}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) CannotBeGT() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.x}} cannot be greater than {{.y}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MustBeOfType() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.key}} must be of type {{.type}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MustBeValidRegex() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.key}} must be a valid regex`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MustBeValidFormat() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.key}} must be a valid format {{.given}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) MustBeGTEZero() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.key}} must be greater than or equal to 0`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) KeyCannotBeGreaterThan() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.key}} cannot be greater than {{.y}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) KeyItemsMustBeOfType() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.key}} items must be {{.type}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) KeyItemsMustBeUnique() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.key}} items must be unique`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) ReferenceMustBeCanonical() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `Reference {{.reference}} must be canonical`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) NotAValidType() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `has a primitive type that is NOT VALID -- given: {{.given}} Expected valid values are:{{.expected}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (l DefaultLocale) Duplicated() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.type}} type is duplicated`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
2017-07-03 09:53:31 +00:00
|
|
|
func (l DefaultLocale) HttpBadStatus() string {
|
|
|
|
return `Could not read schema from HTTP, response status is {{.status}}`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Replacement options: field, description, context, value
|
|
|
|
func (l DefaultLocale) ErrorFormat() string {
|
2017-07-03 09:53:31 +00:00
|
|
|
return `{{.field}}: {{.description}}`
|
|
|
|
}
|
|
|
|
|
|
|
|
//Parse error
|
|
|
|
func (l DefaultLocale) ParseError() string {
|
|
|
|
return `Expected: %expected%, given: Invalid JSON`
|
2017-02-07 21:33:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
STRING_NUMBER = "number"
|
|
|
|
STRING_ARRAY_OF_STRINGS = "array of strings"
|
|
|
|
STRING_ARRAY_OF_SCHEMAS = "array of schemas"
|
|
|
|
STRING_SCHEMA = "schema"
|
|
|
|
STRING_SCHEMA_OR_ARRAY_OF_STRINGS = "schema or array of strings"
|
|
|
|
STRING_PROPERTIES = "properties"
|
|
|
|
STRING_DEPENDENCY = "dependency"
|
|
|
|
STRING_PROPERTY = "property"
|
|
|
|
STRING_UNDEFINED = "undefined"
|
|
|
|
STRING_CONTEXT_ROOT = "(root)"
|
|
|
|
STRING_ROOT_SCHEMA_PROPERTY = "(root)"
|
|
|
|
)
|