1 | /**
|
2 | * Created by bdunn on 10/11/2014.
|
3 | */
|
4 | var Valdiator = require('../lib/modelValidator');
|
5 | var validator = new Valdiator();
|
6 |
|
7 | module.exports.refTests = {
|
8 | hasRefTest: function(test) {
|
9 | var data = {
|
10 | sample: true,
|
11 | location: {
|
12 | top: 1,
|
13 | left: 1,
|
14 | right: 5,
|
15 | bottom: 5
|
16 | }
|
17 | };
|
18 |
|
19 | var models = {
|
20 | dataModel: {
|
21 | required: [ "sample" ],
|
22 | properties: {
|
23 | sample: {
|
24 | type: "boolean"
|
25 | },
|
26 | location: {
|
27 | $ref: "Location"
|
28 | }
|
29 | }
|
30 | },
|
31 | Location: {
|
32 | required: [ "top", "left" ],
|
33 | properties: {
|
34 | top: {
|
35 | type: "integer"
|
36 | },
|
37 | left: {
|
38 | type: "integer"
|
39 | },
|
40 | right: {
|
41 | type: "integer"
|
42 | },
|
43 | bottom: {
|
44 | type: "integer"
|
45 | }
|
46 | }
|
47 | }
|
48 | };
|
49 |
|
50 | var errors = validator.validate(data, models["dataModel"], models);
|
51 |
|
52 | test.expect(1);
|
53 | test.ok(errors.valid);
|
54 | test.done();
|
55 | },
|
56 | hasRefWithMissingDataTest: function(test) {
|
57 | var data = {
|
58 | sample: true,
|
59 | location: {
|
60 | right: 1,
|
61 | bottom: 1
|
62 | }
|
63 | };
|
64 |
|
65 | var models = {
|
66 | dataModel: {
|
67 | required: [ "sample" ],
|
68 | properties: {
|
69 | sample: {
|
70 | type: "boolean"
|
71 | },
|
72 | location: {
|
73 | $ref: "Location"
|
74 | }
|
75 | }
|
76 | },
|
77 | Location: {
|
78 | required: [ "top", "left" ],
|
79 | properties: {
|
80 | top: {
|
81 | type: "integer"
|
82 | },
|
83 | left: {
|
84 | type: "integer"
|
85 | },
|
86 | right: {
|
87 | type: "integer"
|
88 | },
|
89 | bottom: {
|
90 | type: "integer"
|
91 | }
|
92 | }
|
93 | }
|
94 | };
|
95 |
|
96 | var errors = validator.validate(data, models["dataModel"], models);
|
97 |
|
98 | test.expect(2);
|
99 | test.ok(!errors.valid);
|
100 | test.ok(errors.errorCount === 2, "Errors: " + errors.errors);
|
101 | test.done();
|
102 | },
|
103 | hasRefWithFailingTypeTest: function(test) {
|
104 | var data = {
|
105 | sample: true,
|
106 | location: {
|
107 | top: 1,
|
108 | left: 1,
|
109 | right: 5,
|
110 | bottom: "Fantasy"
|
111 | }
|
112 | };
|
113 |
|
114 | var models = {
|
115 | dataModel: {
|
116 | required: [ "sample" ],
|
117 | properties: {
|
118 | sample: {
|
119 | type: "boolean"
|
120 | },
|
121 | location: {
|
122 | $ref: "Location"
|
123 | }
|
124 | }
|
125 | },
|
126 | Location: {
|
127 | required: [ "top", "left" ],
|
128 | properties: {
|
129 | top: {
|
130 | type: "integer"
|
131 | },
|
132 | left: {
|
133 | type: "integer"
|
134 | },
|
135 | right: {
|
136 | type: "integer"
|
137 | },
|
138 | bottom: {
|
139 | type: "integer"
|
140 | }
|
141 | }
|
142 | }
|
143 | };
|
144 |
|
145 | var errors = validator.validate(data, models["dataModel"], models);
|
146 |
|
147 | test.expect(1);
|
148 | test.ok(!errors.valid);
|
149 | test.done();
|
150 | },
|
151 | hasRefWithTooLargeANumberTest: function(test) {
|
152 | var data = {
|
153 | sample: true,
|
154 | location: {
|
155 | top: 1,
|
156 | left: 1,
|
157 | right: 5,
|
158 | bottom: 5
|
159 | }
|
160 | };
|
161 |
|
162 | var models = {
|
163 | dataModel: {
|
164 | required: [ "sample" ],
|
165 | properties: {
|
166 | sample: {
|
167 | type: "boolean"
|
168 | },
|
169 | location: {
|
170 | $ref: "Location"
|
171 | }
|
172 | }
|
173 | },
|
174 | Location: {
|
175 | required: [ "top", "left" ],
|
176 | properties: {
|
177 | top: {
|
178 | type: "integer"
|
179 | },
|
180 | left: {
|
181 | type: "integer"
|
182 | },
|
183 | right: {
|
184 | type: "integer"
|
185 | },
|
186 | bottom: {
|
187 | type: "integer",
|
188 | maximum: 2
|
189 | }
|
190 | }
|
191 | }
|
192 | };
|
193 |
|
194 | var errors = validator.validate(data, models["dataModel"], models);
|
195 |
|
196 | test.expect(1);
|
197 | test.ok(!errors.valid);
|
198 | test.done();
|
199 | }
|
200 | }; |
\ | No newline at end of file |