github.com/kaptinlin/jsonschema@v0.4.6/allOf.go (about) 1 package jsonschema 2 3 import ( 4 "fmt" 5 "strconv" 6 "strings" 7 ) 8 9 // EvaluateAllOf checks if the data conforms to all schemas specified in the allOf attribute. 10 // According to the JSON Schema Draft 2020-12: 11 // - The "allOf" keyword's value must be a non-empty array, where each item is a valid JSON Schema or a boolean. 12 // - An instance validates successfully against this keyword if it validates successfully against all schemas or booleans in this array. 13 // 14 // This function ensures that the data instance meets all the specified constraints collectively defined by the schemas or booleans in the allOf array. 15 // If the instance fails to conform to any schema or boolean in the array, it returns a EvaluationError detailing the specific failure. 16 // 17 // Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-allof 18 func evaluateAllOf(schema *Schema, instance interface{}, evaluatedProps map[string]bool, evaluatedItems map[int]bool, dynamicScope *DynamicScope) ([]*EvaluationResult, *EvaluationError) { 19 if len(schema.AllOf) == 0 { 20 return nil, nil // No allOf constraints to validate against. 21 } 22 23 invalid_indexs := []string{} 24 results := []*EvaluationResult{} 25 26 for i, subSchema := range schema.AllOf { 27 if subSchema != nil { 28 skipEval := false 29 if subSchema.Boolean != nil && *subSchema.Boolean { 30 // If the schema is `true`, skip updating evaluated properties and items. 31 skipEval = true 32 } 33 34 result, schemaEvaluatedProps, schemaEvaluatedItems := subSchema.evaluate(instance, dynamicScope) 35 if !skipEval { 36 mergeStringMaps(evaluatedProps, schemaEvaluatedProps) 37 mergeIntMaps(evaluatedItems, schemaEvaluatedItems) 38 } 39 40 if result != nil { 41 results = append(results, result.SetEvaluationPath(fmt.Sprintf("/allOf/%d", i)). 42 SetSchemaLocation(schema.GetSchemaLocation(fmt.Sprintf("/allOf/%d", i))). 43 SetInstanceLocation(""), 44 ) 45 46 if !result.IsValid() { 47 invalid_indexs = append(invalid_indexs, strconv.Itoa(i)) 48 } 49 } 50 } 51 } 52 53 if len(invalid_indexs) == 0 { 54 return results, nil 55 } 56 57 return results, NewEvaluationError("allOf", "all_of_item_mismatch", "Value does not match the allOf schema at index {indexs}", map[string]interface{}{ 58 "indexs": strings.Join(invalid_indexs, ", "), 59 }) 60 }