(field)
| 205 | } |
| 206 | |
| 207 | validate(field) { |
| 208 | if (field == null) { |
| 209 | // 如果字段为空 |
| 210 | const allowEmpty = this._allowEmpty() |
| 211 | const defaultValue = this._hasDefault() |
| 212 | if (allowEmpty) { |
| 213 | return new RuleFieldResult(true, '', defaultValue) |
| 214 | } else { |
| 215 | return new RuleFieldResult(false, '字段是必填参数') |
| 216 | } |
| 217 | } |
| 218 | |
| 219 | const filedResult = new RuleFieldResult(false) |
| 220 | for (let rule of this.rules) { |
| 221 | let result = rule.validate(field) |
| 222 | if (!result.pass) { |
| 223 | filedResult.msg = result.msg |
| 224 | filedResult.legalValue = null |
| 225 | // 一旦一条校验规则不通过,则立即终止这个字段的验证 |
| 226 | return filedResult |
| 227 | } |
| 228 | } |
| 229 | return new RuleFieldResult(true, '', this._convert(field)) |
| 230 | } |
| 231 | |
| 232 | _convert(value) { |
| 233 | for (let rule of this.rules) { |
no test coverage detected