Validator Options
Beyond the per-validator settings (min:, max:, with:, etc.) every validator accepts a small set of shared options that change when the validator runs or how it reports failure.
allow-nil / allow_nil
Skips every validator on the declaration when the attribute's value is undefined (Nil). The validator is silently bypassed; nothing is appended to errors.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Output
1 | |
Both allow-nil and the Rails-spelled allow_nil are accepted.
allow-blank / allow_blank
Skips every validator on the declaration when the attribute is undefined, the empty string, whitespace-only, an empty collection, or False. Use this when an attribute is optional but, when present, must meet the validators' constraints.
1 2 3 4 5 6 7 8 9 10 11 12 | |
strict
Raises X::StrictValidationFailed instead of pushing onto errors. The exception carries model, attribute, and message-text so callers can inspect what failed.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
as
Overrides the value substituted for {attribute} in error message templates. Useful when the column name is internal but the user-facing label needs to be friendlier.
1 2 3 4 5 6 7 8 9 10 11 12 | |
as does not change which key the error is stored under in errors — errors.max_score still works.
case-sensitive (uniqueness only)
Controls whether the uniqueness lookup matches case-sensitively. Defaults to True. On MySQL the case-sensitive branch uses BINARY to bypass the default _ci collation, so the same declaration behaves identically across PostgreSQL, MySQL, and SQLite.
1 2 3 4 5 6 7 8 9 10 | |
conditions (uniqueness only)
Narrows the uniqueness lookup to records matching an extra WHERE clause. Combines with scope when both are given.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Combine scope and conditions to enforce "unique-per-tenant, only among active rows":
1 2 | |