> For the complete documentation index, see [llms.txt](https://vue-generators.gitbook.io/vue-generators/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://vue-generators.gitbook.io/vue-generators/validation/custom-validators.md).

# Custom Validators

Custom validators are just functions which accept the following parameters:

| parameter | type                | meaning                                         |
| --------- | ------------------- | ----------------------------------------------- |
| `value`   | String              | The current value of the field being validated. |
| `field`   | schema.field object | The schema for the field being validated.       |
| `model`   | model object        | The relevant model.                             |

> Note: If you don't need all the parameters, your function doesn't have to use them.

## Synchronous Validators

This is a simple example of a synchronous US Zip Code validator function, that just takes the current value:

```javascript
zipcode(value) {
  let re = /(^\d{5}$)|(^\d{5}-\d{4}$)/;

  if (!re.test(value)) {
    return ["Invalid Zip Code."];
  } else {
    return []
  }
};
```

> Note: There's a [built-in regular expression validator](/vue-generators/validation/built-in-validators.md#regexp) that you could also use for validating zip codes.

## Asynchronous Validators

Asynchronous validators are also supported. This allows you to perform validations that might take a long time: a complex calculation, or validation via AJAX, for example.

These are very similar to synchronous validators, but they should return a promise, which resolves to an array of errors:

```javascript
let customAsyncValidator = function(value) {
  return new Promise((resolve, reject) => {
    setTimeout(() => {
      if (value)
      resolve();
      else
      resolve([ "Invalid value from async validator" ]);
    }, 1000);
  });
};
```


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## 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, and the optional `goal` query parameter:

```
GET https://vue-generators.gitbook.io/vue-generators/validation/custom-validators.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

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.
