Skip to content

Commit dedc679

Browse files
committed
more tests
1 parent 26290b8 commit dedc679

File tree

3 files changed

+94
-2
lines changed

3 files changed

+94
-2
lines changed

test/integration.spec.js

Lines changed: 33 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -895,7 +895,7 @@ describe("Test $$strict schema restriction on root-level", () => {
895895
});
896896
});
897897

898-
describe("Test $$strict schema restriction on root-level for nested objects", () => {
898+
describe("Test $$strict schema restriction for nested objects", () => {
899899
const v = new Validator({ debug: false });
900900

901901
let schema = {
@@ -932,7 +932,7 @@ describe("Test $$strict schema restriction on root-level for nested objects", ()
932932
});
933933
});
934934

935-
describe("Test $$strict schema restriction on sub-level", () => {
935+
describe("Test strict schema restriction on sub-level", () => {
936936
const v = new Validator();
937937

938938
let schema = {
@@ -964,3 +964,34 @@ describe("Test $$strict schema restriction on sub-level", () => {
964964
});
965965
});
966966

967+
describe("Test default value sanitizer", () => {
968+
const v = new Validator();
969+
970+
let schema = {
971+
id: { type: "number", default: 5 },
972+
name: { type: "string", default: "John" },
973+
age: { type: "number", optional: true, default: 33 },
974+
roles: { type: "array", items: "string", default: ["user"] },
975+
status: { type: "boolean", default: true }
976+
};
977+
let check = v.compile(schema);
978+
979+
it("should fill not defined properties", () => {
980+
let obj = {
981+
name: null,
982+
status: false
983+
};
984+
985+
let res = check(obj);
986+
987+
expect(res).toBe(true);
988+
expect(obj).toEqual({
989+
id: 5,
990+
name: "John",
991+
age: 33,
992+
roles: ["user"],
993+
status: false
994+
});
995+
});
996+
});
997+

test/rules/forbidden.spec.js

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,4 +19,30 @@ describe("Test rule: forbidden", () => {
1919
expect(check(false)).toEqual([{ type: "forbidden", actual: false, message }]);
2020
expect(check(true)).toEqual([{ type: "forbidden", actual: true, message }]);
2121
});
22+
23+
24+
describe("Test sanitization", () => {
25+
26+
it("should remove the field if 'remove: true'", () => {
27+
let schema = {
28+
id: { type: "number" },
29+
name: { type: "string" },
30+
token: { type: "forbidden", remove: true }
31+
};
32+
let check = v.compile(schema);
33+
34+
const obj = {
35+
id: 2,
36+
name: "John"
37+
};
38+
39+
expect(check(obj)).toEqual(true);
40+
expect(obj).toEqual({
41+
id: 2,
42+
name: "John"
43+
});
44+
});
45+
46+
});
47+
2248
});

test/rules/object.spec.js

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,4 +56,39 @@ describe("Test rule: object", () => {
5656
expect(check({ user: { firstName: "John", address: { country: "UK" }}}))
5757
.toEqual([{ type: "required", field: "user.address.city", actual: undefined, message: "The 'user.address.city' field is required." }]);
5858
});
59+
60+
61+
describe("Test sanitization", () => {
62+
63+
it("should remove additional fields if 'strict: 'remove''", () => {
64+
let schema = {
65+
name: { type: "string" },
66+
address: { type: "object", strict: "remove", properties: {
67+
country: "string",
68+
city: "string"
69+
} }
70+
};
71+
let check = v.compile(schema);
72+
73+
const obj = {
74+
name: "John",
75+
address: {
76+
country: "Hungary",
77+
city: "Budapest",
78+
street: "Kossuth Lajos street",
79+
zip: 1234
80+
}
81+
};
82+
83+
expect(check(obj)).toEqual(true);
84+
expect(obj).toEqual({
85+
name: "John",
86+
address: {
87+
country: "Hungary",
88+
city: "Budapest"
89+
}
90+
});
91+
});
92+
93+
});
5994
});

0 commit comments

Comments
 (0)