Skip to content

Commit 5e7c2cb

Browse files
committed
haltOnFirstError example
1 parent e4fee54 commit 5e7c2cb

File tree

1 file changed

+116
-0
lines changed

1 file changed

+116
-0
lines changed

examples/halt-on-first-error.js

Lines changed: 116 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,116 @@
1+
"use strict";
2+
const Validator = require("../index");
3+
const v = new Validator({
4+
haltOnFirstError: true,
5+
debug: false,
6+
useNewCustomCheckerFunction: true
7+
});
8+
9+
const schema = {
10+
id: { type: "number", min: 8, max: 101 },
11+
name: { type: "string", optional: false, min: 5, max: 128 },
12+
settings: { type: "object", props: {
13+
notify: [
14+
{ type: "boolean" },
15+
{ type: "object" }
16+
]
17+
}},
18+
multi: [
19+
{ type: "string", min: 3, max: 255 },
20+
{ type: "boolean" }
21+
],
22+
sex: { type: "string", enum: ["male", "female"] },
23+
sex2: { type: "enum", values: ["male", "female"] },
24+
roles: { type: "array", items: { type: "string" }, enum: ["admin", "user"] },
25+
friends: { type: "array", items: { type: "number", positive: true }},
26+
comments: { type: "array", items: { type: "object", props: {
27+
user: { type: "number", positive: true, integer: true },
28+
content: { type: "string" },
29+
voters: { type: "array", optional: true, items: { type: "number" }}
30+
} } },
31+
multiarray: { type: "array", empty: false, items: {
32+
type: "array", empty: true, items: {
33+
type: "number"
34+
}
35+
}},
36+
email: { type: "email", optional: true },
37+
homepage: { type: "url", optional: true },
38+
status: "boolean",
39+
age: { type: "number", min: 18, max: 100, convert: true },
40+
apikey: "forbidden",
41+
uuidv4: { type: "uuid", version: 4 },
42+
uuid: "uuid",
43+
phone: { type: "string", length: 15, custom: (v, errors) => {
44+
if (!v.startsWith("+"))
45+
errors.push({ type: "phoneNumber", actual: v });
46+
return v.replace(/[^\d+]/g, "");
47+
} },
48+
action: "function",
49+
created: "date",
50+
raw: { type: "class", instanceOf: Buffer, custom: v => v.toString("base64") },
51+
now: { type: "date", convert: true },
52+
state: {
53+
type: "enum",
54+
values: ["active", "inactive"],
55+
defVal: "active",
56+
default: (schema, field, parent, context) => {
57+
return schema.defVal;
58+
}
59+
}
60+
};
61+
62+
const obj = {
63+
id: 5, // expect error min 8
64+
name: "John", // expect error min len 5
65+
sex: "N/A", // expect error
66+
sex2: "invalid", // expect error
67+
settings: {
68+
//notify: true,
69+
notify: {
70+
corner: "top"
71+
}
72+
},
73+
multi: "AA", // expect error
74+
roles: [
75+
"reader" // expect error
76+
],
77+
78+
friends: [
79+
5,
80+
10,
81+
2
82+
],
83+
84+
comments: [
85+
{ user: 1, content: "Cool!" },
86+
{ user: 2, content: "Very fast!" },
87+
{ user: 1, content: "", voters: [1] }
88+
],
89+
90+
multiarray: [
91+
[
92+
],
93+
[
94+
5,
95+
10,
96+
//"a"
97+
]
98+
],
99+
100+
email: "john.doe@clipboard.space",
101+
homepage: "http://google.com",
102+
status: true,
103+
age: "28",
104+
apikey: null,
105+
uuidv4: "10ba038e-48da-487b-96e8-8d3b99b6d18a",
106+
uuid: "10ba038e-48da-487b-96e8-8d3b99b6d18a",
107+
phone: "+36-70-123-4567",
108+
action: () => {},
109+
created: new Date(),
110+
now: Date.now(),
111+
raw: Buffer.from([1,2,3])
112+
};
113+
114+
const res = v.validate(obj, schema);
115+
116+
console.log(res, obj);

0 commit comments

Comments
 (0)