Validates a value against a collection of constraints, stopping once the first violation is raised.
AVD::Constraint(s) that are to be applied sequentially.
NOTE This constraint does not support a
Array(String) | String | Nil Default:
Hash(String, String)? Default:
Suppose you have an object with a
address property which should meet the following criteria:
- Is not a blank string
- Is at least 10 characters long
- Is in a specific format
- Is geolocalizable using an external API
If you were to apply all of these constraints to the
address property, you may run into some problems.
For example, multiple violations may be added for the same property, or you may perform a useless and heavy
external call to geolocalize the address when it is not in a proper format.
To solve this we can validate these constraints sequentially.
class Location include AVD::Validatable PATTERN = /some_pattern/ def initialize(@address : String); end @[Assert::Sequentially([ @[Assert::NotBlank], @[Assert::Size(10..)], @[Assert::Regex(Location::PATTERN)], @[Assert::CustomGeolocalizationConstraint], ])] getter address : String end
NOTE The annotation approach only supports two levels of nested annotations. Manually wire up the constraint via code if you require more than that.constraints/sequentially.cr
- .new(constraints : Array(AVD::Constraint) | AVD::Constraint, groups : Array(String) | String | Nil = nil, payload : Hash(String, String)? = nil)
#validated_by : AVD::ConstraintValidator.class
AVD::ConstraintValidator.classthat should handle validating