You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
expect(check({d: 4})).toEqual([{"actual": undefined,"field": "a","message": "The 'a' field is required.","type": "required"},{"actual": undefined,"field": "b","message": "The 'b' field is required.","type": "required"},{"actual": undefined,"field": "c","message": "The 'c' field is required.","type": "required"}]);
89
89
});
90
90
});
91
+
92
+
describe("should work with custom validator",()=>{
93
+
constcheckerFn=jest.fn(()=>{});
94
+
95
+
constv=newValidator({
96
+
useNewCustomCheckerFunction: true,
97
+
aliases: {
98
+
strOK: {
99
+
type: "string",
100
+
custom: (value,errors)=>{
101
+
checkerFn();
102
+
if(value!=="OK"){
103
+
errors.push({type: "strOK"});
104
+
return;
105
+
}
106
+
returnvalue;
107
+
}
108
+
},
109
+
num99: {
110
+
type: "number",
111
+
custom: (value,errors)=>{
112
+
checkerFn();
113
+
if(value!==99){
114
+
errors.push({type: "num99"});
115
+
return;
116
+
}
117
+
returnvalue;
118
+
}
119
+
}
120
+
}
121
+
});
122
+
123
+
constschema={
124
+
a: {
125
+
type: "multi",
126
+
rules: ["strOK","num99"]
127
+
}
128
+
};
129
+
constcheck=v.compile(schema);
130
+
131
+
it("test strOK",()=>{
132
+
{
133
+
consto={a: "OK"};
134
+
expect(check(o)).toBe(true);
135
+
expect(o).toStrictEqual({a: "OK"});
136
+
expect(checkerFn).toBeCalledTimes(1);
137
+
}
138
+
{
139
+
consto={a: "not-OK"};
140
+
expect(check(o)).toStrictEqual([{"field": "a","message": undefined,"type": "strOK"},{"actual": "not-OK","field": "a","message": "The 'a' field must be a number.","type": "number"},{"field": "a","message": undefined,"type": "num99"}]);
141
+
expect(o).toStrictEqual({a: "not-OK"});
142
+
expect(checkerFn).toBeCalledTimes(3);
143
+
}
144
+
});
145
+
146
+
it("test num99",()=>{
147
+
{
148
+
consto={a: 99};
149
+
expect(check(o)).toBe(true);
150
+
expect(o).toStrictEqual({a: 99});
151
+
expect(checkerFn).toBeCalledTimes(5);
152
+
}
153
+
{
154
+
consto={a: 1199};
155
+
expect(check(o)).toStrictEqual([{"actual": 1199,"field": "a","message": "The 'a' field must be a string.","type": "string"},{"field": "a","message": undefined,"type": "strOK"},{"field": "a","message": undefined,"type": "num99"}]);
0 commit comments