Skip to content

Commit f83d71b

Browse files
authored
Merge pull request #55 from intech/master
add `uuid` rule fix#43
2 parents 1f18db2 + 66c583b commit f83d71b

File tree

5 files changed

+100
-2
lines changed

5 files changed

+100
-2
lines changed

examples/full.js

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ const schema = {
3535
status: "boolean",
3636
age: { type: "number", min: 18, max: 100, convert: true },
3737
apikey: "forbidden",
38+
uuidv4: { type: "uuid", version: 4 },
39+
uuid: "uuid",
3840
action: "function",
3941
created: "date"
4042
};
@@ -81,6 +83,8 @@ const obj = {
8183
status: true,
8284
age: "28",
8385
apikey: null,
86+
uuidv4: "10ba038e-48da-487b-96e8-8d3b99b6d18a",
87+
uuid: "10ba038e-48da-487b-96e8-8d3b99b6d18a",
8488
action: () => {},
8589
created: new Date()
8690
};

lib/messages.js

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ module.exports = {
2424
numberInteger: "The '{field}' field must be an integer!",
2525
numberPositive: "The '{field}' field must be a positive number!",
2626
numberNegative: "The '{field}' field must be a negative number!",
27-
27+
2828
array: "The '{field}' field must be an array!",
2929
arrayEmpty: "The '{field}' field must not be an empty array!",
3030
arrayMin: "The '{field}' field must contain at least {expected} items!",
@@ -42,10 +42,13 @@ module.exports = {
4242
dateMax: "The '{field}' field must be less than or equal to {expected}!",
4343

4444
forbidden: "The '{field}' field is forbidden!",
45-
45+
4646
email: "The '{field}' field must be a valid e-mail!",
4747

4848
url: "The '{field}' field must be a valid URL!",
4949

5050
enumValue: "The '{field} field value '{expected}' does not match any of the allowed values!",
51+
52+
uuid: "The {field} field must be a valid UUID",
53+
uuidVersion: "The {field} field must be a valid version provided",
5154
};

lib/rules/uuid.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
"use strict";
2+
3+
const PATTERN = /^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
4+
5+
module.exports = function checkUUID(value, schema) {
6+
if (typeof value !== "string")
7+
return this.makeError("string");
8+
9+
value = value.toLowerCase();
10+
if (!PATTERN.test(value))
11+
return this.makeError("uuid");
12+
13+
const version = value.charAt(14)|0;
14+
if(schema.version && schema.version !== version)
15+
return this.makeError("uuidVersion", schema.version, version);
16+
17+
switch (version) {
18+
case 1:
19+
case 2:
20+
return true;
21+
case 3:
22+
case 4:
23+
case 5:
24+
return ["8", "9", "a", "b"].indexOf(value.charAt(19)) !== -1 || this.makeError("uuid");
25+
}
26+
};

test/messages.spec.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,8 @@ describe("Test Messages", () => {
3939
expect(msg.forbidden).toBeDefined();
4040
expect(msg.email).toBeDefined();
4141
expect(msg.url).toBeDefined();
42+
expect(msg.uuid).toBeDefined();
43+
expect(msg.uuidVersion).toBeDefined();
4244

4345
});
4446

test/rules/uuid.spec.js

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
"use strict";
2+
3+
const Validator = require("../../lib/validator");
4+
const fn = require("../../lib/rules/uuid");
5+
6+
const v = new Validator();
7+
const check = fn.bind(v);
8+
9+
describe("Test checkUUID", () => {
10+
11+
it("should check type of value", () => {
12+
const uuid = {type: "uuid"};
13+
const err = {type: "uuid"};
14+
const errString = {type: "string"};
15+
16+
expect(check(null, uuid)).toEqual(errString);
17+
expect(check(undefined, uuid)).toEqual(errString);
18+
expect(check(0, uuid)).toEqual(errString);
19+
expect(check(1, uuid)).toEqual(errString);
20+
expect(check("", uuid)).toEqual(err);
21+
expect(check("true", uuid)).toEqual(err);
22+
expect(check([], uuid)).toEqual(errString);
23+
expect(check({}, uuid)).toEqual(errString);
24+
expect(check(false, uuid)).toEqual(errString);
25+
expect(check(true, uuid)).toEqual(errString);
26+
expect(check("00000000-0000-0000-0000-000000000000", uuid)).toEqual(err);
27+
expect(check("1234567-1234-1234-1234-1234567890ab", uuid)).toEqual(err);
28+
expect(check("12345678-1234-1234-1234-1234567890ab", uuid)).toEqual(true);
29+
});
30+
31+
it("check invalid version", () => {
32+
const err = {type: "uuid"};
33+
const v1 = {type: "uuid", version: 1};
34+
const v2 = {type: "uuid", version: 2};
35+
const v3 = {type: "uuid", version: 3};
36+
const v4 = {type: "uuid", version: 4};
37+
const v5 = {type: "uuid", version: 5};
38+
39+
expect(check("00000000-0000-7000-0000-000000000000", err)).toEqual(err);
40+
expect(check("fdda765f-fc57-5604-c269-52a7df8164ec", v5)).toEqual(err);
41+
expect(check("9a7b330a-a736-51e5-af7f-feaf819cdc9f", v1)).toEqual({"actual": 5, "expected": 1, "type": "uuidVersion"});
42+
expect(check("9a7b330a-a736-51e5-af7f-feaf819cdc9f", v1)).toEqual({"actual": 5, "expected": 1, "type": "uuidVersion"});
43+
expect(check("9a7b330a-a736-41e5-af7f-feaf819cdc9f", v2)).toEqual({"actual": 4, "expected": 2, "type": "uuidVersion"});
44+
expect(check("9a7b330a-a736-41e5-af7f-feaf819cdc9f", v3)).toEqual({"actual": 4, "expected": 3, "type": "uuidVersion"});
45+
expect(check("9a7b330a-a736-21e5-af7f-feaf819cdc9f", v4)).toEqual({"actual": 2, "expected": 4, "type": "uuidVersion"});
46+
expect(check("9a7b330a-a736-11e5-af7f-feaf819cdc9f", v5)).toEqual({"actual": 1, "expected": 5, "type": "uuidVersion"});
47+
});
48+
49+
it("check valid version", () => {
50+
const v1 = {type: "uuid", version: 1};
51+
const v2 = {type: "uuid", version: 2};
52+
const v3 = {type: "uuid", version: 3};
53+
const v4 = {type: "uuid", version: 4};
54+
const v5 = {type: "uuid", version: 5};
55+
56+
expect(check("45745c60-7b1a-11e8-9c9c-2d42b21b1a3e", v1)).toEqual(true);
57+
expect(check("9a7b330a-a736-21e5-af7f-feaf819cdc9f", v2)).toEqual(true);
58+
expect(check("9125a8dc-52ee-365b-a5aa-81b0b3681cf6", v3)).toEqual(true);
59+
expect(check("10ba038e-48da-487b-96e8-8d3b99b6d18a", v4)).toEqual(true);
60+
expect(check("fdda765f-fc57-5604-a269-52a7df8164ec", v5)).toEqual(true);
61+
62+
});
63+
});

0 commit comments

Comments
 (0)