Validation in Azu is support by the Schema shards and is already included with every Crystal Application
Self validating Schemas are beneficial, and in my opinion, ideal, for when defining API Requests, Web Forms, JSON. Schema-Validation Takes a different approach and focuses a lot on explicitness, clarity, and precision of validation logic. It is designed to work with any data input, whether it’s a simple hash, an array or a complex object with deeply nested data.
Each validation is encapsulated by a simple, stateless predicate that receives some input and returns either true or false. Those predicates are encapsulated by rules which can be composed together using predicate logic, meaning you can use the familiar logic operators to build up a validation schema.
Usage
includeSchema::Validation
Schema instance methods
valid? -Boolvalidate! -True or RaiseValidationErrorerrors -Errors(T,S)
Validations
You can also perform validations for existing objects without the use of Schemas.
classUser < ModelincludeSchema::Validationproperty email :Stringproperty name :Stringproperty age :Int32property alive :Boolproperty childrens :Array(String)property childrens_ages :Array(Int32) # To use a custom validator. UniqueRecordValidator will be initialized with an `User` instance use UniqueRecordValidator # Use the `custom` class name predicate as follow validate email, match: /\w+@\w+\.\w{2,3}/, message: "Email must be valid!", unique_record: true validate name, size: (1..20) validate age, gte: 18, lte: 25, message: "Must be 24 and 30 years old" validate alive, eq: true # Define your custom predicates predicates dodefsome?(value:String, some) :Bool (!value.nil?&& value !="") &&!some.nil?enddefif?(value:Array(Int32), bool :Bool) :Bool!boolendenddefinitialize(@email, @name, @age, @alive, @childrens, @childrens_ages)endend
Custom Validations
Simply create a class {Name}Validator with the following signature:
classEmailValidator < Schema::Validatorgetter :record, :field, :messagedefinitialize(@record :UserModel) @field = :email @message ="Email must be valid!"enddefvalid?:Array(Schema::Error) [] ofSchema::ErrorendendclassUniqueRecordValidator < Schema::Validatorgetter :record, :field, :messagedefinitialize(@record :UserModel) @field = :email @message ="Record must be unique!"enddefvalid?:Array(Schema::Error) [] ofSchema::Errorendend
Defining Predicates
You can define your custom predicates by simply creating a custom validator or creating methods in the Schema::Predicates module ending with ? and it should return a boolean. For example:
classUser < Modelproperty email :Stringproperty name :Stringproperty age :Int32property alive :Boolproperty childrens :Array(String)property childrens_ages :Array(Int32)... # Uses a `presense` predicate validate password :String, presence: true # Use the `predicates` macro to define predicate methods predicates do # Presence Predicate Definitiondefpresence?(password:String, _other:String) :Bool!value.nil?endenddefinitialize(@email, @name, @age, @alive, @childrens, @childrens_ages)endend
Differences: Custom Validator vs Predicates
The differences between a custom validator and a method predicate are:
Custom Validators
Must be inherited from Schema::Validator abstract
Receives an instance of the object as a record instance var.
Must have a :field and :message defined.
Must implement a def valid? : Array(Schema::Error) method.
Predicates
Assertions of the property value against an expected value.
Predicates are light weight boolean methods.
Predicates methods must be defined as def {predicate}?(property_value, expected_value) : Bool .
Built in Predicates
These are the current available predicates.
gte -GreaterThan or EqualTolte -LessThan or EqualTogt -GreaterThanlt -LessThansize -Sizein-Inclusionregex -RegularExpressioneq -Equal
CONTRIBUTE - Add more predicates to this shards by contributing a Pull Request.
Additional Parameters
message -Error message to displaynilable -Allownil,true or false