2017-04-17 18:08:24 -04: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 Extends Schema and subSchema, implements the validation phase.
|
|
|
|
//
|
|
|
|
// created 28-02-2013
|
|
|
|
|
|
|
|
package gojsonschema
|
|
|
|
|
|
|
|
import (
|
|
|
|
"encoding/json"
|
2019-04-12 20:56:09 -04:00
|
|
|
"math/big"
|
2017-04-17 18:08:24 -04:00
|
|
|
"reflect"
|
|
|
|
"regexp"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
"unicode/utf8"
|
|
|
|
)
|
|
|
|
|
|
|
|
func Validate(ls JSONLoader, ld JSONLoader) (*Result, error) {
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
// load schema
|
|
|
|
|
|
|
|
schema, err := NewSchema(ls)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
// begine validation
|
|
|
|
|
|
|
|
return schema.Validate(ld)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Schema) Validate(l JSONLoader) (*Result, error) {
|
|
|
|
|
|
|
|
// load document
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
root, err := l.LoadJSON()
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
return v.validateDocument(root), nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (v *Schema) validateDocument(root interface{}) *Result {
|
2017-04-17 18:08:24 -04:00
|
|
|
// begin validation
|
|
|
|
|
|
|
|
result := &Result{}
|
2019-04-12 20:56:09 -04:00
|
|
|
context := NewJsonContext(STRING_CONTEXT_ROOT, nil)
|
2017-04-17 18:08:24 -04:00
|
|
|
v.rootSchema.validateRecursive(v.rootSchema, root, result, context)
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
return result
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) subValidateWithContext(document interface{}, context *JsonContext) *Result {
|
2017-04-17 18:08:24 -04:00
|
|
|
result := &Result{}
|
|
|
|
v.validateRecursive(v, document, result, context)
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
// Walker function to validate the json recursively against the subSchema
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validateRecursive(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *JsonContext) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validateRecursive %s", context.String())
|
|
|
|
internalLog(" %v", currentNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle referenced schemas, returns directly when a $ref is found
|
|
|
|
if currentSubSchema.refSchema != nil {
|
|
|
|
v.validateRecursive(currentSubSchema.refSchema, currentNode, result, context)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check for null value
|
|
|
|
if currentNode == nil {
|
|
|
|
if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_NULL) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(InvalidTypeError),
|
|
|
|
context,
|
|
|
|
currentNode,
|
|
|
|
ErrorDetails{
|
|
|
|
"expected": currentSubSchema.types.String(),
|
|
|
|
"given": TYPE_NULL,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSubSchema.validateSchema(currentSubSchema, currentNode, result, context)
|
|
|
|
v.validateCommon(currentSubSchema, currentNode, result, context)
|
|
|
|
|
|
|
|
} else { // Not a null value
|
|
|
|
|
|
|
|
if isJsonNumber(currentNode) {
|
|
|
|
|
|
|
|
value := currentNode.(json.Number)
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
isInt := checkJsonInteger(value)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
validType := currentSubSchema.types.Contains(TYPE_NUMBER) || (isInt && currentSubSchema.types.Contains(TYPE_INTEGER))
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if currentSubSchema.types.IsTyped() && !validType {
|
|
|
|
|
|
|
|
givenType := TYPE_INTEGER
|
2019-04-12 20:56:09 -04:00
|
|
|
if !isInt {
|
2017-04-17 18:08:24 -04:00
|
|
|
givenType = TYPE_NUMBER
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(InvalidTypeError),
|
|
|
|
context,
|
|
|
|
currentNode,
|
|
|
|
ErrorDetails{
|
|
|
|
"expected": currentSubSchema.types.String(),
|
|
|
|
"given": givenType,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSubSchema.validateSchema(currentSubSchema, value, result, context)
|
|
|
|
v.validateNumber(currentSubSchema, value, result, context)
|
|
|
|
v.validateCommon(currentSubSchema, value, result, context)
|
|
|
|
v.validateString(currentSubSchema, value, result, context)
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
rValue := reflect.ValueOf(currentNode)
|
|
|
|
rKind := rValue.Kind()
|
|
|
|
|
|
|
|
switch rKind {
|
|
|
|
|
|
|
|
// Slice => JSON array
|
|
|
|
|
|
|
|
case reflect.Slice:
|
|
|
|
|
|
|
|
if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_ARRAY) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(InvalidTypeError),
|
|
|
|
context,
|
|
|
|
currentNode,
|
|
|
|
ErrorDetails{
|
|
|
|
"expected": currentSubSchema.types.String(),
|
|
|
|
"given": TYPE_ARRAY,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
castCurrentNode := currentNode.([]interface{})
|
|
|
|
|
|
|
|
currentSubSchema.validateSchema(currentSubSchema, castCurrentNode, result, context)
|
|
|
|
|
|
|
|
v.validateArray(currentSubSchema, castCurrentNode, result, context)
|
|
|
|
v.validateCommon(currentSubSchema, castCurrentNode, result, context)
|
|
|
|
|
|
|
|
// Map => JSON object
|
|
|
|
|
|
|
|
case reflect.Map:
|
|
|
|
if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_OBJECT) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(InvalidTypeError),
|
|
|
|
context,
|
|
|
|
currentNode,
|
|
|
|
ErrorDetails{
|
|
|
|
"expected": currentSubSchema.types.String(),
|
|
|
|
"given": TYPE_OBJECT,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
castCurrentNode, ok := currentNode.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
castCurrentNode = convertDocumentNode(currentNode).(map[string]interface{})
|
|
|
|
}
|
|
|
|
|
|
|
|
currentSubSchema.validateSchema(currentSubSchema, castCurrentNode, result, context)
|
|
|
|
|
|
|
|
v.validateObject(currentSubSchema, castCurrentNode, result, context)
|
|
|
|
v.validateCommon(currentSubSchema, castCurrentNode, result, context)
|
|
|
|
|
|
|
|
for _, pSchema := range currentSubSchema.propertiesChildren {
|
|
|
|
nextNode, ok := castCurrentNode[pSchema.property]
|
|
|
|
if ok {
|
2019-04-12 20:56:09 -04:00
|
|
|
subContext := NewJsonContext(pSchema.property, context)
|
2017-04-17 18:08:24 -04:00
|
|
|
v.validateRecursive(pSchema, nextNode, result, subContext)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Simple JSON values : string, number, boolean
|
|
|
|
|
|
|
|
case reflect.Bool:
|
|
|
|
|
|
|
|
if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_BOOLEAN) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(InvalidTypeError),
|
|
|
|
context,
|
|
|
|
currentNode,
|
|
|
|
ErrorDetails{
|
|
|
|
"expected": currentSubSchema.types.String(),
|
|
|
|
"given": TYPE_BOOLEAN,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
value := currentNode.(bool)
|
|
|
|
|
|
|
|
currentSubSchema.validateSchema(currentSubSchema, value, result, context)
|
|
|
|
v.validateNumber(currentSubSchema, value, result, context)
|
|
|
|
v.validateCommon(currentSubSchema, value, result, context)
|
|
|
|
v.validateString(currentSubSchema, value, result, context)
|
|
|
|
|
|
|
|
case reflect.String:
|
|
|
|
|
|
|
|
if currentSubSchema.types.IsTyped() && !currentSubSchema.types.Contains(TYPE_STRING) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(InvalidTypeError),
|
|
|
|
context,
|
|
|
|
currentNode,
|
|
|
|
ErrorDetails{
|
|
|
|
"expected": currentSubSchema.types.String(),
|
|
|
|
"given": TYPE_STRING,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
value := currentNode.(string)
|
|
|
|
|
|
|
|
currentSubSchema.validateSchema(currentSubSchema, value, result, context)
|
|
|
|
v.validateNumber(currentSubSchema, value, result, context)
|
|
|
|
v.validateCommon(currentSubSchema, value, result, context)
|
|
|
|
v.validateString(currentSubSchema, value, result, context)
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
result.incrementScore()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Different kinds of validation there, subSchema / common / array / object / string...
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validateSchema(currentSubSchema *subSchema, currentNode interface{}, result *Result, context *JsonContext) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validateSchema %s", context.String())
|
|
|
|
internalLog(" %v", currentNode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(currentSubSchema.anyOf) > 0 {
|
|
|
|
|
|
|
|
validatedAnyOf := false
|
|
|
|
var bestValidationResult *Result
|
|
|
|
|
|
|
|
for _, anyOfSchema := range currentSubSchema.anyOf {
|
|
|
|
if !validatedAnyOf {
|
|
|
|
validationResult := anyOfSchema.subValidateWithContext(currentNode, context)
|
|
|
|
validatedAnyOf = validationResult.Valid()
|
|
|
|
|
|
|
|
if !validatedAnyOf && (bestValidationResult == nil || validationResult.score > bestValidationResult.score) {
|
|
|
|
bestValidationResult = validationResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !validatedAnyOf {
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(new(NumberAnyOfError), context, currentNode, ErrorDetails{})
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if bestValidationResult != nil {
|
|
|
|
// add error messages of closest matching subSchema as
|
|
|
|
// that's probably the one the user was trying to match
|
|
|
|
result.mergeErrors(bestValidationResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(currentSubSchema.oneOf) > 0 {
|
|
|
|
|
|
|
|
nbValidated := 0
|
|
|
|
var bestValidationResult *Result
|
|
|
|
|
|
|
|
for _, oneOfSchema := range currentSubSchema.oneOf {
|
|
|
|
validationResult := oneOfSchema.subValidateWithContext(currentNode, context)
|
|
|
|
if validationResult.Valid() {
|
|
|
|
nbValidated++
|
|
|
|
} else if nbValidated == 0 && (bestValidationResult == nil || validationResult.score > bestValidationResult.score) {
|
|
|
|
bestValidationResult = validationResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if nbValidated != 1 {
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(new(NumberOneOfError), context, currentNode, ErrorDetails{})
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if nbValidated == 0 {
|
|
|
|
// add error messages of closest matching subSchema as
|
|
|
|
// that's probably the one the user was trying to match
|
|
|
|
result.mergeErrors(bestValidationResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(currentSubSchema.allOf) > 0 {
|
|
|
|
nbValidated := 0
|
|
|
|
|
|
|
|
for _, allOfSchema := range currentSubSchema.allOf {
|
|
|
|
validationResult := allOfSchema.subValidateWithContext(currentNode, context)
|
|
|
|
if validationResult.Valid() {
|
|
|
|
nbValidated++
|
|
|
|
}
|
|
|
|
result.mergeErrors(validationResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nbValidated != len(currentSubSchema.allOf) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(new(NumberAllOfError), context, currentNode, ErrorDetails{})
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentSubSchema.not != nil {
|
|
|
|
validationResult := currentSubSchema.not.subValidateWithContext(currentNode, context)
|
|
|
|
if validationResult.Valid() {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(new(NumberNotError), context, currentNode, ErrorDetails{})
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if currentSubSchema.dependencies != nil && len(currentSubSchema.dependencies) > 0 {
|
|
|
|
if isKind(currentNode, reflect.Map) {
|
|
|
|
for elementKey := range currentNode.(map[string]interface{}) {
|
|
|
|
if dependency, ok := currentSubSchema.dependencies[elementKey]; ok {
|
|
|
|
switch dependency := dependency.(type) {
|
|
|
|
|
|
|
|
case []string:
|
|
|
|
for _, dependOnKey := range dependency {
|
|
|
|
if _, dependencyResolved := currentNode.(map[string]interface{})[dependOnKey]; !dependencyResolved {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(MissingDependencyError),
|
|
|
|
context,
|
|
|
|
currentNode,
|
|
|
|
ErrorDetails{"dependency": dependOnKey},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *subSchema:
|
|
|
|
dependency.validateRecursive(dependency, currentNode, result, context)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
if currentSubSchema._if != nil {
|
|
|
|
validationResultIf := currentSubSchema._if.subValidateWithContext(currentNode, context)
|
|
|
|
if currentSubSchema._then != nil && validationResultIf.Valid() {
|
|
|
|
validationResultThen := currentSubSchema._then.subValidateWithContext(currentNode, context)
|
|
|
|
if !validationResultThen.Valid() {
|
|
|
|
result.addInternalError(new(ConditionThenError), context, currentNode, ErrorDetails{})
|
|
|
|
result.mergeErrors(validationResultThen)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentSubSchema._else != nil && !validationResultIf.Valid() {
|
|
|
|
validationResultElse := currentSubSchema._else.subValidateWithContext(currentNode, context)
|
|
|
|
if !validationResultElse.Valid() {
|
|
|
|
result.addInternalError(new(ConditionElseError), context, currentNode, ErrorDetails{})
|
|
|
|
result.mergeErrors(validationResultElse)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
result.incrementScore()
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validateCommon(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validateCommon %s", context.String())
|
|
|
|
internalLog(" %v", value)
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// const:
|
|
|
|
if currentSubSchema._const != nil {
|
|
|
|
vString, err := marshalWithoutNumber(value)
|
|
|
|
if err != nil {
|
|
|
|
result.addInternalError(new(InternalError), context, value, ErrorDetails{"error": err})
|
|
|
|
}
|
|
|
|
if *vString != *currentSubSchema._const {
|
|
|
|
result.addInternalError(new(ConstError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{
|
|
|
|
"allowed": *currentSubSchema._const,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
// enum:
|
|
|
|
if len(currentSubSchema.enum) > 0 {
|
|
|
|
has, err := currentSubSchema.ContainsEnum(value)
|
|
|
|
if err != nil {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(new(InternalError), context, value, ErrorDetails{"error": err})
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
if !has {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(EnumError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{
|
|
|
|
"allowed": strings.Join(currentSubSchema.enum, ", "),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.incrementScore()
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validateArray(currentSubSchema *subSchema, value []interface{}, result *Result, context *JsonContext) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validateArray %s", context.String())
|
|
|
|
internalLog(" %v", value)
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
nbValues := len(value)
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// TODO explain
|
|
|
|
if currentSubSchema.itemsChildrenIsSingleSchema {
|
|
|
|
for i := range value {
|
2019-04-12 20:56:09 -04:00
|
|
|
subContext := NewJsonContext(strconv.Itoa(i), context)
|
2017-04-17 18:08:24 -04:00
|
|
|
validationResult := currentSubSchema.itemsChildren[0].subValidateWithContext(value[i], subContext)
|
|
|
|
result.mergeErrors(validationResult)
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if currentSubSchema.itemsChildren != nil && len(currentSubSchema.itemsChildren) > 0 {
|
|
|
|
|
|
|
|
nbItems := len(currentSubSchema.itemsChildren)
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// while we have both schemas and values, check them against each other
|
|
|
|
for i := 0; i != nbItems && i != nbValues; i++ {
|
|
|
|
subContext := NewJsonContext(strconv.Itoa(i), context)
|
|
|
|
validationResult := currentSubSchema.itemsChildren[i].subValidateWithContext(value[i], subContext)
|
|
|
|
result.mergeErrors(validationResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
if nbItems < nbValues {
|
|
|
|
// we have less schemas than elements in the instance array,
|
|
|
|
// but that might be ok if "additionalItems" is specified.
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
switch currentSubSchema.additionalItems.(type) {
|
|
|
|
case bool:
|
|
|
|
if !currentSubSchema.additionalItems.(bool) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(new(ArrayNoAdditionalItemsError), context, value, ErrorDetails{})
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
case *subSchema:
|
|
|
|
additionalItemSchema := currentSubSchema.additionalItems.(*subSchema)
|
|
|
|
for i := nbItems; i != nbValues; i++ {
|
2019-04-12 20:56:09 -04:00
|
|
|
subContext := NewJsonContext(strconv.Itoa(i), context)
|
2017-04-17 18:08:24 -04:00
|
|
|
validationResult := additionalItemSchema.subValidateWithContext(value[i], subContext)
|
|
|
|
result.mergeErrors(validationResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// minItems & maxItems
|
|
|
|
if currentSubSchema.minItems != nil {
|
2019-04-12 20:56:09 -04:00
|
|
|
if nbValues < int(*currentSubSchema.minItems) {
|
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(ArrayMinItemsError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"min": *currentSubSchema.minItems},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentSubSchema.maxItems != nil {
|
2019-04-12 20:56:09 -04:00
|
|
|
if nbValues > int(*currentSubSchema.maxItems) {
|
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(ArrayMaxItemsError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"max": *currentSubSchema.maxItems},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// uniqueItems:
|
|
|
|
if currentSubSchema.uniqueItems {
|
|
|
|
var stringifiedItems []string
|
2019-04-12 20:56:09 -04:00
|
|
|
for j, v := range value {
|
|
|
|
vString, err := marshalWithoutNumber(v)
|
2017-04-17 18:08:24 -04:00
|
|
|
if err != nil {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(new(InternalError), context, value, ErrorDetails{"err": err})
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
2019-04-12 20:56:09 -04:00
|
|
|
if i := indexStringInSlice(stringifiedItems, *vString); i > -1 {
|
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(ItemsMustBeUniqueError),
|
|
|
|
context,
|
|
|
|
value,
|
2019-04-12 20:56:09 -04:00
|
|
|
ErrorDetails{"type": TYPE_ARRAY, "i": i, "j": j},
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
stringifiedItems = append(stringifiedItems, *vString)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// contains:
|
|
|
|
|
|
|
|
if currentSubSchema.contains != nil {
|
|
|
|
validatedOne := false
|
|
|
|
var bestValidationResult *Result
|
|
|
|
|
|
|
|
for i, v := range value {
|
|
|
|
subContext := NewJsonContext(strconv.Itoa(i), context)
|
|
|
|
|
|
|
|
validationResult := currentSubSchema.contains.subValidateWithContext(v, subContext)
|
|
|
|
if validationResult.Valid() {
|
|
|
|
validatedOne = true
|
|
|
|
break
|
|
|
|
} else {
|
|
|
|
if bestValidationResult == nil || validationResult.score > bestValidationResult.score {
|
|
|
|
bestValidationResult = validationResult
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !validatedOne {
|
|
|
|
result.addInternalError(
|
|
|
|
new(ArrayContainsError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{},
|
|
|
|
)
|
|
|
|
if bestValidationResult != nil {
|
|
|
|
result.mergeErrors(bestValidationResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
result.incrementScore()
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validateObject(currentSubSchema *subSchema, value map[string]interface{}, result *Result, context *JsonContext) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validateObject %s", context.String())
|
|
|
|
internalLog(" %v", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
// minProperties & maxProperties:
|
|
|
|
if currentSubSchema.minProperties != nil {
|
|
|
|
if len(value) < int(*currentSubSchema.minProperties) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(ArrayMinPropertiesError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"min": *currentSubSchema.minProperties},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentSubSchema.maxProperties != nil {
|
|
|
|
if len(value) > int(*currentSubSchema.maxProperties) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(ArrayMaxPropertiesError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"max": *currentSubSchema.maxProperties},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// required:
|
|
|
|
for _, requiredProperty := range currentSubSchema.required {
|
|
|
|
_, ok := value[requiredProperty]
|
|
|
|
if ok {
|
|
|
|
result.incrementScore()
|
|
|
|
} else {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(RequiredError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"property": requiredProperty},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// additionalProperty & patternProperty:
|
|
|
|
if currentSubSchema.additionalProperties != nil {
|
|
|
|
|
|
|
|
switch currentSubSchema.additionalProperties.(type) {
|
|
|
|
case bool:
|
|
|
|
|
|
|
|
if !currentSubSchema.additionalProperties.(bool) {
|
|
|
|
|
|
|
|
for pk := range value {
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, spValue := range currentSubSchema.propertiesChildren {
|
|
|
|
if pk == spValue.property {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
|
|
|
|
|
|
|
|
if found {
|
|
|
|
|
|
|
|
if pp_has && !pp_match {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(AdditionalPropertyNotAllowedError),
|
|
|
|
context,
|
2019-04-12 20:56:09 -04:00
|
|
|
value[pk],
|
2017-04-17 18:08:24 -04:00
|
|
|
ErrorDetails{"property": pk},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if !pp_has || !pp_match {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(AdditionalPropertyNotAllowedError),
|
|
|
|
context,
|
2019-04-12 20:56:09 -04:00
|
|
|
value[pk],
|
2017-04-17 18:08:24 -04:00
|
|
|
ErrorDetails{"property": pk},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
case *subSchema:
|
|
|
|
|
|
|
|
additionalPropertiesSchema := currentSubSchema.additionalProperties.(*subSchema)
|
|
|
|
for pk := range value {
|
|
|
|
|
|
|
|
found := false
|
|
|
|
for _, spValue := range currentSubSchema.propertiesChildren {
|
|
|
|
if pk == spValue.property {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
|
|
|
|
|
|
|
|
if found {
|
|
|
|
|
|
|
|
if pp_has && !pp_match {
|
|
|
|
validationResult := additionalPropertiesSchema.subValidateWithContext(value[pk], context)
|
|
|
|
result.mergeErrors(validationResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
} else {
|
|
|
|
|
|
|
|
if !pp_has || !pp_match {
|
|
|
|
validationResult := additionalPropertiesSchema.subValidateWithContext(value[pk], context)
|
|
|
|
result.mergeErrors(validationResult)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
|
|
|
|
for pk := range value {
|
|
|
|
|
|
|
|
pp_has, pp_match := v.validatePatternProperty(currentSubSchema, pk, value[pk], result, context)
|
|
|
|
|
|
|
|
if pp_has && !pp_match {
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(InvalidPropertyPatternError),
|
|
|
|
context,
|
2019-04-12 20:56:09 -04:00
|
|
|
value[pk],
|
2017-04-17 18:08:24 -04:00
|
|
|
ErrorDetails{
|
|
|
|
"property": pk,
|
|
|
|
"pattern": currentSubSchema.PatternPropertiesString(),
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
// propertyNames:
|
|
|
|
if currentSubSchema.propertyNames != nil {
|
|
|
|
for pk := range value {
|
|
|
|
validationResult := currentSubSchema.propertyNames.subValidateWithContext(pk, context)
|
|
|
|
if !validationResult.Valid() {
|
|
|
|
result.addInternalError(new(InvalidPropertyNameError),
|
|
|
|
context,
|
|
|
|
value, ErrorDetails{
|
|
|
|
"property": pk,
|
|
|
|
})
|
|
|
|
result.mergeErrors(validationResult)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-17 18:08:24 -04:00
|
|
|
result.incrementScore()
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validatePatternProperty(currentSubSchema *subSchema, key string, value interface{}, result *Result, context *JsonContext) (has bool, matched bool) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validatePatternProperty %s", context.String())
|
|
|
|
internalLog(" %s %v", key, value)
|
|
|
|
}
|
|
|
|
|
|
|
|
has = false
|
|
|
|
|
|
|
|
validatedkey := false
|
|
|
|
|
|
|
|
for pk, pv := range currentSubSchema.patternProperties {
|
|
|
|
if matches, _ := regexp.MatchString(pk, key); matches {
|
|
|
|
has = true
|
2019-04-12 20:56:09 -04:00
|
|
|
subContext := NewJsonContext(key, context)
|
2017-04-17 18:08:24 -04:00
|
|
|
validationResult := pv.subValidateWithContext(value, subContext)
|
|
|
|
result.mergeErrors(validationResult)
|
2019-04-12 20:56:09 -04:00
|
|
|
validatedkey = true
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !validatedkey {
|
|
|
|
return has, false
|
|
|
|
}
|
|
|
|
|
|
|
|
result.incrementScore()
|
|
|
|
|
|
|
|
return has, true
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validateString(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// Ignore JSON numbers
|
|
|
|
if isJsonNumber(value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore non strings
|
|
|
|
if !isKind(value, reflect.String) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validateString %s", context.String())
|
|
|
|
internalLog(" %v", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
stringValue := value.(string)
|
|
|
|
|
|
|
|
// minLength & maxLength:
|
|
|
|
if currentSubSchema.minLength != nil {
|
|
|
|
if utf8.RuneCount([]byte(stringValue)) < int(*currentSubSchema.minLength) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(StringLengthGTEError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"min": *currentSubSchema.minLength},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentSubSchema.maxLength != nil {
|
|
|
|
if utf8.RuneCount([]byte(stringValue)) > int(*currentSubSchema.maxLength) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(StringLengthLTEError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"max": *currentSubSchema.maxLength},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// pattern:
|
|
|
|
if currentSubSchema.pattern != nil {
|
|
|
|
if !currentSubSchema.pattern.MatchString(stringValue) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(DoesNotMatchPatternError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"pattern": currentSubSchema.pattern},
|
|
|
|
)
|
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// format
|
|
|
|
if currentSubSchema.format != "" {
|
|
|
|
if !FormatCheckers.IsFormat(currentSubSchema.format, stringValue) {
|
2019-04-12 20:56:09 -04:00
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(DoesNotMatchFormatError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"format": currentSubSchema.format},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.incrementScore()
|
|
|
|
}
|
|
|
|
|
2019-04-12 20:56:09 -04:00
|
|
|
func (v *subSchema) validateNumber(currentSubSchema *subSchema, value interface{}, result *Result, context *JsonContext) {
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// Ignore non numbers
|
|
|
|
if !isJsonNumber(value) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if internalLogEnabled {
|
|
|
|
internalLog("validateNumber %s", context.String())
|
|
|
|
internalLog(" %v", value)
|
|
|
|
}
|
|
|
|
|
|
|
|
number := value.(json.Number)
|
2019-04-12 20:56:09 -04:00
|
|
|
float64Value, _ := new(big.Rat).SetString(string(number))
|
2017-04-17 18:08:24 -04:00
|
|
|
|
|
|
|
// multipleOf:
|
|
|
|
if currentSubSchema.multipleOf != nil {
|
2019-04-12 20:56:09 -04:00
|
|
|
if q := new(big.Rat).Quo(float64Value, currentSubSchema.multipleOf); !q.IsInt() {
|
|
|
|
result.addInternalError(
|
2017-04-17 18:08:24 -04:00
|
|
|
new(MultipleOfError),
|
|
|
|
context,
|
|
|
|
resultErrorFormatJsonNumber(number),
|
2019-04-12 20:56:09 -04:00
|
|
|
ErrorDetails{"multiple": new(big.Float).SetRat(currentSubSchema.multipleOf)},
|
2017-04-17 18:08:24 -04:00
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//maximum & exclusiveMaximum:
|
|
|
|
if currentSubSchema.maximum != nil {
|
2019-04-12 20:56:09 -04:00
|
|
|
if float64Value.Cmp(currentSubSchema.maximum) == 1 {
|
|
|
|
result.addInternalError(
|
|
|
|
new(NumberLTEError),
|
|
|
|
context,
|
|
|
|
resultErrorFormatJsonNumber(number),
|
|
|
|
ErrorDetails{
|
|
|
|
"max": currentSubSchema.maximum,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentSubSchema.exclusiveMaximum != nil {
|
|
|
|
if float64Value.Cmp(currentSubSchema.exclusiveMaximum) >= 0 {
|
|
|
|
result.addInternalError(
|
|
|
|
new(NumberLTError),
|
|
|
|
context,
|
|
|
|
resultErrorFormatJsonNumber(number),
|
|
|
|
ErrorDetails{
|
|
|
|
"max": currentSubSchema.exclusiveMaximum,
|
|
|
|
},
|
|
|
|
)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//minimum & exclusiveMinimum:
|
|
|
|
if currentSubSchema.minimum != nil {
|
2019-04-12 20:56:09 -04:00
|
|
|
if float64Value.Cmp(currentSubSchema.minimum) == -1 {
|
|
|
|
result.addInternalError(
|
|
|
|
new(NumberGTEError),
|
|
|
|
context,
|
|
|
|
resultErrorFormatJsonNumber(number),
|
|
|
|
ErrorDetails{
|
|
|
|
"min": currentSubSchema.minimum,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if currentSubSchema.exclusiveMinimum != nil {
|
|
|
|
if float64Value.Cmp(currentSubSchema.exclusiveMinimum) <= 0 {
|
|
|
|
// if float64Value <= *currentSubSchema.minimum {
|
|
|
|
result.addInternalError(
|
|
|
|
new(NumberGTError),
|
|
|
|
context,
|
|
|
|
resultErrorFormatJsonNumber(number),
|
|
|
|
ErrorDetails{
|
|
|
|
"min": currentSubSchema.exclusiveMinimum,
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// format
|
|
|
|
if currentSubSchema.format != "" {
|
|
|
|
if !FormatCheckers.IsFormat(currentSubSchema.format, float64Value) {
|
|
|
|
result.addInternalError(
|
|
|
|
new(DoesNotMatchFormatError),
|
|
|
|
context,
|
|
|
|
value,
|
|
|
|
ErrorDetails{"format": currentSubSchema.format},
|
|
|
|
)
|
2017-04-17 18:08:24 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
result.incrementScore()
|
|
|
|
}
|