Clear ORM
Search…
Validations
Validation offers simple way to disable the updating and creation of invalid models to your database.

Presence validator

The presence validation ensure than a column is not NULL inside the database. While we encourage to use the NOT NULL feature of PostgreSQL, the presence validator is automatically setup via the typing of the column:
    column x : String is assumed to be present
    column x : String can be nilable
    column x : String, presence: false will assume the value cannot be NULL in the database, while not performing any validation check. This is great for values which have default database set value (ex: serial, timestamp...)

Custom validation

To create a custom validator, just override the validate method:
1
class Article
2
include Clear::Model
3
4
column name : String
5
column description : String
6
7
def validate
8
if description_column.present?
9
if description.size < 100
10
add_error("description", "must contains at least 100 characters")
11
end
12
end
13
end
14
end
Copied!
Ensure to check presence of your column while validation. In case of semi-fetching where the column would not have been fetched from the database or not setup, the validator will raise an exception otherwise.

Helpers methods

To simplify the writing of validation code, you may want to use on_presence(field, &block) or ensure_than(field, message, &block) built-in helpers:
1
class Article
2
include Clear::Model
3
4
column name : String
5
column description : String
6
7
def validate
8
ensure_than :description, "must contains at least 100 characters", &.size.<(100)
9
end
10
end
Copied!
The code above will perform exactly like the previous one, while keeping a more compact syntax.

Error object

Whenever a validation check is failing, an error is created and stored in the model. Error is simply a structure with two fields: column : String? and reason : String
The list of error can be accessed through errors method:
1
a = Article.new
2
a.content = "Lorem ipsum"
3
4
unless a.valid?
5
a.errors.each do |err|
6
puts "Error on column: #{err.column} => #{err.reason}"
7
end
8
end
Copied!
Last modified 1yr ago