not : Schema
not
SchemaAn instance is valid against this keyword if it fails to validate successfully against the schema defined by this keyword.
Value | This keyword must be set to a valid JSON Schema |
---|---|
Kind | Applicator |
Applies To | Any |
Base Dialect | 2020-12 |
Changed In | None |
Introduced In | Draft 4 |
Vocabulary | Applicator |
Specification | https://json-schema.org/draft/2020-12/json-schema-core.html#section-10.2.1.4 |
Metaschema | https://json-schema.org/draft/2020-12/meta/applicator |
Official Tests | draft2020-12/not.json |
Default |
false
|
Annotation | None |
Affected By | None |
Affects | None |
Also See |
|
The not keyword restricts instances to fail validation against the given subschema. This keyword represents a logical negation (NOT) operation. In other words, the instance successfully validates against the schema only if it does not match the given subschema.
Digging Deeper
After evaluating this keyword, any annotation emitted by its subschema is discarded, independently of whether the subschema was successful or not, as annotations are always discarded on failure. While this might seem counter-intuitive, consider the following cases:
- If the subschema successfully validates against the instance, then the negation keyword itself fails and annotations are discarded
- If the subschema fails to validate against the instance, then annotations are discarded before bubbling up to the outer negation keyword
Best Practice
Avoid the use of this keyword (usually negating the
required
keyword) to prohibit
specific object properties from being defined. Instead, use the
properties
keyword and set the disallowed object
properties to the false
boolean schema.
This keyword is equivalent to the !
operator found in most programming
languages. For example:
bool valid = !not_schema;
Examples
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"not": {
"title": "I will never be emitted as an annotation",
"const": "Prohibited"
}
}
"Hello World"
"Prohibited"
{
"$schema": "https://json-schema.org/draft/2020-12/schema",
"not": {
"type": "string",
"minLength": 10,
"maxLength": 9
}
}
"Hello World"