Skip to content

Commit

Permalink
Merge pull request #164 from jpenna/master
Browse files Browse the repository at this point in the history
Enum validates `false` value
  • Loading branch information
yiminghe committed Aug 7, 2019
2 parents fb862c9 + baf3932 commit 2452cdf
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 2 deletions.
14 changes: 14 additions & 0 deletions README.md
Expand Up @@ -193,6 +193,8 @@ If the `len` property is combined with the `min` and `max` range properties, `le

#### Enumerable

> Since version 3.0.0 if you want to validate the values `0` or `false` inside `enum` types, you have to include them explicitly.
To validate a value from a list of possible values use the `enum` type with a `enum` property listing the valid values for the field, for example:

```javascript
Expand Down Expand Up @@ -420,6 +422,18 @@ var Schema = require('async-validator').default;
Schema.warning = function(){};
```

### How to check if it is `true`

Use `enum` type passing `true` as option.

```js
{
type: 'enum',
enum: [true],
message: '',
}
```

## Test Case

```
Expand Down
18 changes: 18 additions & 0 deletions __tests__/enum.spec.js
@@ -0,0 +1,18 @@
import Schema from '../src/';

describe('enum', () => {
it('run validation on `false`', (done) => {
new Schema({
v: {
type: 'enum',
enum: [true],
},
}).validate({
v: false,
}, (errors) => {
expect(errors.length).toBe(1);
expect(errors[0].message).toBe('v must be one of true');
done();
});
});
});
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "async-validator",
"version": "2.0.1",
"version": "3.0.0",
"description": "validate form asynchronous",
"keywords": [
"validator",
Expand Down
2 changes: 1 addition & 1 deletion src/validator/enum.js
Expand Up @@ -22,7 +22,7 @@ function enumerable(rule, value, callback, source, options) {
return callback();
}
rules.required(rule, value, source, errors, options);
if (value) {
if (value !== undefined) {
rules[ENUM](rule, value, source, errors, options);
}
}
Expand Down

0 comments on commit 2452cdf

Please sign in to comment.