# Validation

{% hint style="success" %}
This is the official documentation of the `@forestadmin/agent` Node.js agent.
{% endhint %}

![A field failing validation](/files/GyCpnHXMsGIq20U33LOz)

Most data sources can import validation rules from their target.\
For instance, if you are using the [SQL data source](/developer-guide-agents-nodejs/data-sources/provided-data-sources/sql.md).

* 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](/developer-guide-agents-nodejs/data-sources/getting-started/queries/filters.md#operators).

```javascript
collection
  .addFieldValidation('firstName', 'Present')
  .addFieldValidation('firstName', 'LongerThan', 2)
  .addFieldValidation('firstName', 'ShorterThan', 13)
  .addFieldValidation('firstName', 'Match', /^[a-z]+$/i);
```

### Custom validators

If you need to implement custom validators or validation over multiple fields you may use [change hooks](/developer-guide-agents-nodejs/agent-customization/hooks.md).

### Make Field Optional

If the introspection marks a field as required, and you would like to make it optional, you can use the `setFieldNullable` function on your collection.

{% hint style="danger" %}
Be wary that if your database system does not allow empty value on the specified field, updating that field on records with an empty value will result in an error.
{% endhint %}

```javascript
collection.setFieldNullable('firstName');
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://docs.forestadmin.com/developer-guide-agents-nodejs/agent-customization/fields/validation.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
