The NotSchema inverts another schema: parsing succeeds when the wrapped schema fails, and fails when it succeeds. It covers the JSON Schema not keyword.
use Chubbyphp\Parsing\Parser;
$p = new Parser();
$schema = $p->not($p->string());
$data = $schema->parse(42); // Returns: 42
$data = $schema->parse('test'); // Throws error (input matches the string schema)- The input is parsed against the wrapped schema
- If the wrapped schema fails, the original input is returned unchanged
- If the wrapped schema succeeds, a
not.matcherror is thrown
not is pure validation: the wrapped schema's coercions and transformations never leak into the output, and its output type is mixed — "anything except X" has no narrower type.
$schema = $p->not($p->const('forbidden'));
$schema->parse('allowed'); // Returns: 'allowed'
$schema->parse('forbidden'); // Throws error$schema = $p->not($p->object([
'deprecatedField' => $p->string(),
]));$schema = $p->record($p->not($p->string()));
$schema->parse(['key1' => 1, 'key2' => true]); // Returns: ['key1' => 1, 'key2' => true]
$schema->parse(['key1' => 'value1']); // Throws errornullinput:not($p->string())acceptsnull, becausenullis not a string. To rejectnullas well, combine with another schema or usenot($p->union([...])).nullable(): as on every schema,nullable()short-circuitsnullbefore validation —not($p->const(null))->nullable()returnsnulleven though the wrapped schema matches it.- Wrapped schemas that cannot fail: a wrapped schema with
catch()ordefault()may always succeed, which makes thenotschema always fail. - Error details: when
notfails, the wrapped schema succeeded, so there are no inner errors to report — the singlenot.matcherror is all there is.
| Code | Description |
|---|---|
not.match |
Input matches the wrapped schema |