# 条件验证

### when 方法

```javascript
when(path, expect, chain)
```

* `path` 参数名
* `expect`  预期值
* `chain:` 验证链

```javascript
const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    age: acr.when('type', 'personal', () => {
        return acr.string().required().min(18);
    }),
};

// 抛出错误，age 必填
await acr.validate({
    type: 'personal',
}, rules);

// 抛出错误，age 不能小于 18
await acr.validate({
    type: 'personal',
    age: 16,
}, rules);

// 通过
await acr.validate({
    type: 'common',
    age: 16,
}, rules);
```

还可以使用 when 的重载方法来实现复杂条件的验证：

```javascript
when(expect, chain)
```

* `expect`  预期值
* `chain:` 验证链

```javascript
const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    age: acr.when((data, context) => {
        return data['type'] === 'persnoal';
    }, () => {
        return acr.string().required().min(18);
    }),
};
```

### other 方法

other 方法只接受一个闭包作为参数，闭包返回值为一个验证链。从逻辑上来说，other 包含 when 没有包含的所有可能，因此，无论声明顺序是怎样，other 总是在所有条件之后执行，并且多次调用 other 只有最后一次有效。

```javascript
const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    age: acr.when('type', 'personal', () => {
        return acr.number().required().min(18);
    }).other(() => {
        return acr.string().equal('');
    }),
};

// 抛出错误，age 不等于 '空字符串'
await acr.validate({
    type: 'common',
    age: 16,
}, rules);
```

### 多条件验证

在 when 方法后面，可以连接多个 when 方法，来实现更加复杂的条件验证。

```javascript
const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    age: acr.when('type', 'personal', () => {
        return acr.number().required().min(18);
    }).when('type', 'common', () => {
        return acr.string().equal('16');
    }),
};
```

### 多个参数联合验证

一个参数值的匹配，可以满足大多数情况的需求，针对一些极端条件，第一个参数可以提供闭包，返回一个 boolean 值来确定是否满足条件。

```javascript
const rules = {
    type: acr.string().required().in([ 'personal', 'common' ]),
    gender: acr.string().required().in([ 'male', 'female' ]),
    age: acr.when((data, context) => {
        return data['type'] === 'personal' && data['gender'] === 'male';
    }, () => {
        return acr.number().required().min(18);
    }),
}
```

### 异步条件

当第一个参数为闭包时，支持使用异步方法来判断是否符合条件。

```javascript
const rules = {
    gender: acr.string().required().in([ 'male', 'female' ]),
    age: acr.when(async (data, context) => {
        return await Promise.resolve(data['gender'] === 'male');
    }, () => {
        return acr.number().required().min(18);
    }),
}
```


---

# 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://seek.gitbook.io/acr/logic-rule.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.
