1 |
|
2 |
|
3 |
|
4 |
|
5 | var Validator = require('../lib/modelValidator');
|
6 | var validator = new Validator();
|
7 |
|
8 | module.exports.validatorTests = {
|
9 | addBlankError: function(test) {
|
10 | test.expect(1);
|
11 | try {
|
12 | validator.addFieldValidator();
|
13 | test.ok(false);
|
14 | } catch(e){
|
15 | test.ok(e.message === 'ModelName is required', e);
|
16 | }
|
17 | test.done();
|
18 | },
|
19 | addFunctionAsModelNameError: function(test) {
|
20 | test.expect(1);
|
21 | try {
|
22 | validator.addFieldValidator(function(){});
|
23 | test.ok(false);
|
24 | } catch(e){
|
25 | test.ok(e.message === 'ModelName must be a string', e);
|
26 | }
|
27 | test.done();
|
28 | },
|
29 | addBlankFieldNameError: function(test) {
|
30 | test.expect(1);
|
31 | try {
|
32 | validator.addFieldValidator("test");
|
33 | test.ok(false);
|
34 | } catch(e){
|
35 | test.ok(e.message === 'FieldName is required', e);
|
36 | }
|
37 | test.done();
|
38 | },
|
39 | addFunctionAsFieldNameError: function(test) {
|
40 | test.expect(1);
|
41 | try {
|
42 | validator.addFieldValidator("test", function(){});
|
43 | test.ok(false);
|
44 | } catch(e){
|
45 | test.ok(e.message === 'FieldName must be a string', e);
|
46 | }
|
47 | test.done();
|
48 | },
|
49 | addBlankFunctionError: function(test) {
|
50 | test.expect(1);
|
51 | try {
|
52 | validator.addFieldValidator("test", "field");
|
53 | test.ok(false);
|
54 | } catch(e){
|
55 | test.ok(e.message === 'ValidatorFunction is required', e);
|
56 | }
|
57 | test.done();
|
58 | },
|
59 | addFunctionAsModelNameError: function(test) {
|
60 | test.expect(1);
|
61 |
|
62 | validator.addFieldValidator("test", "field", function(){});
|
63 | test.ok(validator.getFieldValidators("test", "field"));
|
64 | test.done();
|
65 | },
|
66 | testCustomValidationRun: function(test) {
|
67 | test.expect(2);
|
68 |
|
69 | var model = {
|
70 | id: "testModel",
|
71 | properties: {
|
72 | id: {
|
73 | type: "integer"
|
74 | }
|
75 | }
|
76 | };
|
77 |
|
78 | var data = {
|
79 | id: 34
|
80 | };
|
81 |
|
82 | validator = new Validator();
|
83 | validator.addFieldValidator("testModel", "id", function(name, value) {
|
84 | if(value === 34) {
|
85 | return new Error("Value Cannot be 34");
|
86 | }
|
87 |
|
88 | return null;
|
89 | });
|
90 | var result = validator.validate(data, model);
|
91 |
|
92 | test.ok(!result.valid);
|
93 | test.ok(result.errorCount === 1);
|
94 | test.done();
|
95 | },
|
96 | testCustomValidationReturnsArrayRun: function(test) {
|
97 | test.expect(2);
|
98 |
|
99 | var model = {
|
100 | id: "testModel",
|
101 | properties: {
|
102 | id: {
|
103 | type: "integer"
|
104 | }
|
105 | }
|
106 | };
|
107 |
|
108 | var data = {
|
109 | id: 34
|
110 | };
|
111 |
|
112 | validator = new Validator();
|
113 | validator.addFieldValidator("testModel", "id", function(name, value) {
|
114 | var errors = [];
|
115 | if(value === 34) {
|
116 | errors.push(new Error("Value Cannot be 34"));
|
117 | }
|
118 |
|
119 | if(value < 40) {
|
120 | errors.push(new Error("Value must be at least 40"));
|
121 | }
|
122 |
|
123 | return errors.length > 0 ? errors : null;
|
124 | });
|
125 | var result = validator.validate(data, model);
|
126 |
|
127 | test.ok(!result.valid);
|
128 | test.ok(result.errorCount === 2);
|
129 | test.done();
|
130 | },
|
131 | testCustomValidationReturnsnullRun: function(test) {
|
132 | test.expect(2);
|
133 |
|
134 | var model = {
|
135 | id: "testModel",
|
136 | properties: {
|
137 | id: {
|
138 | type: "integer"
|
139 | }
|
140 | }
|
141 | };
|
142 |
|
143 | var data = {
|
144 | id: 41
|
145 | };
|
146 |
|
147 | validator = new Validator();
|
148 | validator.addFieldValidator("testModel", "id", function(name, value) {
|
149 | var errors = [];
|
150 | if(value === 34) {
|
151 | errors.push(new Error("Value Cannot be 34"));
|
152 | }
|
153 |
|
154 | if(value < 40) {
|
155 | errors.push(new Error("Value must be at least 40"));
|
156 | }
|
157 |
|
158 | return errors.length > 0 ? errors : null;
|
159 | });
|
160 | var result = validator.validate(data, model);
|
161 |
|
162 | test.ok(result.valid);
|
163 | test.ok(result.errorCount === 0);
|
164 | test.done();
|
165 | },
|
166 | testCustomValidationReturnsEmptyArrayRun: function(test) {
|
167 | test.expect(2);
|
168 |
|
169 | var model = {
|
170 | id: "testModel",
|
171 | properties: {
|
172 | id: {
|
173 | type: "integer"
|
174 | }
|
175 | }
|
176 | };
|
177 |
|
178 | var data = {
|
179 | id: 41
|
180 | };
|
181 |
|
182 | validator = new Validator();
|
183 | validator.addFieldValidator("testModel", "id", function(name, value) {
|
184 | var errors = [];
|
185 | if(value === 34) {
|
186 | errors.push(new Error("Value Cannot be 34"));
|
187 | }
|
188 |
|
189 | if(value < 40) {
|
190 | errors.push(new Error("Value must be at least 40"));
|
191 | }
|
192 |
|
193 | return errors;
|
194 | });
|
195 | var result = validator.validate(data, model);
|
196 |
|
197 | test.ok(result.valid);
|
198 | test.ok(result.errorCount === 0);
|
199 | test.done();
|
200 | },
|
201 | testCustomValidationReturnsUndefinedlRun: function(test) {
|
202 | test.expect(2);
|
203 |
|
204 | var model = {
|
205 | id: "testModel",
|
206 | properties: {
|
207 | id: {
|
208 | type: "integer"
|
209 | }
|
210 | }
|
211 | };
|
212 |
|
213 | var data = {
|
214 | id: 41
|
215 | };
|
216 |
|
217 | validator = new Validator();
|
218 | validator.addFieldValidator("testModel", "id", function(name, value) {
|
219 | var errors = [];
|
220 | if(value === 34) {
|
221 | errors.push(new Error("Value Cannot be 34"));
|
222 | }
|
223 |
|
224 | if(value < 40) {
|
225 | errors.push(new Error("Value must be at least 40"));
|
226 | }
|
227 |
|
228 | return undefined;
|
229 | });
|
230 | var result = validator.validate(data, model);
|
231 |
|
232 | test.ok(result.valid);
|
233 | test.ok(result.errorCount === 0);
|
234 | test.done();
|
235 | }
|
236 | }; |
\ | No newline at end of file |