UNPKG

3.82 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 validateDate: function(test) {
9 var data = {
10 travis: 'test',
11 dob: '2014-02-01'
12 };
13 var model = {
14 properties: {
15 dob: {
16 type: 'string',
17 format: 'date'
18 }
19 }
20 };
21
22 var errors = validator.validate(data, model);
23
24 test.expect(1);
25 test.ok(errors.valid);
26
27 test.done();
28 },
29 validateNotADate: function(test) {
30 var data = {
31 travis: 'test',
32 dob: 'This is NOt a Date'
33 };
34 var model = {
35 properties: {
36 dob: {
37 type: 'string',
38 format: 'date'
39 }
40 }
41 };
42
43 var errors = validator.validate(data, model);
44
45 test.expect(1);
46 test.ok(!errors.valid);
47
48 test.done();
49 },
50 validateNumberAsDateFails: function(test) {
51 var data = {
52 travis: 'test',
53 dob: '3456'
54 };
55 var model = {
56 properties: {
57 dob: {
58 type: 'string',
59 format: 'date'
60 }
61 }
62 };
63
64 var errors = validator.validate(data, model);
65
66 test.expect(1);
67 test.ok(!errors.valid);
68
69 test.done();
70 },
71 validateDateTime: function(test) {
72 var data = {
73 "salutation": "Mr Death",
74 "dateOfBirth": "2014-01-01"
75 };
76 var model = {
77 properties: {
78 dateOfBirth: {
79 type: "string",
80 format: 'date-time'
81 }
82 }
83 };
84
85 var errors = validator.validate(data, model);
86
87 test.expect(1);
88 test.ok(errors.valid);
89
90 test.done();
91 },
92 validateDateFormatOk: function(test) {
93 var data = {
94 "salutation": "Mr Death",
95 "dateOfBirth": "2014-01-01"
96 };
97 var model = {
98 properties: {
99 dateOfBirth: {
100 type: "string",
101 format: 'date'
102 }
103 }
104 };
105
106 var errors = validator.validate(data, model);
107
108 test.expect(1);
109 test.ok(errors.valid);
110
111 test.done();
112 },
113 validateDateFormatFailed: function(test) {
114 var data = {
115 "salutation": "Mr Death",
116 "dateOfBirth": "2014-1-1"
117 };
118 var model = {
119 properties: {
120 dateOfBirth: {
121 type: "string",
122 format: 'date'
123 }
124 }
125 };
126
127 var errors = validator.validate(data, model);
128
129 test.expect(1);
130 test.ok(!errors.valid);
131
132 test.done();
133 },
134
135 validateDateTime: function(test) {
136 var data = {
137 "salutation": "Mr Death",
138 "dateOfBirth": "2014-01-01T12:00:00"
139 };
140 var model = {
141 properties: {
142 dateOfBirth: {
143 type: "string",
144 format: 'date'
145 }
146 }
147 };
148
149 var errors = validator.validate(data, model);
150
151 test.expect(3);
152 test.ok(!errors.valid);
153 test.ok(errors.errorCount === 1);
154 test.ok(errors.errors[0].message === 'dateOfBirth (2014-01-01T12:00:00) is not a type of date', errors.errors[0].message);
155
156 test.done();
157 }
158};
\No newline at end of file