---
description: custom(callback, [ctx])
---

# custom

## Effect

Declare a custom restrict by a custom `callback` function.

## Arguments

* `callback` _**(Function)**_ - The custom function for restricting. _`callback` has a parameter: the detected value._ If validate failed, the callback should throw an error, otherwise, it means validation passed.
* `[ctx]` _**(\*)**_ - The execution context of `callback`.

> **NOTE**
>
> Since 1.3.0, `callback` can return a non-empty string to respect that validating failed, the string means the failure reason. Otherwise, it means validating passed if you return other values.

## Example

```javascript
const schema = racoon.string().custom((val) => {
  if (val.length % 2 === 0) {
    return true;
  }
  throw new Error('The length of detected string should be even');
});
schema.validate('12'); // pass
schema.validate('123'); // fail
```

