1
|
|
|
"use strict"; |
2
|
|
|
const validate = require("../src/validate"); |
3
|
|
|
const JString = require("../src/types/string"); |
4
|
|
|
|
5
|
|
|
test("test validate not required", () => { |
6
|
|
|
const data = { "username": "oenstrom" }; |
7
|
|
|
const schema = { "test": {} }; |
8
|
|
|
|
9
|
|
|
expect(validate(data, schema)).toBeNull(); |
10
|
|
|
}); |
11
|
|
|
|
12
|
|
|
test("test validate required", () => { |
13
|
|
|
const data = {}; |
14
|
|
|
const schema = { "test": new JString().required() }; |
15
|
|
|
|
16
|
|
|
expect(validate(data, schema)).toEqual({ error: { |
17
|
|
|
type: "Any:required", |
18
|
|
|
field: "test", |
19
|
|
|
message: "'test' is required and can't be omitted." |
20
|
|
|
}}); |
21
|
|
|
}); |
22
|
|
|
|
23
|
|
|
test("test validate using type validate", () => { |
24
|
|
|
const schema = { "test": new JString().alphanum() }; |
25
|
|
|
const data = { "test": "test(/)!" }; |
26
|
|
|
const data2 = { "test": "test" }; |
27
|
|
|
|
28
|
|
|
expect(validate(data, schema, "json")).toEqual( |
29
|
|
|
JSON.stringify({ error: { |
30
|
|
|
type: "JString:alphanum", |
31
|
|
|
field: "test", |
32
|
|
|
message: "'test' only accepts alphanumeric characters." |
33
|
|
|
}}, null, 4) |
34
|
|
|
); |
35
|
|
|
expect(validate(data2, schema)).toBeNull(); |
36
|
|
|
}); |
37
|
|
|
|
38
|
|
|
test("test multiple fields", () => { |
39
|
|
|
const schema = { |
40
|
|
|
username: new JString().min(3).max(3), |
41
|
|
|
password: new JString().min(6).max(100) |
42
|
|
|
}; |
43
|
|
|
const data = { password: "test" }; |
44
|
|
|
|
45
|
|
|
expect(validate(data, schema)).toEqual({ error: { |
46
|
|
|
type: "JString:min", |
47
|
|
|
field: "password", |
48
|
|
|
message: "'password' has a minimum required length of '6'." |
49
|
|
|
}}); |
50
|
|
|
}); |
51
|
|
|
|