Skip to content

Commit

Permalink
add missing message text to "enum" rule
Browse files Browse the repository at this point in the history
Add "enum" to readme docs
Add actual value to enum err object
  • Loading branch information
icebob committed Jun 7, 2018
1 parent f126503 commit 8216e29
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 6 deletions.
18 changes: 18 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -309,6 +309,24 @@ Property | Default | Description
-------- | -------- | -----------
`mode` | `quick` | Checker method. Can be `quick` or `precise`.

## `enum`
This is an enum validator.

```js
let schema = {
sex: { type: "enum", values: ["male", "female"] }
}

v.validate({ sex: "male" }, schema); // Valid
v.validate({ sex: "female" }, schema); // Valid
v.validate({ sex: "other" }, schema); // Fail
```

### Properties
Property | Default | Description
-------- | -------- | -----------
`values` | `null` | The valid values.


## `forbidden`
This validator gives error if the property is exists in the object.
Expand Down
2 changes: 2 additions & 0 deletions examples/full.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const schema = {
]
}},
sex: { type: "string", enum: ["male", "female"] },
sex2: { type: "enum", values: ["male", "female"] },
roles: { type: "array", items: { type: "string" }, enum: ["admin", "user"] },
friends: { type: "array", items: { type: "number", positive: true }},
comments: { type: "array", items: { type: "object", props: {
Expand All @@ -42,6 +43,7 @@ const obj = {
id: 5,
name: "John",
sex: "male",
sex2: "female",
settings: {
//notify: true,
notify: {
Expand Down
4 changes: 3 additions & 1 deletion lib/messages.js
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@ module.exports = {

forbidden: "The '{field}' field is forbidden!",

email: "The '{field}' field must be a valid e-mail!"
email: "The '{field}' field must be a valid e-mail!",

enumValue: "The '{field} field value '{expected}' does not match any of the allowed values!",
};
2 changes: 1 addition & 1 deletion lib/rules/enum.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
module.exports = function checkEnum(value, schema) {

if (schema.values != null && schema.values.indexOf(value) === -1) {
return this.makeError("enumValue", schema.values);
return this.makeError("enumValue", schema.values, value);
}

return true;
Expand Down
8 changes: 4 additions & 4 deletions test/rules/enum.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,18 +11,18 @@ describe("Test checkEnum", () => {
it("check enum", () => {
const e1 = { type: "enum", values: ["male", "female"] };

expect(check("", e1)).toEqual({ type: "enumValue", expected: ["male", "female"] });
expect(check("human", e1)).toEqual({ type: "enumValue", expected: ["male", "female"] });
expect(check("", e1)).toEqual({ type: "enumValue", expected: ["male", "female"], actual: "" });
expect(check("human", e1)).toEqual({ type: "enumValue", expected: ["male", "female"], actual: "human" });
expect(check("male", e1)).toEqual(true);
expect(check("female", e1)).toEqual(true);

const e2 = { type: "enum", values: [null, 1, 2, "done", false] };
expect(check("male", e2)).toEqual({ type: "enumValue", expected: [null, 1, 2, "done", false] });
expect(check("male", e2)).toEqual({ type: "enumValue", expected: [null, 1, 2, "done", false], actual: "male" });
expect(check(null, e2)).toEqual(true);
expect(check(2, e2)).toEqual(true);
expect(check("done", e2)).toEqual(true);
expect(check(false, e2)).toEqual(true);
});

}
}
);

0 comments on commit 8216e29

Please sign in to comment.