Validation rules
Each rule of a field is mapped to the schema of the field, or to a condition: a sentence written in the description
of the field, for what JSON Schema cannot say. A rule the generator does not know keeps the field and adds an info; a
closure rule is ignored with an info. Add your own with a rule mapper
or DescribesRule.
Presence
| Rule | Effect |
|---|---|
required | required |
present | required |
sometimes | not required, even with required |
nullable | adds null to the type |
filled | minLength: 1 on a string, minProperties: 1 on an object |
required_if, required_unless, required_with, required_with_all, required_without, required_without_all, required_if_accepted, required_if_declined, Rule::requiredIf() | not required; condition required if type is company |
present_if, present_unless, present_with, present_with_all | not required; condition |
missing, prohibited | the field is left out |
missing_if, missing_unless, missing_with, missing_with_all, prohibited_if, prohibited_unless, prohibited_if_accepted, prohibited_if_declined, prohibits | condition |
Rule::when() | its rules, applying in some cases only: not required, condition required in some cases |
bail, exclude, exclude_if, exclude_unless, exclude_with, exclude_without | nothing |
Types
| Rule | Schema |
|---|---|
string | {"type": "string"} |
integer, int | {"type": "integer"} |
numeric, Rule::numeric() | {"type": "number"} (integer kept with integer) |
decimal:2, decimal:1,3 | number; condition 2 decimal places |
boolean, bool | {"type": "boolean"} |
array, list | {"type": "array"} |
array:id,name, Rule::array(['id']) | object with these properties, no other key |
json | string with contentMediaType: application/json |
accepted | required, enum of yes, on, 1, true |
declined | required, enum of no, off, 0, false |
accepted_if, declined_if | condition |
Rule::anyOf([...]) | anyOf of the schemas of each set of rules |
Sizes
On a string, a number, a list or an object, depending on the type of the field:
| Rule | String | Number | List | Object |
|---|---|---|---|---|
min:2 | minLength | minimum | minItems | minProperties |
max:5 | maxLength | maximum | maxItems | maxProperties |
between:1,3 | both | both | both | both |
size:4 | both, equal | both, equal | both, equal | both, equal |
gt:3, lt:3 | minLength: 4, maxLength: 2 | exclusiveMinimum, exclusiveMaximum | minItems: 4, maxItems: 2 | minProperties: 4, maxProperties: 2 |
gte:3, lte:3 | minLength, maxLength | minimum, maximum | minItems, maxItems | minProperties, maxProperties |
On a file, the sizes are conditions in KB (at most 2048 KB). Without a type, max:5 is a maxLength.
The rule order does not matter: max:5|string is a maxLength too. gt, gte,
lt and lte against another field are conditions (greater than minimum).
| Rule | Schema |
|---|---|
digits:4, digits_between:2,4 | string with pattern: ^[0-9]{4}$; on an integer, minimum and maximum |
min_digits, max_digits | condition |
multiple_of:5 | multipleOf: 5 |
Formats
| Rule | Schema |
|---|---|
email, email:rfc,dns, Rule::email() | format: email |
url, active_url | format: uri; url:https adds the condition scheme https |
uuid | format: uuid |
ulid | pattern of a ULID |
ipv4, ipv6 | format: ipv4, format: ipv6 |
ip | string; condition an IP address |
mac_address, hex_color, ascii | pattern |
timezone | string; condition |
alpha:ascii, alpha_num:ascii, alpha_dash:ascii | pattern |
alpha, alpha_num, alpha_dash, lowercase, uppercase | string; condition |
regex:/^[a-z]+$/ | pattern: ^[a-z]+$; with flags (/i), a condition |
not_regex | condition |
starts_with, ends_with | pattern; a condition when the field already has one |
doesnt_start_with, doesnt_end_with | condition |
Dates
| Rule | Schema |
|---|---|
date | string; condition a date |
date_format:Y-m-d | format: date |
date_format:Y-m-d\TH:i:sP | format: date-time |
date_format:d/m/Y | string; condition format d/m/Y |
Rule::date()->format(…) | the same as date_format, with its conditions |
after, after_or_equal, before, before_or_equal, date_equals | condition (after today) |
The formats recognized as date and date-time are listed in date_formats of the configuration; add yours there.
Values
| Rule | Schema |
|---|---|
in:a,b, Rule::in([...]) | enum, typed by its values (string, integer…); on a list, the items |
not_in:a, Rule::notIn([...]) | not: {enum: [...]} |
Rule::enum(Status::class) | the values of the enum, with only() and except() |
exists:users,id, Rule::exists() | condition exists in users.id |
unique:users,email | condition unique in users.email |
distinct | uniqueItems on the list |
contains:a,b | condition |
required_array_keys:id,name | object with these keys required |
same, different, in_array | condition |
confirmed, confirmed:repeat | adds the field password_confirmation (or repeat) with the same schema |
Files
A file makes the body multipart/form-data, its media types giving the encoding of the field.
| Rule | Schema |
|---|---|
file | string with format: binary |
image, File::image() | a file of image/jpeg, image/png, image/bmp, image/gif, image/webp (image:allow_svg adds image/svg+xml) |
mimes:jpg,png, mimetypes:…, File::types([...]) | a file of these media types |
extensions:pdf,docx | condition |
dimensions:… | condition |
File::…->min(), ->max() | conditions in KB |
Passwords
| Rule | Schema |
|---|---|
Password::min(10) | string with format: password and minLength: 10 |
->max(64) | maxLength: 64 |
->letters(), ->mixedCase(), ->numbers(), ->symbols(), ->uncompromised() | conditions |
Password::defaults() | the rules of the defaults |