Negation
Assert::not() turns any rule into its opposite, and the message says what had to be true:
php
Assert::not(Assert::lessThan(Assert::value(), 5))->violations(3);
// value must not be less than 5Composites
Negation is pushed down to the leaves by De Morgan's laws, so the messages stay specific:
| Rule | Means |
|---|---|
not(all(a, b)) | any(not a, not b) |
not(any(a, b)) | all(not a, not b) |
not(each(x, r)) | some(x, not r) |
not(some(x, r)) | each(x, not r) |
not(not(r)) | r |
php
Assert::not(Assert::all(Assert::isString(Assert::value()), Assert::empty(Assert::value())))->violations('');
// value must not be of type string, string given or value must not be emptynot(each(...)) on an empty iterable fails with lines must contain at least one element: there is no element for which the rule fails.
Preconditions are never negated
Some rules can only be checked on certain values: you cannot compare null with 5, measure the length of an int, or match against a broken pattern. These preconditions stay as they are under not():
php
Assert::not(Assert::lessThan(Assert::value(), 5))->violations(null);
// value must be of type int|float|string|DateTimeInterface, null givenWithout this rule, not(lessThan(x, 5)) would accept null, an array or a string for a number field, because none of them is "less than 5". Such violations have the code invalid.