not keyword validates an instance by inverting the validation result of its subschema. An instance is valid if it fails to validate against the specified schema.
Syntax
Behavior
An instance is valid againstnot if and only if it fails to validate successfully against the schema defined by this keyword.
- The subschema is evaluated normally
- The validation result is inverted (negated)
- If the subschema passes,
notfails - If the subschema fails,
notpasses - No annotations are produced by
not
Examples
Exclude Specific Values
Reject a particular value:"hello", "world", "anything else"Invalid:
"forbidden"
Exclude Specific Types
Reject instances of a certain type:"hello", 42, true, [], {}Invalid:
null
Exclude Pattern Matches
Reject strings matching a pattern:"production", "development"Invalid:
"test", "testing", "test-env"
Exclude Properties
Reject objects with specific properties:Exclude Number Ranges
Reject numbers in a specific range:5, 9, 21, 100Invalid:
10, 15, 20
Reject Empty Strings
Require non-empty strings:"hello", "x"Invalid:
""
Exclude Array Sizes
Reject arrays with specific lengths:[1], [1, 2, 3]Invalid:
[]
Complex Exclusions
Reject objects matching a complex pattern:Enforce Constraints
Combine with other keywords to enforce rules:Common Use Cases
- Value exclusion: Reject specific disallowed values
- Type restriction: Exclude certain types
- Pattern rejection: Reject strings matching unwanted patterns
- Property prohibition: Disallow specific object properties
- Range exclusion: Reject values in forbidden ranges
- State validation: Prevent invalid state combinations
Notes
notis purely an assertion keyword; it never produces annotations- Boolean schema
falseis equivalent to{ "not": {} }(always fails) - Boolean schema
trueis equivalent to{ "not": false }(always passes) - Use with caution: negation can make schemas harder to understand
- Validation error messages for
notmay be less intuitive - Double negation (
notcontainingnot) is valid but should be avoided for clarity
Best Practices
- Provide clear descriptions when using
notto help schema consumers understand what is being rejected - Consider whether positive assertions might be clearer than negative ones
- Use
notsparingly and only when exclusion logic is truly necessary - Combine with
if/then/elsefor complex conditional validation