class User < Crecto::Model
field :password, String, virtual: true
field :password_confirmation, String, virtual: true
# Validate that fields arent blank before inserting or updating
validate_required :age # or an array [:age, :name]
# Validate the format of fields using a regex
validate_format :name, /^[a-zA-Z]*&/
# Validate the length of a field
validate_length :uuid, is: 36, # min: 36, max: 60
# Validate the existence of the value of the field in an array
validate_inclusion :role, in: ["admin", "user"]
validate_exclusion :age, in: Range(0, 20)
# Generic or custom validation using a proc
validate "Passwords must match", ->(user: User) do
user.password == user.password_confirmation
format: {pattern: /^[a-zA-Z]*&/},
exclusion: {in: ["foo", "bar"]},
length: {min: 2, max: 50}