# Validation

{% hint style="success" %}
This is the official documentation of the `agent_ruby` Ruby agent.
{% endhint %}

![A field failing validation](https://561307319-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fn23xpsrpC8uJXljJ3an2%2Fuploads%2Fgit-blob-8668bc8ffc7b3c737c80351447aa58c6eed2f303%2Ffield-validation-error.png?alt=media)

* Columns of type `VARCHAR(15)` will automatically carry a `less than 15 chars` validator.
* Non-nullable columns will automatically carry a `Present` validator.

However, you may want to enforce stricter restrictions than the ones which are implemented in your data source.

### Adding validation rules

The list of operators (`Present`, `LongerThan`, ...) which can be used when adding validators is the same as the [filter operators](https://docs.forestadmin.com/developer-guide-agents-ruby/data-sources/getting-started/queries/filters#operators).

#### Traditional Syntax

Using full context methods with `customize_collection`:

```ruby
include ForestAdmin::Types

@create_agent.customize_collection('customer') do |collection|
  collection.add_field_validation('firstName', Operators::PRESENT)
    .add_field_validation('firstName', Operators::LONGER_THAN, 2)
    .add_field_validation('firstName', Operators::SHORTER_THAN, 13)
    .add_field_validation('firstName', Operators::MATCH, /^[a-z]+$/)
end
```

#### DSL Syntax

Using simplified DSL with `collection`:

```ruby
@create_agent.collection :customer do |collection|
  collection.validates :firstName,
    present: true,
    longer_than: 2,
    shorter_than: 13,
    match: /^[a-z]+$/
end
```
