> For the complete documentation index, see [llms.txt](https://racoon-js.gitbook.io/zh/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/zh/jie-kou-shuo-ming/object/custom.md).

# custom

custom(callback, \[ctx])

## 作用

通过函数`callback`声明自定义限制器。

## 参数

* `callback` ***(function)*** - 用于自定义限制器的函数。*`callback`有一个参数：被检测值***。**&#x5982;果检测不通过，则需要抛出错误，否则，表示检测通过。
* `ctx` ***(\*)*** *callback* 执行的上下文`this`.

> 说明
>
> 从1.3.0版本开始，若检测不通过，`callback`也可以返回非空字符串，字符串你内容代表不通过原因。否则，返回其它值将表示检测通过。

## 示例

```javascript
const schema = racoon.object().custom((val) => {
  if (Object.keys(val) === 1) {
    return true;
  }
  throw new Error('被检测对象属性数量必须有且仅有一个');
});
schema.validate({ a: 1 }); // pass
schema.validate({ a: 1, b: 2 }); // fail
```
