diff --git a/README.md b/README.md index 87deeea..28c80e1 100644 --- a/README.md +++ b/README.md @@ -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. diff --git a/examples/full.js b/examples/full.js index 693745b..638592a 100644 --- a/examples/full.js +++ b/examples/full.js @@ -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: { @@ -42,6 +43,7 @@ const obj = { id: 5, name: "John", sex: "male", + sex2: "female", settings: { //notify: true, notify: { diff --git a/lib/messages.js b/lib/messages.js index 22470ac..2461f89 100644 --- a/lib/messages.js +++ b/lib/messages.js @@ -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!", }; \ No newline at end of file diff --git a/lib/rules/enum.js b/lib/rules/enum.js index 5a376b6..fcfff74 100644 --- a/lib/rules/enum.js +++ b/lib/rules/enum.js @@ -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; diff --git a/test/rules/enum.spec.js b/test/rules/enum.spec.js index 82bb001..b46383a 100644 --- a/test/rules/enum.spec.js +++ b/test/rules/enum.spec.js @@ -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); }); - } +} );