github.com/kaptinlin/jsonschema@v0.4.6/not.go (about)

     1  package jsonschema
     2  
     3  // EvaluateNot checks if the data fails to conform to the schema or boolean specified in the not attribute.
     4  // According to the JSON Schema Draft 2020-12:
     5  //   - The "not" keyword's value must be either a boolean or a valid JSON Schema.
     6  //   - If "not" is a schema, an instance is valid against this keyword if it fails to validate successfully against the schema.
     7  //   - If "not" is a boolean, the boolean value dictates the negation directly (true forbids validation, false allows any data).
     8  //
     9  // This function ensures that the data instance does not meet the constraints defined by the schema or respects the boolean in the not attribute.
    10  // If the instance fails to conform to the schema or the boolean logic dictates failure, it returns a EvaluationError.
    11  //
    12  // Reference: https://json-schema.org/draft/2020-12/json-schema-core#name-not
    13  func evaluateNot(schema *Schema, instance interface{}, _ map[string]bool, _ map[int]bool, dynamicScope *DynamicScope) (*EvaluationResult, *EvaluationError) {
    14  	if schema.Not == nil {
    15  		return nil, nil // No 'not' constraints to validate against
    16  	}
    17  
    18  	result, _, _ := schema.Not.evaluate(instance, dynamicScope)
    19  
    20  	if result != nil {
    21  		//nolint:errcheck
    22  		result.SetEvaluationPath("/oneOf").
    23  			SetSchemaLocation(schema.GetSchemaLocation("/oneOf")).
    24  			SetInstanceLocation("")
    25  
    26  		if result.IsValid() {
    27  			return result, NewEvaluationError("not", "not_schema_mismatch", "Value should not match the not schema")
    28  		}
    29  	}
    30  
    31  	return result, nil
    32  }