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

# custom

custom(callback, \[ctx])

## Effect

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

## Arguments

* `callback` ***(Function)*** - The custom function for restricting. *`callback` has an argument: 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.any().custom((val) => {
  if (typeof val === 'number' || typeof val === 'string') {
    return true;
  }
  throw new Error('The detected value must be a typeof number or string');
});
schema.validate(1); // pass
schema.validate('abc'); // pass
schema.validate(true); // fail
```
