# Custom validation

A task may enforce **custom validation** for options by including validation logic in its code, inspecting the current value of an option and rendering an [**error object**](/core/tasks/code/error-objects.md) if the option does not meet its criteria.

A modification to a task option will always result in a new [**preview**](/core/tasks/previews.md) being rendered. In this way, a task developer may provide the user with immediate feedback on their task configuration.

## Example

In this example, a task begins by validating an option called "A positive number". The only flags on this option are "required" and "number", meaning that Mechanic's involvement is limited to making sure the user fills in this task option with a number.

Once the option is filled in, the task preview will be rendered. If the user has entered a zero, or a negative number, the [error tag](/platform/liquid/tags/error.md) is used to generate an [error object](/core/tasks/code/error-objects.md). The error message will then be shown to the user, and they will be prevented from saving the task until they provide valid input.

```liquid
{% if options.a_positive_number__required_number <= 0 %}
  {% error "The option 'A positive number' must be greater than zero." %}
{% endif %}

{% action "cache", "set", "a_positive_number_to_remember", options.a_positive_number__required_number %}
```


---

# 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://learn.mechanic.dev/core/tasks/options/custom-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.
