UNPKG

5.9 kBJavaScriptView Raw
1/**
2 * Created by bdunn on 18/09/2014.
3 */
4var Validator = require('../lib/modelValidator');
5var validator = new Validator();
6
7module.exports.validationTests = {
8 invalidStringTypeTest: function(test) {
9 var data = {
10 id: 1
11 };
12 var model = {
13 required: [ 'id' ],
14 properties: {
15 id: {
16 type: 'string',
17 description: 'The object id'
18 }
19 }
20 };
21
22 var errors = validator.validate(data, model);
23
24 test.expect(2);
25 test.ok(!errors.valid);
26 test.ok(errors.errors[0].message === 'id (1) is not a type of string', errors.errors[0].message);
27
28 test.done();
29 },
30 stringBlankTest: function(test) {
31 var data = {
32 id: ""
33 };
34 var model = {
35 required: [],
36 properties: {
37 id: {
38 type: 'string',
39 description: 'The object id'
40 }
41 }
42 };
43
44 var errors = validator.validate(data, model);
45
46 test.expect(1);
47 test.ok(errors.valid);
48
49 test.done();
50 },
51 stringBlankWhenRequiredTest: function(test) {
52 var data = {
53 id: ""
54 };
55 var model = {
56 required: ['id'],
57 properties: {
58 id: {
59 type: 'string',
60 description: 'The object id'
61 }
62 }
63 };
64
65 var errors = validator.validate(data, model);
66
67 test.expect(2);
68 test.ok(!errors.valid);
69 test.ok(errors.errors[0].message === 'id is a required field');
70
71 test.done();
72 },
73 validStringTypeTest: function(test) {
74 var data = {
75 sample: 'Helpful Text sample'
76 };
77 var model = {
78 required: [],
79 properties: {
80 sample: {
81 type: 'string'
82 }
83 }
84 };
85
86 var errors = validator.validate(data, model);
87
88 test.expect(1);
89 test.ok(errors.valid);
90
91 test.done();
92 },
93 lengthOkTest: function(test) {
94 var data = {
95 sample: 'TestData'
96 };
97 var model = {
98 properties: {
99 sample: {
100 type: 'string',
101 minLength: 4,
102 maxLength: 12
103 }
104 }
105 };
106
107 var errors = validator.validate(data, model);
108
109 test.expect(1);
110 test.ok(errors.valid);
111
112 test.done();
113 },
114 lengthShortTest: function(test) {
115 var data = {
116 sample: 'Me'
117 };
118 var model = {
119 properties: {
120 sample: {
121 type: 'string',
122 minLength: 4,
123 maxLength: 12
124 }
125 }
126 };
127
128 var errors = validator.validate(data, model);
129
130 test.expect(2);
131 test.ok(!errors.valid);
132 test.ok(errors.errors[0].message === 'sample must be at least 4 characters long and no more than 12 characters long');
133
134 test.done();
135 },
136 lengthLongTest: function(test) {
137 var data = {
138 sample: 'ThisTestDataIsTooLong'
139 };
140 var model = {
141 properties: {
142 sample: {
143 type: 'string',
144 minLength: 4,
145 maxLength: 12
146 }
147 }
148 };
149
150 var errors = validator.validate(data, model);
151
152 test.expect(2);
153 test.ok(!errors.valid);
154 test.ok(errors.errors[0].message === 'sample must be at least 4 characters long and no more than 12 characters long');
155
156 test.done();
157 },
158 validateTypeOfUndefinedPropertyTest: function(test) {
159 var data = {
160 sample: true
161 };
162 var model = {
163 required: [],
164 properties: {
165 sample: {
166 type: 'boolean'
167 },
168 truffle: {
169 type: 'boolean'
170 }
171 }
172 };
173
174 var errors = validator.validate(data, model);
175
176 test.expect(1);
177 test.ok(errors.valid);
178
179 test.done();
180 },
181 validateStringMinLengthFailsTest: function(test) {
182 var data = {
183 sample: true,
184 tag: ""
185 };
186 var model = {
187 required: [],
188 properties: {
189 sample: {
190 type: 'boolean'
191 },
192 truffle: {
193 type: 'boolean'
194 },
195 tag: {
196 type: "string",
197 minLength: 1
198 }
199 }
200 };
201
202 var errors = validator.validate(data, model);
203
204 test.expect(2);
205 test.ok(!errors.valid);
206 test.ok(errors.errors[0].message === 'tag cannot be blank', errors.errors[0].message);
207
208 test.done();
209}
210 ,
211 validateStringMaxLengthFailsTest: function(test) {
212 var data = {
213 sample: true,
214 tag: "12"
215 };
216 var model = {
217 required: [],
218 properties: {
219 sample: {
220 type: 'boolean'
221 },
222 truffle: {
223 type: 'boolean'
224 },
225 tag: {
226 type: "string",
227 maxLength: 1
228 }
229 }
230 };
231
232 var errors = validator.validate(data, model);
233
234 test.expect(1);
235 test.ok(!errors.valid);
236 //test.ok(false, errors.errors);
237
238 test.done();
239 }
240};
\No newline at end of file