Debian Codegraph @

LspCpp/third_party/rapidjson/test/unittest/schematest.cpp

raw
1 // Tencent is pleased to support the open source community by making RapidJSON available.
2 //
3 // Copyright (C) 2015 THL A29 Limited, a Tencent company, and Milo Yip.
4 //
5 // Licensed under the MIT License (the "License"); you may not use this file except
6 // in compliance with the License. You may obtain a copy of the License at
7 //
8 // http://opensource.org/licenses/MIT
9 //
10 // Unless required by applicable law or agreed to in writing, software distributed
11 // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
12 // CONDITIONS OF ANY KIND, either express or implied. See the License for the
13 // specific language governing permissions and limitations under the License.
14
15 #define RAPIDJSON_SCHEMA_VERBOSE 0
16 #define RAPIDJSON_HAS_STDSTRING 1
17
18 #include "unittest.h"
19 #include "rapidjson/schema.h"
20 #include "rapidjson/stringbuffer.h"
21 #include "rapidjson/writer.h"
22 #include "rapidjson/error/error.h"
23 #include "rapidjson/error/en.h"
24
25 #ifdef __clang__
26 RAPIDJSON_DIAG_PUSH
27 RAPIDJSON_DIAG_OFF(variadic-macros)
28 #elif defined(_MSC_VER)
29 RAPIDJSON_DIAG_PUSH
30 RAPIDJSON_DIAG_OFF(4822) // local class member function does not have a body
31 #endif
32
33 using namespace rapidjson;
34
35 #define TEST_HASHER(json1, json2, expected) \
36 {\
37 Document d1, d2;\
38 d1.Parse(json1);\
39 ASSERT_FALSE(d1.HasParseError());\
40 d2.Parse(json2);\
41 ASSERT_FALSE(d2.HasParseError());\
42 internal::Hasher<Value, CrtAllocator> h1, h2;\
43 d1.Accept(h1);\
44 d2.Accept(h2);\
45 ASSERT_TRUE(h1.IsValid());\
46 ASSERT_TRUE(h2.IsValid());\
47 /*printf("%s: 0x%016llx\n%s: 0x%016llx\n\n", json1, h1.GetHashCode(), json2, h2.GetHashCode());*/\
48 EXPECT_TRUE(expected == (h1.GetHashCode() == h2.GetHashCode()));\
49 }
50
51 TEST(SchemaValidator, Hasher) {
52 TEST_HASHER("null", "null", true);
53
54 TEST_HASHER("true", "true", true);
55 TEST_HASHER("false", "false", true);
56 TEST_HASHER("true", "false", false);
57 TEST_HASHER("false", "true", false);
58 TEST_HASHER("true", "null", false);
59 TEST_HASHER("false", "null", false);
60
61 TEST_HASHER("1", "1", true);
62 TEST_HASHER("2147483648", "2147483648", true); // 2^31 can only be fit in unsigned
63 TEST_HASHER("-2147483649", "-2147483649", true); // -2^31 - 1 can only be fit in int64_t
64 TEST_HASHER("2147483648", "2147483648", true); // 2^31 can only be fit in unsigned
65 TEST_HASHER("4294967296", "4294967296", true); // 2^32 can only be fit in int64_t
66 TEST_HASHER("9223372036854775808", "9223372036854775808", true); // 2^63 can only be fit in uint64_t
67 TEST_HASHER("1.5", "1.5", true);
68 TEST_HASHER("1", "1.0", true);
69 TEST_HASHER("1", "-1", false);
70 TEST_HASHER("0.0", "-0.0", false);
71 TEST_HASHER("1", "true", false);
72 TEST_HASHER("0", "false", false);
73 TEST_HASHER("0", "null", false);
74
75 TEST_HASHER("\"\"", "\"\"", true);
76 TEST_HASHER("\"\"", "\"\\u0000\"", false);
77 TEST_HASHER("\"Hello\"", "\"Hello\"", true);
78 TEST_HASHER("\"Hello\"", "\"World\"", false);
79 TEST_HASHER("\"Hello\"", "null", false);
80 TEST_HASHER("\"Hello\\u0000\"", "\"Hello\"", false);
81 TEST_HASHER("\"\"", "null", false);
82 TEST_HASHER("\"\"", "true", false);
83 TEST_HASHER("\"\"", "false", false);
84
85 TEST_HASHER("[]", "[ ]", true);
86 TEST_HASHER("[1, true, false]", "[1, true, false]", true);
87 TEST_HASHER("[1, true, false]", "[1, true]", false);
88 TEST_HASHER("[1, 2]", "[2, 1]", false);
89 TEST_HASHER("[[1], 2]", "[[1, 2]]", false);
90 TEST_HASHER("[1, 2]", "[1, [2]]", false);
91 TEST_HASHER("[]", "null", false);
92 TEST_HASHER("[]", "true", false);
93 TEST_HASHER("[]", "false", false);
94 TEST_HASHER("[]", "0", false);
95 TEST_HASHER("[]", "0.0", false);
96 TEST_HASHER("[]", "\"\"", false);
97
98 TEST_HASHER("{}", "{ }", true);
99 TEST_HASHER("{\"a\":1}", "{\"a\":1}", true);
100 TEST_HASHER("{\"a\":1}", "{\"b\":1}", false);
101 TEST_HASHER("{\"a\":1}", "{\"a\":2}", false);
102 TEST_HASHER("{\"a\":1, \"b\":2}", "{\"b\":2, \"a\":1}", true); // Member order insensitive
103 TEST_HASHER("{}", "null", false);
104 TEST_HASHER("{}", "false", false);
105 TEST_HASHER("{}", "true", false);
106 TEST_HASHER("{}", "0", false);
107 TEST_HASHER("{}", "0.0", false);
108 TEST_HASHER("{}", "\"\"", false);
109 }
110
111 // Test cases following http://spacetelescope.github.io/understanding-json-schema
112
113 #define VALIDATE(schema, json, expected) \
114 {\
115 VALIDATE_(schema, json, expected, true) \
116 }
117
118 #define VALIDATE_(schema, json, expected, expected2) \
119 {\
120 EXPECT_TRUE(expected2 == schema.GetError().ObjectEmpty());\
121 EXPECT_TRUE(schema.IsSupportedSpecification());\
122 SchemaValidator validator(schema);\
123 Document d;\
124 /*printf("\n%s\n", json);*/\
125 d.Parse(json);\
126 EXPECT_FALSE(d.HasParseError());\
127 EXPECT_TRUE(expected == d.Accept(validator));\
128 EXPECT_TRUE(expected == validator.IsValid());\
129 ValidateErrorCode code = validator.GetInvalidSchemaCode();\
130 if (expected) {\
131 EXPECT_TRUE(code == kValidateErrorNone);\
132 EXPECT_TRUE(validator.GetInvalidSchemaKeyword() == 0);\
133 }\
134 if ((expected) && !validator.IsValid()) {\
135 StringBuffer sb;\
136 validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);\
137 printf("Invalid schema: %s\n", sb.GetString());\
138 printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());\
139 printf("Invalid code: %d\n", code);\
140 printf("Invalid message: %s\n", GetValidateError_En(code));\
141 sb.Clear();\
142 validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);\
143 printf("Invalid document: %s\n", sb.GetString());\
144 sb.Clear();\
145 Writer<StringBuffer> w(sb);\
146 validator.GetError().Accept(w);\
147 printf("Validation error: %s\n", sb.GetString());\
148 }\
149 }
150
151 #define INVALIDATE(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer, error) \
152 {\
153 INVALIDATE_(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer, error, kValidateDefaultFlags, SchemaValidator, Pointer) \
154 }
155
156 #define INVALIDATE_(schema, json, invalidSchemaPointer, invalidSchemaKeyword, invalidDocumentPointer, error, \
157 flags, SchemaValidatorType, PointerType) \
158 {\
159 EXPECT_TRUE(schema.GetError().ObjectEmpty());\
160 EXPECT_TRUE(schema.IsSupportedSpecification());\
161 SchemaValidatorType validator(schema);\
162 validator.SetValidateFlags(flags);\
163 Document d;\
164 /*printf("\n%s\n", json);*/\
165 d.Parse(json);\
166 EXPECT_FALSE(d.HasParseError());\
167 d.Accept(validator);\
168 EXPECT_FALSE(validator.IsValid());\
169 ValidateErrorCode code = validator.GetInvalidSchemaCode();\
170 ASSERT_TRUE(code != kValidateErrorNone);\
171 ASSERT_TRUE(strcmp(GetValidateError_En(code), "Unknown error.") != 0);\
172 if (validator.GetInvalidSchemaPointer() != PointerType(invalidSchemaPointer)) {\
173 StringBuffer sb;\
174 validator.GetInvalidSchemaPointer().Stringify(sb);\
175 printf("GetInvalidSchemaPointer() Expected: %s Actual: %s\n", invalidSchemaPointer, sb.GetString());\
176 ADD_FAILURE();\
177 }\
178 ASSERT_TRUE(validator.GetInvalidSchemaKeyword() != 0);\
179 if (strcmp(validator.GetInvalidSchemaKeyword(), invalidSchemaKeyword) != 0) {\
180 printf("GetInvalidSchemaKeyword() Expected: %s Actual %s\n", invalidSchemaKeyword, validator.GetInvalidSchemaKeyword());\
181 ADD_FAILURE();\
182 }\
183 if (validator.GetInvalidDocumentPointer() != PointerType(invalidDocumentPointer)) {\
184 StringBuffer sb;\
185 validator.GetInvalidDocumentPointer().Stringify(sb);\
186 printf("GetInvalidDocumentPointer() Expected: %s Actual: %s\n", invalidDocumentPointer, sb.GetString());\
187 ADD_FAILURE();\
188 }\
189 Document e;\
190 e.Parse(error);\
191 if (validator.GetError() != e) {\
192 StringBuffer sb;\
193 Writer<StringBuffer> w(sb);\
194 validator.GetError().Accept(w);\
195 printf("GetError() Expected: %s Actual: %s\n", error, sb.GetString());\
196 ADD_FAILURE();\
197 }\
198 }
199
200 // Use for checking whether a compiled schema document contains errors
201 #define SCHEMAERROR(schema, error) \
202 {\
203 Document e;\
204 e.Parse(error);\
205 if (schema.GetError() != e) {\
206 StringBuffer sb;\
207 Writer<StringBuffer> w(sb);\
208 schema.GetError().Accept(w);\
209 printf("GetError() Expected: %s Actual: %s\n", error, sb.GetString());\
210 ADD_FAILURE();\
211 }\
212 }
213
214 TEST(SchemaValidator, Typeless) {
215 Document sd;
216 sd.Parse("{}");
217 SchemaDocument s(sd);
218
219 VALIDATE(s, "42", true);
220 VALIDATE(s, "\"I'm a string\"", true);
221 VALIDATE(s, "{ \"an\": [ \"arbitrarily\", \"nested\" ], \"data\": \"structure\" }", true);
222 }
223
224 TEST(SchemaValidator, MultiType) {
225 Document sd;
226 sd.Parse("{ \"type\": [\"number\", \"string\"] }");
227 SchemaDocument s(sd);
228
229 VALIDATE(s, "42", true);
230 VALIDATE(s, "\"Life, the universe, and everything\"", true);
231 INVALIDATE(s, "[\"Life\", \"the universe\", \"and everything\"]", "", "type", "",
232 "{ \"type\": {"
233 " \"errorCode\": 20,"
234 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
235 " \"expected\": [\"string\", \"number\"], \"actual\": \"array\""
236 "}}");
237 }
238
239 TEST(SchemaValidator, Enum_Typed) {
240 Document sd;
241 sd.Parse("{ \"type\": \"string\", \"enum\" : [\"red\", \"amber\", \"green\"] }");
242 SchemaDocument s(sd);
243
244 VALIDATE(s, "\"red\"", true);
245 INVALIDATE(s, "\"blue\"", "", "enum", "",
246 "{ \"enum\": { \"errorCode\": 19, \"instanceRef\": \"#\", \"schemaRef\": \"#\" }}");
247 }
248
249 TEST(SchemaValidator, Enum_Typeless) {
250 Document sd;
251 sd.Parse("{ \"enum\": [\"red\", \"amber\", \"green\", null, 42] }");
252 SchemaDocument s(sd);
253
254 VALIDATE(s, "\"red\"", true);
255 VALIDATE(s, "null", true);
256 VALIDATE(s, "42", true);
257 INVALIDATE(s, "0", "", "enum", "",
258 "{ \"enum\": { \"errorCode\": 19, \"instanceRef\": \"#\", \"schemaRef\": \"#\" }}");
259 }
260
261 TEST(SchemaValidator, Enum_InvalidType) {
262 Document sd;
263 sd.Parse("{ \"type\": \"string\", \"enum\": [\"red\", \"amber\", \"green\", null] }");
264 SchemaDocument s(sd);
265
266 VALIDATE(s, "\"red\"", true);
267 INVALIDATE(s, "null", "", "type", "",
268 "{ \"type\": {"
269 " \"errorCode\": 20,"
270 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
271 " \"expected\": [\"string\"], \"actual\": \"null\""
272 "}}");
273 }
274
275 TEST(SchemaValidator, AllOf) {
276 {
277 Document sd;
278 sd.Parse("{\"allOf\": [{ \"type\": \"string\" }, { \"type\": \"string\", \"maxLength\": 5 }]}");
279 SchemaDocument s(sd);
280
281 VALIDATE(s, "\"ok\"", true);
282 INVALIDATE(s, "\"too long\"", "", "allOf", "",
283 "{ \"allOf\": {"
284 " \"errors\": ["
285 " {},"
286 " {\"maxLength\": {\"errorCode\": 6, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/1\", \"expected\": 5, \"actual\": \"too long\"}}"
287 " ],"
288 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
289 "}}");
290 }
291 {
292 Document sd;
293 sd.Parse("{\"allOf\": [{ \"type\": \"string\" }, { \"type\": \"number\" } ] }");
294 SchemaDocument s(sd);
295
296 VALIDATE(s, "\"No way\"", false);
297 INVALIDATE(s, "-1", "", "allOf", "",
298 "{ \"allOf\": {"
299 " \"errors\": ["
300 " {\"type\": { \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/0\", \"errorCode\": 20, \"expected\": [\"string\"], \"actual\": \"integer\"}},"
301 " {}"
302 " ],"
303 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
304 "}}");
305 }
306 }
307
308 TEST(SchemaValidator, AnyOf) {
309 Document sd;
310 sd.Parse("{\"anyOf\": [{ \"type\": \"string\" }, { \"type\": \"number\" } ] }");
311 SchemaDocument s(sd);
312
313 VALIDATE(s, "\"Yes\"", true);
314 VALIDATE(s, "42", true);
315 INVALIDATE(s, "{ \"Not a\": \"string or number\" }", "", "anyOf", "",
316 "{ \"anyOf\": {"
317 " \"errorCode\": 24,"
318 " \"instanceRef\": \"#\", \"schemaRef\": \"#\", "
319 " \"errors\": ["
320 " { \"type\": {"
321 " \"errorCode\": 20,"
322 " \"instanceRef\": \"#\", \"schemaRef\": \"#/anyOf/0\","
323 " \"expected\": [\"string\"], \"actual\": \"object\""
324 " }},"
325 " { \"type\": {"
326 " \"errorCode\": 20,"
327 " \"instanceRef\": \"#\", \"schemaRef\": \"#/anyOf/1\","
328 " \"expected\": [\"number\"], \"actual\": \"object\""
329 " }}"
330 " ]"
331 "}}");
332 }
333
334 TEST(SchemaValidator, OneOf) {
335 Document sd;
336 sd.Parse("{\"oneOf\": [{ \"type\": \"number\", \"multipleOf\": 5 }, { \"type\": \"number\", \"multipleOf\": 3 } ] }");
337 SchemaDocument s(sd);
338
339 VALIDATE(s, "10", true);
340 VALIDATE(s, "9", true);
341 INVALIDATE(s, "2", "", "oneOf", "",
342 "{ \"oneOf\": {"
343 " \"errorCode\": 21,"
344 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
345 " \"errors\": ["
346 " { \"multipleOf\": {"
347 " \"errorCode\": 1,"
348 " \"instanceRef\": \"#\", \"schemaRef\": \"#/oneOf/0\","
349 " \"expected\": 5, \"actual\": 2"
350 " }},"
351 " { \"multipleOf\": {"
352 " \"errorCode\": 1,"
353 " \"instanceRef\": \"#\", \"schemaRef\": \"#/oneOf/1\","
354 " \"expected\": 3, \"actual\": 2"
355 " }}"
356 " ]"
357 "}}");
358 INVALIDATE(s, "15", "", "oneOf", "",
359 "{ \"oneOf\": { \"errorCode\": 22, \"instanceRef\": \"#\", \"schemaRef\": \"#\", \"matches\": [0,1]}}");
360 }
361
362 TEST(SchemaValidator, Not) {
363 Document sd;
364 sd.Parse("{\"not\":{ \"type\": \"string\"}}");
365 SchemaDocument s(sd);
366
367 VALIDATE(s, "42", true);
368 VALIDATE(s, "{ \"key\": \"value\" }", true);
369 INVALIDATE(s, "\"I am a string\"", "", "not", "",
370 "{ \"not\": { \"errorCode\": 25, \"instanceRef\": \"#\", \"schemaRef\": \"#\" }}");
371 }
372
373 TEST(SchemaValidator, Ref) {
374 Document sd;
375 sd.Parse(
376 "{"
377 " \"$schema\": \"http://json-schema.org/draft-04/schema#\","
378 ""
379 " \"definitions\": {"
380 " \"address\": {"
381 " \"type\": \"object\","
382 " \"properties\": {"
383 " \"street_address\": { \"type\": \"string\" },"
384 " \"city\": { \"type\": \"string\" },"
385 " \"state\": { \"type\": \"string\" }"
386 " },"
387 " \"required\": [\"street_address\", \"city\", \"state\"]"
388 " }"
389 " },"
390 " \"type\": \"object\","
391 " \"properties\": {"
392 " \"billing_address\": { \"$ref\": \"#/definitions/address\" },"
393 " \"shipping_address\": { \"$ref\": \"#/definitions/address\" }"
394 " }"
395 "}");
396 SchemaDocument s(sd);
397
398 VALIDATE(s, "{\"shipping_address\": {\"street_address\": \"1600 Pennsylvania Avenue NW\", \"city\": \"Washington\", \"state\": \"DC\"}, \"billing_address\": {\"street_address\": \"1st Street SE\", \"city\": \"Washington\", \"state\": \"DC\"} }", true);
399 }
400
401 TEST(SchemaValidator, Ref_AllOf) {
402 Document sd;
403 sd.Parse(
404 "{"
405 " \"$schema\": \"http://json-schema.org/draft-04/schema#\","
406 ""
407 " \"definitions\": {"
408 " \"address\": {"
409 " \"type\": \"object\","
410 " \"properties\": {"
411 " \"street_address\": { \"type\": \"string\" },"
412 " \"city\": { \"type\": \"string\" },"
413 " \"state\": { \"type\": \"string\" }"
414 " },"
415 " \"required\": [\"street_address\", \"city\", \"state\"]"
416 " }"
417 " },"
418 " \"type\": \"object\","
419 " \"properties\": {"
420 " \"billing_address\": { \"$ref\": \"#/definitions/address\" },"
421 " \"shipping_address\": {"
422 " \"allOf\": ["
423 " { \"$ref\": \"#/definitions/address\" },"
424 " { \"properties\":"
425 " { \"type\": { \"enum\": [ \"residential\", \"business\" ] } },"
426 " \"required\": [\"type\"]"
427 " }"
428 " ]"
429 " }"
430 " }"
431 "}");
432 SchemaDocument s(sd);
433
434 INVALIDATE(s, "{\"shipping_address\": {\"street_address\": \"1600 Pennsylvania Avenue NW\", \"city\": \"Washington\", \"state\": \"DC\"} }", "/properties/shipping_address", "allOf", "/shipping_address",
435 "{ \"allOf\": {"
436 " \"errors\": ["
437 " {},"
438 " {\"required\": {\"errorCode\": 15, \"instanceRef\": \"#/shipping_address\", \"schemaRef\": \"#/properties/shipping_address/allOf/1\", \"missing\": [\"type\"]}}"
439 " ],"
440 " \"errorCode\":23,\"instanceRef\":\"#/shipping_address\",\"schemaRef\":\"#/properties/shipping_address\""
441 "}}");
442 VALIDATE(s, "{\"shipping_address\": {\"street_address\": \"1600 Pennsylvania Avenue NW\", \"city\": \"Washington\", \"state\": \"DC\", \"type\": \"business\"} }", true);
443 }
444
445 TEST(SchemaValidator, String) {
446 Document sd;
447 sd.Parse("{\"type\":\"string\"}");
448 SchemaDocument s(sd);
449
450 VALIDATE(s, "\"I'm a string\"", true);
451 INVALIDATE(s, "42", "", "type", "",
452 "{ \"type\": {"
453 " \"errorCode\": 20,"
454 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
455 " \"expected\": [\"string\"], \"actual\": \"integer\""
456 "}}");
457 INVALIDATE(s, "2147483648", "", "type", "",
458 "{ \"type\": {"
459 " \"errorCode\": 20,"
460 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
461 " \"expected\": [\"string\"], \"actual\": \"integer\""
462 "}}"); // 2^31 can only be fit in unsigned
463 INVALIDATE(s, "-2147483649", "", "type", "",
464 "{ \"type\": {"
465 " \"errorCode\": 20,"
466 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
467 " \"expected\": [\"string\"], \"actual\": \"integer\""
468 "}}"); // -2^31 - 1 can only be fit in int64_t
469 INVALIDATE(s, "4294967296", "", "type", "",
470 "{ \"type\": {"
471 " \"errorCode\": 20,"
472 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
473 " \"expected\": [\"string\"], \"actual\": \"integer\""
474 "}}"); // 2^32 can only be fit in int64_t
475 INVALIDATE(s, "3.1415926", "", "type", "",
476 "{ \"type\": {"
477 " \"errorCode\": 20,"
478 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
479 " \"expected\": [\"string\"], \"actual\": \"number\""
480 "}}");
481 }
482
483 TEST(SchemaValidator, String_LengthRange) {
484 Document sd;
485 sd.Parse("{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}");
486 SchemaDocument s(sd);
487
488 INVALIDATE(s, "\"A\"", "", "minLength", "",
489 "{ \"minLength\": {"
490 " \"errorCode\": 7,"
491 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
492 " \"expected\": 2, \"actual\": \"A\""
493 "}}");
494 VALIDATE(s, "\"AB\"", true);
495 VALIDATE(s, "\"ABC\"", true);
496 INVALIDATE(s, "\"ABCD\"", "", "maxLength", "",
497 "{ \"maxLength\": {"
498 " \"errorCode\": 6,"
499 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
500 " \"expected\": 3, \"actual\": \"ABCD\""
501 "}}");
502 }
503
504 #if RAPIDJSON_SCHEMA_HAS_REGEX
505 TEST(SchemaValidator, String_Pattern) {
506 Document sd;
507 sd.Parse("{\"type\":\"string\",\"pattern\":\"^(\\\\([0-9]{3}\\\\))?[0-9]{3}-[0-9]{4}$\"}");
508 SchemaDocument s(sd);
509
510 VALIDATE(s, "\"555-1212\"", true);
511 VALIDATE(s, "\"(888)555-1212\"", true);
512 INVALIDATE(s, "\"(888)555-1212 ext. 532\"", "", "pattern", "",
513 "{ \"pattern\": {"
514 " \"errorCode\": 8,"
515 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
516 " \"actual\": \"(888)555-1212 ext. 532\""
517 "}}");
518 INVALIDATE(s, "\"(800)FLOWERS\"", "", "pattern", "",
519 "{ \"pattern\": {"
520 " \"errorCode\": 8,"
521 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
522 " \"actual\": \"(800)FLOWERS\""
523 "}}");
524 }
525
526 TEST(SchemaValidator, String_Pattern_Invalid) {
527 Document sd;
528 sd.Parse("{\"type\":\"string\",\"pattern\":\"a{0}\"}");
529 SchemaDocument s(sd);
530 SCHEMAERROR(s, "{\"RegexInvalid\":{\"errorCode\":9,\"instanceRef\":\"#/pattern\",\"value\":\"a{0}\"}}");
531
532 VALIDATE_(s, "\"\"", true, false);
533 VALIDATE_(s, "\"a\"", true, false);
534 VALIDATE_(s, "\"aa\"", true, false);
535 }
536 #endif
537
538 TEST(SchemaValidator, Integer) {
539 Document sd;
540 sd.Parse("{\"type\":\"integer\"}");
541 SchemaDocument s(sd);
542
543 VALIDATE(s, "42", true);
544 VALIDATE(s, "-1", true);
545 VALIDATE(s, "2147483648", true); // 2^31 can only be fit in unsigned
546 VALIDATE(s, "-2147483649", true); // -2^31 - 1 can only be fit in int64_t
547 VALIDATE(s, "2147483648", true); // 2^31 can only be fit in unsigned
548 VALIDATE(s, "4294967296", true); // 2^32 can only be fit in int64_t
549 INVALIDATE(s, "3.1415926", "", "type", "",
550 "{ \"type\": {"
551 " \"errorCode\": 20,"
552 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
553 " \"expected\": [\"integer\"], \"actual\": \"number\""
554 "}}");
555 INVALIDATE(s, "\"42\"", "", "type", "",
556 "{ \"type\": {"
557 " \"errorCode\": 20,"
558 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
559 " \"expected\": [\"integer\"], \"actual\": \"string\""
560 "}}");
561 }
562
563 TEST(SchemaValidator, Integer_Range) {
564 Document sd;
565 sd.Parse("{\"type\":\"integer\",\"minimum\":0,\"maximum\":100,\"exclusiveMaximum\":true}");
566 SchemaDocument s(sd);
567
568 INVALIDATE(s, "-1", "", "minimum", "",
569 "{ \"minimum\": {"
570 " \"errorCode\": 4,"
571 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
572 " \"expected\": 0, \"actual\": -1"
573 "}}");
574 VALIDATE(s, "0", true);
575 VALIDATE(s, "10", true);
576 VALIDATE(s, "99", true);
577 INVALIDATE(s, "100", "", "maximum", "",
578 "{ \"maximum\": {"
579 " \"errorCode\": 3,"
580 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
581 " \"expected\": 100, \"exclusiveMaximum\": true, \"actual\": 100"
582 "}}");
583 INVALIDATE(s, "101", "", "maximum", "",
584 "{ \"maximum\": {"
585 " \"errorCode\": 3,"
586 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
587 " \"expected\": 100, \"exclusiveMaximum\": true, \"actual\": 101"
588 "}}");
589 }
590
591 TEST(SchemaValidator, Integer_Range64Boundary) {
592 Document sd;
593 sd.Parse("{\"type\":\"integer\",\"minimum\":-9223372036854775807,\"maximum\":9223372036854775806}");
594 SchemaDocument s(sd);
595
596 INVALIDATE(s, "-9223372036854775808", "", "minimum", "",
597 "{ \"minimum\": {"
598 " \"errorCode\": 4,"
599 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
600 " \"expected\": -9223372036854775807, \"actual\": -9223372036854775808"
601 "}}");
602 VALIDATE(s, "-9223372036854775807", true);
603 VALIDATE(s, "-2147483648", true); // int min
604 VALIDATE(s, "0", true);
605 VALIDATE(s, "2147483647", true); // int max
606 VALIDATE(s, "2147483648", true); // unsigned first
607 VALIDATE(s, "4294967295", true); // unsigned max
608 VALIDATE(s, "9223372036854775806", true);
609 INVALIDATE(s, "9223372036854775807", "", "maximum", "",
610 "{ \"maximum\": {"
611 " \"errorCode\": 2,"
612 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
613 " \"expected\": 9223372036854775806, \"actual\": 9223372036854775807"
614 "}}");
615 INVALIDATE(s, "18446744073709551615", "", "maximum", "",
616 "{ \"maximum\": {"
617 " \"errorCode\": 2,"
618 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
619 " \"expected\": 9223372036854775806, \"actual\": 18446744073709551615"
620 "}}"); // uint64_t max
621 }
622
623 TEST(SchemaValidator, Integer_RangeU64Boundary) {
624 Document sd;
625 sd.Parse("{\"type\":\"integer\",\"minimum\":9223372036854775808,\"maximum\":18446744073709551614}");
626 SchemaDocument s(sd);
627
628 INVALIDATE(s, "-9223372036854775808", "", "minimum", "",
629 "{ \"minimum\": {"
630 " \"errorCode\": 4,"
631 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
632 " \"expected\": 9223372036854775808, \"actual\": -9223372036854775808"
633 "}}");
634 INVALIDATE(s, "9223372036854775807", "", "minimum", "",
635 "{ \"minimum\": {"
636 " \"errorCode\": 4,"
637 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
638 " \"expected\": 9223372036854775808, \"actual\": 9223372036854775807"
639 "}}");
640 INVALIDATE(s, "-2147483648", "", "minimum", "",
641 "{ \"minimum\": {"
642 " \"errorCode\": 4,"
643 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
644 " \"expected\": 9223372036854775808, \"actual\": -2147483648"
645 "}}"); // int min
646 INVALIDATE(s, "0", "", "minimum", "",
647 "{ \"minimum\": {"
648 " \"errorCode\": 4,"
649 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
650 " \"expected\": 9223372036854775808, \"actual\": 0"
651 "}}");
652 INVALIDATE(s, "2147483647", "", "minimum", "",
653 "{ \"minimum\": {"
654 " \"errorCode\": 4,"
655 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
656 " \"expected\": 9223372036854775808, \"actual\": 2147483647"
657 "}}"); // int max
658 INVALIDATE(s, "2147483648", "", "minimum", "",
659 "{ \"minimum\": {"
660 " \"errorCode\": 4,"
661 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
662 " \"expected\": 9223372036854775808, \"actual\": 2147483648"
663 "}}"); // unsigned first
664 INVALIDATE(s, "4294967295", "", "minimum", "",
665 "{ \"minimum\": {"
666 " \"errorCode\": 4,"
667 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
668 " \"expected\": 9223372036854775808, \"actual\": 4294967295"
669 "}}"); // unsigned max
670 VALIDATE(s, "9223372036854775808", true);
671 VALIDATE(s, "18446744073709551614", true);
672 INVALIDATE(s, "18446744073709551615", "", "maximum", "",
673 "{ \"maximum\": {"
674 " \"errorCode\": 2,"
675 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
676 " \"expected\": 18446744073709551614, \"actual\": 18446744073709551615"
677 "}}");
678 }
679
680 TEST(SchemaValidator, Integer_Range64BoundaryExclusive) {
681 Document sd;
682 sd.Parse("{\"type\":\"integer\",\"minimum\":-9223372036854775808,\"maximum\":18446744073709551615,\"exclusiveMinimum\":true,\"exclusiveMaximum\":true}");
683 SchemaDocument s(sd);
684
685 INVALIDATE(s, "-9223372036854775808", "", "minimum", "",
686 "{ \"minimum\": {"
687 " \"errorCode\": 5,"
688 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
689 " \"expected\": -9223372036854775808, \"exclusiveMinimum\": true, "
690 " \"actual\": -9223372036854775808"
691 "}}");
692 VALIDATE(s, "-9223372036854775807", true);
693 VALIDATE(s, "18446744073709551614", true);
694 INVALIDATE(s, "18446744073709551615", "", "maximum", "",
695 "{ \"maximum\": {"
696 " \"errorCode\": 3,"
697 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
698 " \"expected\": 18446744073709551615, \"exclusiveMaximum\": true, "
699 " \"actual\": 18446744073709551615"
700 "}}");
701 }
702
703 TEST(SchemaValidator, Integer_MultipleOf) {
704 Document sd;
705 sd.Parse("{\"type\":\"integer\",\"multipleOf\":10}");
706 SchemaDocument s(sd);
707
708 VALIDATE(s, "0", true);
709 VALIDATE(s, "10", true);
710 VALIDATE(s, "-10", true);
711 VALIDATE(s, "20", true);
712 INVALIDATE(s, "23", "", "multipleOf", "",
713 "{ \"multipleOf\": {"
714 " \"errorCode\": 1,"
715 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
716 " \"expected\": 10, \"actual\": 23"
717 "}}");
718 INVALIDATE(s, "-23", "", "multipleOf", "",
719 "{ \"multipleOf\": {"
720 " \"errorCode\": 1,"
721 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
722 " \"expected\": 10, \"actual\": -23"
723 "}}");
724 }
725
726 TEST(SchemaValidator, Integer_MultipleOf64Boundary) {
727 Document sd;
728 sd.Parse("{\"type\":\"integer\",\"multipleOf\":18446744073709551615}");
729 SchemaDocument s(sd);
730
731 VALIDATE(s, "0", true);
732 VALIDATE(s, "18446744073709551615", true);
733 INVALIDATE(s, "18446744073709551614", "", "multipleOf", "",
734 "{ \"multipleOf\": {"
735 " \"errorCode\": 1,"
736 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
737 " \"expected\": 18446744073709551615, \"actual\": 18446744073709551614"
738 "}}");
739 }
740
741 TEST(SchemaValidator, Number_Range) {
742 Document sd;
743 sd.Parse("{\"type\":\"number\",\"minimum\":0,\"maximum\":100,\"exclusiveMaximum\":true}");
744 SchemaDocument s(sd);
745
746 INVALIDATE(s, "-1", "", "minimum", "",
747 "{ \"minimum\": {"
748 " \"errorCode\": 4,"
749 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
750 " \"expected\": 0, \"actual\": -1"
751 "}}");
752 VALIDATE(s, "0", true);
753 VALIDATE(s, "0.1", true);
754 VALIDATE(s, "10", true);
755 VALIDATE(s, "99", true);
756 VALIDATE(s, "99.9", true);
757 INVALIDATE(s, "100", "", "maximum", "",
758 "{ \"maximum\": {"
759 " \"errorCode\": 3,"
760 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
761 " \"expected\": 100, \"exclusiveMaximum\": true, \"actual\": 100"
762 "}}");
763 INVALIDATE(s, "100.0", "", "maximum", "",
764 "{ \"maximum\": {"
765 " \"errorCode\": 3,"
766 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
767 " \"expected\": 100, \"exclusiveMaximum\": true, \"actual\": 100.0"
768 "}}");
769 INVALIDATE(s, "101.5", "", "maximum", "",
770 "{ \"maximum\": {"
771 " \"errorCode\": 3,"
772 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
773 " \"expected\": 100, \"exclusiveMaximum\": true, \"actual\": 101.5"
774 "}}");
775 }
776
777 TEST(SchemaValidator, Number_RangeInt) {
778 Document sd;
779 sd.Parse("{\"type\":\"number\",\"minimum\":-100,\"maximum\":-1,\"exclusiveMaximum\":true}");
780 SchemaDocument s(sd);
781
782 INVALIDATE(s, "-101", "", "minimum", "",
783 "{ \"minimum\": {"
784 " \"errorCode\": 4,"
785 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
786 " \"expected\": -100, \"actual\": -101"
787 "}}");
788 INVALIDATE(s, "-100.1", "", "minimum", "",
789 "{ \"minimum\": {"
790 " \"errorCode\": 4,"
791 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
792 " \"expected\": -100, \"actual\": -100.1"
793 "}}");
794 VALIDATE(s, "-100", true);
795 VALIDATE(s, "-2", true);
796 INVALIDATE(s, "-1", "", "maximum", "",
797 "{ \"maximum\": {"
798 " \"errorCode\": 3,"
799 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
800 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": -1"
801 "}}");
802 INVALIDATE(s, "-0.9", "", "maximum", "",
803 "{ \"maximum\": {"
804 " \"errorCode\": 3,"
805 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
806 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": -0.9"
807 "}}");
808 INVALIDATE(s, "0", "", "maximum", "",
809 "{ \"maximum\": {"
810 " \"errorCode\": 3,"
811 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
812 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": 0"
813 "}}");
814 INVALIDATE(s, "2147483647", "", "maximum", "",
815 "{ \"maximum\": {"
816 " \"errorCode\": 3,"
817 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
818 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": 2147483647"
819 "}}"); // int max
820 INVALIDATE(s, "2147483648", "", "maximum", "",
821 "{ \"maximum\": {"
822 " \"errorCode\": 3,"
823 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
824 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": 2147483648"
825 "}}"); // unsigned first
826 INVALIDATE(s, "4294967295", "", "maximum", "",
827 "{ \"maximum\": {"
828 " \"errorCode\": 3,"
829 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
830 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": 4294967295"
831 "}}"); // unsigned max
832 INVALIDATE(s, "9223372036854775808", "", "maximum", "",
833 "{ \"maximum\": {"
834 " \"errorCode\": 3,"
835 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
836 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": 9223372036854775808"
837 "}}");
838 INVALIDATE(s, "18446744073709551614", "", "maximum", "",
839 "{ \"maximum\": {"
840 " \"errorCode\": 3,"
841 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
842 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": 18446744073709551614"
843 "}}");
844 INVALIDATE(s, "18446744073709551615", "", "maximum", "",
845 "{ \"maximum\": {"
846 " \"errorCode\": 3,"
847 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
848 " \"expected\": -1, \"exclusiveMaximum\": true, \"actual\": 18446744073709551615"
849 "}}");
850 }
851
852 TEST(SchemaValidator, Number_RangeDouble) {
853 Document sd;
854 sd.Parse("{\"type\":\"number\",\"minimum\":0.1,\"maximum\":100.1,\"exclusiveMaximum\":true}");
855 SchemaDocument s(sd);
856
857 INVALIDATE(s, "-9223372036854775808", "", "minimum", "",
858 "{ \"minimum\": {"
859 " \"errorCode\": 4,"
860 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
861 " \"expected\": 0.1, \"actual\": -9223372036854775808"
862 "}}");
863 INVALIDATE(s, "-2147483648", "", "minimum", "",
864 "{ \"minimum\": {"
865 " \"errorCode\": 4,"
866 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
867 " \"expected\": 0.1, \"actual\": -2147483648"
868 "}}"); // int min
869 INVALIDATE(s, "-1", "", "minimum", "",
870 "{ \"minimum\": {"
871 " \"errorCode\": 4,"
872 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
873 " \"expected\": 0.1, \"actual\": -1"
874 "}}");
875 VALIDATE(s, "0.1", true);
876 VALIDATE(s, "10", true);
877 VALIDATE(s, "99", true);
878 VALIDATE(s, "100", true);
879 INVALIDATE(s, "101", "", "maximum", "",
880 "{ \"maximum\": {"
881 " \"errorCode\": 3,"
882 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
883 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 101"
884 "}}");
885 INVALIDATE(s, "101.5", "", "maximum", "",
886 "{ \"maximum\": {"
887 " \"errorCode\": 3,"
888 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
889 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 101.5"
890 "}}");
891 INVALIDATE(s, "18446744073709551614", "", "maximum", "",
892 "{ \"maximum\": {"
893 " \"errorCode\": 3,"
894 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
895 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 18446744073709551614"
896 "}}");
897 INVALIDATE(s, "18446744073709551615", "", "maximum", "",
898 "{ \"maximum\": {"
899 " \"errorCode\": 3,"
900 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
901 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 18446744073709551615"
902 "}}");
903 INVALIDATE(s, "2147483647", "", "maximum", "",
904 "{ \"maximum\": {"
905 " \"errorCode\": 3,"
906 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
907 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 2147483647"
908 "}}"); // int max
909 INVALIDATE(s, "2147483648", "", "maximum", "",
910 "{ \"maximum\": {"
911 " \"errorCode\": 3,"
912 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
913 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 2147483648"
914 "}}"); // unsigned first
915 INVALIDATE(s, "4294967295", "", "maximum", "",
916 "{ \"maximum\": {"
917 " \"errorCode\": 3,"
918 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
919 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 4294967295"
920 "}}"); // unsigned max
921 INVALIDATE(s, "9223372036854775808", "", "maximum", "",
922 "{ \"maximum\": {"
923 " \"errorCode\": 3,"
924 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
925 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 9223372036854775808"
926 "}}");
927 INVALIDATE(s, "18446744073709551614", "", "maximum", "",
928 "{ \"maximum\": {"
929 " \"errorCode\": 3,"
930 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
931 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 18446744073709551614"
932 "}}");
933 INVALIDATE(s, "18446744073709551615", "", "maximum", "",
934 "{ \"maximum\": {"
935 " \"errorCode\": 3,"
936 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
937 " \"expected\": 100.1, \"exclusiveMaximum\": true, \"actual\": 18446744073709551615"
938 "}}");
939 }
940
941 TEST(SchemaValidator, Number_RangeDoubleU64Boundary) {
942 Document sd;
943 sd.Parse("{\"type\":\"number\",\"minimum\":9223372036854775808.0,\"maximum\":18446744073709550000.0}");
944 SchemaDocument s(sd);
945
946 INVALIDATE(s, "-9223372036854775808", "", "minimum", "",
947 "{ \"minimum\": {"
948 " \"errorCode\": 4,"
949 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
950 " \"expected\": 9223372036854775808.0, \"actual\": -9223372036854775808"
951 "}}");
952 INVALIDATE(s, "-2147483648", "", "minimum", "",
953 "{ \"minimum\": {"
954 " \"errorCode\": 4,"
955 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
956 " \"expected\": 9223372036854775808.0, \"actual\": -2147483648"
957 "}}"); // int min
958 INVALIDATE(s, "0", "", "minimum", "",
959 "{ \"minimum\": {"
960 " \"errorCode\": 4,"
961 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
962 " \"expected\": 9223372036854775808.0, \"actual\": 0"
963 "}}");
964 INVALIDATE(s, "2147483647", "", "minimum", "",
965 "{ \"minimum\": {"
966 " \"errorCode\": 4,"
967 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
968 " \"expected\": 9223372036854775808.0, \"actual\": 2147483647"
969 "}}"); // int max
970 INVALIDATE(s, "2147483648", "", "minimum", "",
971 "{ \"minimum\": {"
972 " \"errorCode\": 4,"
973 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
974 " \"expected\": 9223372036854775808.0, \"actual\": 2147483648"
975 "}}"); // unsigned first
976 INVALIDATE(s, "4294967295", "", "minimum", "",
977 "{ \"minimum\": {"
978 " \"errorCode\": 4,"
979 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
980 " \"expected\": 9223372036854775808.0, \"actual\": 4294967295"
981 "}}"); // unsigned max
982 VALIDATE(s, "9223372036854775808", true);
983 VALIDATE(s, "18446744073709540000", true);
984 INVALIDATE(s, "18446744073709551615", "", "maximum", "",
985 "{ \"maximum\": {"
986 " \"errorCode\": 2,"
987 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
988 " \"expected\": 18446744073709550000.0, \"actual\": 18446744073709551615"
989 "}}");
990 }
991
992 TEST(SchemaValidator, Number_MultipleOf) {
993 Document sd;
994 sd.Parse("{\"type\":\"number\",\"multipleOf\":10.0}");
995 SchemaDocument s(sd);
996
997 VALIDATE(s, "0", true);
998 VALIDATE(s, "10", true);
999 VALIDATE(s, "-10", true);
1000 VALIDATE(s, "20", true);
1001 INVALIDATE(s, "23", "", "multipleOf", "",
1002 "{ \"multipleOf\": {"
1003 " \"errorCode\": 1,"
1004 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1005 " \"expected\": 10.0, \"actual\": 23"
1006 "}}");
1007 INVALIDATE(s, "-2147483648", "", "multipleOf", "",
1008 "{ \"multipleOf\": {"
1009 " \"errorCode\": 1,"
1010 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1011 " \"expected\": 10.0, \"actual\": -2147483648"
1012 "}}"); // int min
1013 VALIDATE(s, "-2147483640", true);
1014 INVALIDATE(s, "2147483647", "", "multipleOf", "",
1015 "{ \"multipleOf\": {"
1016 " \"errorCode\": 1,"
1017 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1018 " \"expected\": 10.0, \"actual\": 2147483647"
1019 "}}"); // int max
1020 INVALIDATE(s, "2147483648", "", "multipleOf", "",
1021 "{ \"multipleOf\": {"
1022 " \"errorCode\": 1,"
1023 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1024 " \"expected\": 10.0, \"actual\": 2147483648"
1025 "}}"); // unsigned first
1026 VALIDATE(s, "2147483650", true);
1027 INVALIDATE(s, "4294967295", "", "multipleOf", "",
1028 "{ \"multipleOf\": {"
1029 " \"errorCode\": 1,"
1030 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1031 " \"expected\": 10.0, \"actual\": 4294967295"
1032 "}}"); // unsigned max
1033 VALIDATE(s, "4294967300", true);
1034 }
1035
1036 TEST(SchemaValidator, Number_MultipleOfOne) {
1037 Document sd;
1038 sd.Parse("{\"type\":\"number\",\"multipleOf\":1}");
1039 SchemaDocument s(sd);
1040
1041 VALIDATE(s, "42", true);
1042 VALIDATE(s, "42.0", true);
1043 INVALIDATE(s, "3.1415926", "", "multipleOf", "",
1044 "{ \"multipleOf\": {"
1045 " \"errorCode\": 1,"
1046 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1047 " \"expected\": 1, \"actual\": 3.1415926"
1048 "}}");
1049 }
1050
1051 TEST(SchemaValidator, Object) {
1052 Document sd;
1053 sd.Parse("{\"type\":\"object\"}");
1054 SchemaDocument s(sd);
1055
1056 VALIDATE(s, "{\"key\":\"value\",\"another_key\":\"another_value\"}", true);
1057 VALIDATE(s, "{\"Sun\":1.9891e30,\"Jupiter\":1.8986e27,\"Saturn\":5.6846e26,\"Neptune\":10.243e25,\"Uranus\":8.6810e25,\"Earth\":5.9736e24,\"Venus\":4.8685e24,\"Mars\":6.4185e23,\"Mercury\":3.3022e23,\"Moon\":7.349e22,\"Pluto\":1.25e22}", true);
1058 INVALIDATE(s, "[\"An\", \"array\", \"not\", \"an\", \"object\"]", "", "type", "",
1059 "{ \"type\": {"
1060 " \"errorCode\": 20,"
1061 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1062 " \"expected\": [\"object\"], \"actual\": \"array\""
1063 "}}");
1064 INVALIDATE(s, "\"Not an object\"", "", "type", "",
1065 "{ \"type\": {"
1066 " \"errorCode\": 20,"
1067 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1068 " \"expected\": [\"object\"], \"actual\": \"string\""
1069 "}}");
1070 }
1071
1072 TEST(SchemaValidator, Object_Properties) {
1073 Document sd;
1074 sd.Parse(
1075 "{"
1076 " \"type\": \"object\","
1077 " \"properties\" : {"
1078 " \"number\": { \"type\": \"number\" },"
1079 " \"street_name\" : { \"type\": \"string\" },"
1080 " \"street_type\" : { \"type\": \"string\", \"enum\" : [\"Street\", \"Avenue\", \"Boulevard\"] }"
1081 " }"
1082 "}");
1083
1084 SchemaDocument s(sd);
1085
1086 VALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }", true);
1087 INVALIDATE(s, "{ \"number\": \"1600\", \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }", "/properties/number", "type", "/number",
1088 "{ \"type\": {"
1089 " \"errorCode\": 20,"
1090 " \"instanceRef\": \"#/number\", \"schemaRef\": \"#/properties/number\","
1091 " \"expected\": [\"number\"], \"actual\": \"string\""
1092 "}}");
1093 INVALIDATE(s, "{ \"number\": \"One\", \"street_name\": \"Microsoft\", \"street_type\": \"Way\" }",
1094 "/properties/number", "type", "/number",
1095 "{ \"type\": {"
1096 " \"errorCode\": 20,"
1097 " \"instanceRef\": \"#/number\", \"schemaRef\": \"#/properties/number\","
1098 " \"expected\": [\"number\"], \"actual\": \"string\""
1099 "}}"); // fail fast
1100 VALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\" }", true);
1101 VALIDATE(s, "{}", true);
1102 VALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\", \"direction\": \"NW\" }", true);
1103 }
1104
1105 TEST(SchemaValidator, Object_AdditionalPropertiesBoolean) {
1106 Document sd;
1107 sd.Parse(
1108 "{"
1109 " \"type\": \"object\","
1110 " \"properties\" : {"
1111 " \"number\": { \"type\": \"number\" },"
1112 " \"street_name\" : { \"type\": \"string\" },"
1113 " \"street_type\" : { \"type\": \"string\","
1114 " \"enum\" : [\"Street\", \"Avenue\", \"Boulevard\"]"
1115 " }"
1116 " },"
1117 " \"additionalProperties\": false"
1118 "}");
1119
1120 SchemaDocument s(sd);
1121
1122 VALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }", true);
1123 INVALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\", \"direction\": \"NW\" }", "", "additionalProperties", "/direction",
1124 "{ \"additionalProperties\": {"
1125 " \"errorCode\": 16,"
1126 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1127 " \"disallowed\": \"direction\""
1128 "}}");
1129 }
1130
1131 TEST(SchemaValidator, Object_AdditionalPropertiesObject) {
1132 Document sd;
1133 sd.Parse(
1134 "{"
1135 " \"type\": \"object\","
1136 " \"properties\" : {"
1137 " \"number\": { \"type\": \"number\" },"
1138 " \"street_name\" : { \"type\": \"string\" },"
1139 " \"street_type\" : { \"type\": \"string\","
1140 " \"enum\" : [\"Street\", \"Avenue\", \"Boulevard\"]"
1141 " }"
1142 " },"
1143 " \"additionalProperties\": { \"type\": \"string\" }"
1144 "}");
1145 SchemaDocument s(sd);
1146
1147 VALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\" }", true);
1148 VALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\", \"direction\": \"NW\" }", true);
1149 INVALIDATE(s, "{ \"number\": 1600, \"street_name\": \"Pennsylvania\", \"street_type\": \"Avenue\", \"office_number\": 201 }", "/additionalProperties", "type", "/office_number",
1150 "{ \"type\": {"
1151 " \"errorCode\": 20,"
1152 " \"instanceRef\": \"#/office_number\", \"schemaRef\": \"#/additionalProperties\","
1153 " \"expected\": [\"string\"], \"actual\": \"integer\""
1154 "}}");
1155 }
1156
1157 TEST(SchemaValidator, Object_Required) {
1158 Document sd;
1159 sd.Parse(
1160 "{"
1161 " \"type\": \"object\","
1162 " \"properties\" : {"
1163 " \"name\": { \"type\": \"string\" },"
1164 " \"email\" : { \"type\": \"string\" },"
1165 " \"address\" : { \"type\": \"string\" },"
1166 " \"telephone\" : { \"type\": \"string\" }"
1167 " },"
1168 " \"required\":[\"name\", \"email\"]"
1169 "}");
1170 SchemaDocument s(sd);
1171
1172 VALIDATE(s, "{ \"name\": \"William Shakespeare\", \"email\" : \"[email protected]\" }", true);
1173 VALIDATE(s, "{ \"name\": \"William Shakespeare\", \"email\" : \"[email protected]\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\", \"authorship\" : \"in question\"}", true);
1174 INVALIDATE(s, "{ \"name\": \"William Shakespeare\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\" }", "", "required", "",
1175 "{ \"required\": {"
1176 " \"errorCode\": 15,"
1177 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1178 " \"missing\": [\"email\"]"
1179 "}}");
1180 INVALIDATE(s, "{}", "", "required", "",
1181 "{ \"required\": {"
1182 " \"errorCode\": 15,"
1183 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1184 " \"missing\": [\"name\", \"email\"]"
1185 "}}");
1186 }
1187
1188 TEST(SchemaValidator, Object_Required_PassWithDefault) {
1189 Document sd;
1190 sd.Parse(
1191 "{"
1192 " \"type\": \"object\","
1193 " \"properties\" : {"
1194 " \"name\": { \"type\": \"string\", \"default\": \"William Shakespeare\" },"
1195 " \"email\" : { \"type\": \"string\", \"default\": \"\" },"
1196 " \"address\" : { \"type\": \"string\" },"
1197 " \"telephone\" : { \"type\": \"string\" }"
1198 " },"
1199 " \"required\":[\"name\", \"email\"]"
1200 "}");
1201 SchemaDocument s(sd);
1202
1203 VALIDATE(s, "{ \"email\" : \"[email protected]\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\", \"authorship\" : \"in question\"}", true);
1204 INVALIDATE(s, "{ \"name\": \"William Shakespeare\", \"address\" : \"Henley Street, Stratford-upon-Avon, Warwickshire, England\" }", "", "required", "",
1205 "{ \"required\": {"
1206 " \"errorCode\": 15,"
1207 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1208 " \"missing\": [\"email\"]"
1209 "}}");
1210 INVALIDATE(s, "{}", "", "required", "",
1211 "{ \"required\": {"
1212 " \"errorCode\": 15,"
1213 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1214 " \"missing\": [\"email\"]"
1215 "}}");
1216 }
1217
1218 TEST(SchemaValidator, Object_PropertiesRange) {
1219 Document sd;
1220 sd.Parse("{\"type\":\"object\", \"minProperties\":2, \"maxProperties\":3}");
1221 SchemaDocument s(sd);
1222
1223 INVALIDATE(s, "{}", "", "minProperties", "",
1224 "{ \"minProperties\": {"
1225 " \"errorCode\": 14,"
1226 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1227 " \"expected\": 2, \"actual\": 0"
1228 "}}");
1229 INVALIDATE(s, "{\"a\":0}", "", "minProperties", "",
1230 "{ \"minProperties\": {"
1231 " \"errorCode\": 14,"
1232 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1233 " \"expected\": 2, \"actual\": 1"
1234 "}}");
1235 VALIDATE(s, "{\"a\":0,\"b\":1}", true);
1236 VALIDATE(s, "{\"a\":0,\"b\":1,\"c\":2}", true);
1237 INVALIDATE(s, "{\"a\":0,\"b\":1,\"c\":2,\"d\":3}", "", "maxProperties", "",
1238 "{ \"maxProperties\": {"
1239 " \"errorCode\": 13,"
1240 " \"instanceRef\": \"#\", \"schemaRef\": \"#\", "
1241 " \"expected\": 3, \"actual\": 4"
1242 "}}");
1243 }
1244
1245 TEST(SchemaValidator, Object_PropertyDependencies) {
1246 Document sd;
1247 sd.Parse(
1248 "{"
1249 " \"type\": \"object\","
1250 " \"properties\": {"
1251 " \"name\": { \"type\": \"string\" },"
1252 " \"credit_card\": { \"type\": \"number\" },"
1253 " \"cvv_code\": { \"type\": \"number\" },"
1254 " \"billing_address\": { \"type\": \"string\" }"
1255 " },"
1256 " \"required\": [\"name\"],"
1257 " \"dependencies\": {"
1258 " \"credit_card\": [\"cvv_code\", \"billing_address\"]"
1259 " }"
1260 "}");
1261 SchemaDocument s(sd);
1262
1263 VALIDATE(s, "{ \"name\": \"John Doe\", \"credit_card\": 5555555555555555, \"cvv_code\": 777, "
1264 "\"billing_address\": \"555 Debtor's Lane\" }", true);
1265 INVALIDATE(s, "{ \"name\": \"John Doe\", \"credit_card\": 5555555555555555 }", "", "dependencies", "",
1266 "{ \"dependencies\": {"
1267 " \"errorCode\": 18,"
1268 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1269 " \"errors\": {"
1270 " \"credit_card\": {"
1271 " \"required\": {"
1272 " \"errorCode\": 15,"
1273 " \"instanceRef\": \"#\", \"schemaRef\": \"#/dependencies/credit_card\","
1274 " \"missing\": [\"cvv_code\", \"billing_address\"]"
1275 " } } }"
1276 "}}");
1277 VALIDATE(s, "{ \"name\": \"John Doe\"}", true);
1278 VALIDATE(s, "{ \"name\": \"John Doe\", \"cvv_code\": 777, \"billing_address\": \"555 Debtor's Lane\" }", true);
1279 }
1280
1281 TEST(SchemaValidator, Object_SchemaDependencies) {
1282 Document sd;
1283 sd.Parse(
1284 "{"
1285 " \"type\": \"object\","
1286 " \"properties\" : {"
1287 " \"name\": { \"type\": \"string\" },"
1288 " \"credit_card\" : { \"type\": \"number\" }"
1289 " },"
1290 " \"required\" : [\"name\"],"
1291 " \"dependencies\" : {"
1292 " \"credit_card\": {"
1293 " \"properties\": {"
1294 " \"billing_address\": { \"type\": \"string\" }"
1295 " },"
1296 " \"required\" : [\"billing_address\"]"
1297 " }"
1298 " }"
1299 "}");
1300 SchemaDocument s(sd);
1301
1302 VALIDATE(s, "{\"name\": \"John Doe\", \"credit_card\" : 5555555555555555,\"billing_address\" : \"555 Debtor's Lane\"}", true);
1303 INVALIDATE(s, "{\"name\": \"John Doe\", \"credit_card\" : 5555555555555555 }", "", "dependencies", "",
1304 "{ \"dependencies\": {"
1305 " \"errorCode\": 18,"
1306 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1307 " \"errors\": {"
1308 " \"credit_card\": {"
1309 " \"required\": {"
1310 " \"errorCode\": 15,"
1311 " \"instanceRef\": \"#\", \"schemaRef\": \"#/dependencies/credit_card\","
1312 " \"missing\": [\"billing_address\"]"
1313 " } } }"
1314 "}}");
1315 VALIDATE(s, "{\"name\": \"John Doe\", \"billing_address\" : \"555 Debtor's Lane\"}", true);
1316 }
1317
1318 #if RAPIDJSON_SCHEMA_HAS_REGEX
1319 TEST(SchemaValidator, Object_PatternProperties) {
1320 Document sd;
1321 sd.Parse(
1322 "{"
1323 " \"type\": \"object\","
1324 " \"patternProperties\": {"
1325 " \"^S_\": { \"type\": \"string\" },"
1326 " \"^I_\": { \"type\": \"integer\" }"
1327 " }"
1328 "}");
1329 SchemaDocument s(sd);
1330
1331 VALIDATE(s, "{ \"S_25\": \"This is a string\" }", true);
1332 VALIDATE(s, "{ \"I_0\": 42 }", true);
1333 INVALIDATE(s, "{ \"S_0\": 42 }", "", "patternProperties", "/S_0",
1334 "{ \"type\": {"
1335 " \"errorCode\": 20,"
1336 " \"instanceRef\": \"#/S_0\", \"schemaRef\": \"#/patternProperties/%5ES_\","
1337 " \"expected\": [\"string\"], \"actual\": \"integer\""
1338 "}}");
1339 INVALIDATE(s, "{ \"I_42\": \"This is a string\" }", "", "patternProperties", "/I_42",
1340 "{ \"type\": {"
1341 " \"errorCode\": 20,"
1342 " \"instanceRef\": \"#/I_42\", \"schemaRef\": \"#/patternProperties/%5EI_\","
1343 " \"expected\": [\"integer\"], \"actual\": \"string\""
1344 "}}");
1345 VALIDATE(s, "{ \"keyword\": \"value\" }", true);
1346 }
1347
1348 TEST(SchemaValidator, Object_PatternProperties_ErrorConflict) {
1349 Document sd;
1350 sd.Parse(
1351 "{"
1352 " \"type\": \"object\","
1353 " \"patternProperties\": {"
1354 " \"^I_\": { \"multipleOf\": 5 },"
1355 " \"30$\": { \"multipleOf\": 6 }"
1356 " }"
1357 "}");
1358 SchemaDocument s(sd);
1359
1360 VALIDATE(s, "{ \"I_30\": 30 }", true);
1361 INVALIDATE(s, "{ \"I_30\": 7 }", "", "patternProperties", "/I_30",
1362 "{ \"multipleOf\": ["
1363 " {"
1364 " \"errorCode\": 1,"
1365 " \"instanceRef\": \"#/I_30\", \"schemaRef\": \"#/patternProperties/%5EI_\","
1366 " \"expected\": 5, \"actual\": 7"
1367 " }, {"
1368 " \"errorCode\": 1,"
1369 " \"instanceRef\": \"#/I_30\", \"schemaRef\": \"#/patternProperties/30%24\","
1370 " \"expected\": 6, \"actual\": 7"
1371 " }"
1372 "]}");
1373 }
1374
1375 TEST(SchemaValidator, Object_Properties_PatternProperties) {
1376 Document sd;
1377 sd.Parse(
1378 "{"
1379 " \"type\": \"object\","
1380 " \"properties\": {"
1381 " \"I_42\": { \"type\": \"integer\", \"minimum\": 73 }"
1382 " },"
1383 " \"patternProperties\": {"
1384 " \"^I_\": { \"type\": \"integer\", \"multipleOf\": 6 }"
1385 " }"
1386 "}");
1387 SchemaDocument s(sd);
1388
1389 VALIDATE(s, "{ \"I_6\": 6 }", true);
1390 VALIDATE(s, "{ \"I_42\": 78 }", true);
1391 INVALIDATE(s, "{ \"I_42\": 42 }", "", "patternProperties", "/I_42",
1392 "{ \"minimum\": {"
1393 " \"errorCode\": 4,"
1394 " \"instanceRef\": \"#/I_42\", \"schemaRef\": \"#/properties/I_42\","
1395 " \"expected\": 73, \"actual\": 42"
1396 "}}");
1397 INVALIDATE(s, "{ \"I_42\": 7 }", "", "patternProperties", "/I_42",
1398 "{ \"minimum\": {"
1399 " \"errorCode\": 4,"
1400 " \"instanceRef\": \"#/I_42\", \"schemaRef\": \"#/properties/I_42\","
1401 " \"expected\": 73, \"actual\": 7"
1402 " },"
1403 " \"multipleOf\": {"
1404 " \"errorCode\": 1,"
1405 " \"instanceRef\": \"#/I_42\", \"schemaRef\": \"#/patternProperties/%5EI_\","
1406 " \"expected\": 6, \"actual\": 7"
1407 " }"
1408 "}");
1409 }
1410
1411 TEST(SchemaValidator, Object_PatternProperties_AdditionalPropertiesObject) {
1412 Document sd;
1413 sd.Parse(
1414 "{"
1415 " \"type\": \"object\","
1416 " \"properties\": {"
1417 " \"builtin\": { \"type\": \"number\" }"
1418 " },"
1419 " \"patternProperties\": {"
1420 " \"^S_\": { \"type\": \"string\" },"
1421 " \"^I_\": { \"type\": \"integer\" }"
1422 " },"
1423 " \"additionalProperties\": { \"type\": \"string\" }"
1424 "}");
1425 SchemaDocument s(sd);
1426
1427 VALIDATE(s, "{ \"builtin\": 42 }", true);
1428 VALIDATE(s, "{ \"keyword\": \"value\" }", true);
1429 INVALIDATE(s, "{ \"keyword\": 42 }", "/additionalProperties", "type", "/keyword",
1430 "{ \"type\": {"
1431 " \"errorCode\": 20,"
1432 " \"instanceRef\": \"#/keyword\", \"schemaRef\": \"#/additionalProperties\","
1433 " \"expected\": [\"string\"], \"actual\": \"integer\""
1434 "}}");
1435 }
1436
1437 // Replaces test Issue285 and tests failure as well as success
1438 TEST(SchemaValidator, Object_PatternProperties_AdditionalPropertiesBoolean) {
1439 Document sd;
1440 sd.Parse(
1441 "{"
1442 " \"type\": \"object\","
1443 " \"patternProperties\": {"
1444 " \"^S_\": { \"type\": \"string\" },"
1445 " \"^I_\": { \"type\": \"integer\" }"
1446 " },"
1447 " \"additionalProperties\": false"
1448 "}");
1449 SchemaDocument s(sd);
1450
1451 VALIDATE(s, "{ \"S_25\": \"This is a string\" }", true);
1452 VALIDATE(s, "{ \"I_0\": 42 }", true);
1453 INVALIDATE(s, "{ \"keyword\": \"value\" }", "", "additionalProperties", "/keyword",
1454 "{ \"additionalProperties\": {"
1455 " \"errorCode\": 16,"
1456 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1457 " \"disallowed\": \"keyword\""
1458 "}}");
1459 }
1460 #endif
1461
1462 TEST(SchemaValidator, Array) {
1463 Document sd;
1464 sd.Parse("{\"type\":\"array\"}");
1465 SchemaDocument s(sd);
1466
1467 VALIDATE(s, "[1, 2, 3, 4, 5]", true);
1468 VALIDATE(s, "[3, \"different\", { \"types\" : \"of values\" }]", true);
1469 INVALIDATE(s, "{\"Not\": \"an array\"}", "", "type", "",
1470 "{ \"type\": {"
1471 " \"errorCode\": 20,"
1472 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1473 " \"expected\": [\"array\"], \"actual\": \"object\""
1474 "}}");
1475 }
1476
1477 TEST(SchemaValidator, Array_ItemsList) {
1478 Document sd;
1479 sd.Parse(
1480 "{"
1481 " \"type\": \"array\","
1482 " \"items\" : {"
1483 " \"type\": \"number\""
1484 " }"
1485 "}");
1486 SchemaDocument s(sd);
1487
1488 VALIDATE(s, "[1, 2, 3, 4, 5]", true);
1489 INVALIDATE(s, "[1, 2, \"3\", 4, 5]", "/items", "type", "/2",
1490 "{ \"type\": {"
1491 " \"errorCode\": 20,"
1492 " \"instanceRef\": \"#/2\", \"schemaRef\": \"#/items\","
1493 " \"expected\": [\"number\"], \"actual\": \"string\""
1494 "}}");
1495 VALIDATE(s, "[]", true);
1496 }
1497
1498 TEST(SchemaValidator, Array_ItemsTuple) {
1499 Document sd;
1500 sd.Parse(
1501 "{"
1502 " \"type\": \"array\","
1503 " \"items\": ["
1504 " {"
1505 " \"type\": \"number\""
1506 " },"
1507 " {"
1508 " \"type\": \"string\""
1509 " },"
1510 " {"
1511 " \"type\": \"string\","
1512 " \"enum\": [\"Street\", \"Avenue\", \"Boulevard\"]"
1513 " },"
1514 " {"
1515 " \"type\": \"string\","
1516 " \"enum\": [\"NW\", \"NE\", \"SW\", \"SE\"]"
1517 " }"
1518 " ]"
1519 "}");
1520 SchemaDocument s(sd);
1521
1522 VALIDATE(s, "[1600, \"Pennsylvania\", \"Avenue\", \"NW\"]", true);
1523 INVALIDATE(s, "[24, \"Sussex\", \"Drive\"]", "/items/2", "enum", "/2",
1524 "{ \"enum\": { \"errorCode\": 19, \"instanceRef\": \"#/2\", \"schemaRef\": \"#/items/2\" }}");
1525 INVALIDATE(s, "[\"Palais de l'Elysee\"]", "/items/0", "type", "/0",
1526 "{ \"type\": {"
1527 " \"errorCode\": 20,"
1528 " \"instanceRef\": \"#/0\", \"schemaRef\": \"#/items/0\","
1529 " \"expected\": [\"number\"], \"actual\": \"string\""
1530 "}}");
1531 INVALIDATE(s, "[\"Twenty-four\", \"Sussex\", \"Drive\"]", "/items/0", "type", "/0",
1532 "{ \"type\": {"
1533 " \"errorCode\": 20,"
1534 " \"instanceRef\": \"#/0\", \"schemaRef\": \"#/items/0\","
1535 " \"expected\": [\"number\"], \"actual\": \"string\""
1536 "}}"); // fail fast
1537 VALIDATE(s, "[10, \"Downing\", \"Street\"]", true);
1538 VALIDATE(s, "[1600, \"Pennsylvania\", \"Avenue\", \"NW\", \"Washington\"]", true);
1539 }
1540
1541 TEST(SchemaValidator, Array_AdditionalItems) {
1542 Document sd;
1543 sd.Parse(
1544 "{"
1545 " \"type\": \"array\","
1546 " \"items\": ["
1547 " {"
1548 " \"type\": \"number\""
1549 " },"
1550 " {"
1551 " \"type\": \"string\""
1552 " },"
1553 " {"
1554 " \"type\": \"string\","
1555 " \"enum\": [\"Street\", \"Avenue\", \"Boulevard\"]"
1556 " },"
1557 " {"
1558 " \"type\": \"string\","
1559 " \"enum\": [\"NW\", \"NE\", \"SW\", \"SE\"]"
1560 " }"
1561 " ],"
1562 " \"additionalItems\": false"
1563 "}");
1564 SchemaDocument s(sd);
1565
1566 VALIDATE(s, "[1600, \"Pennsylvania\", \"Avenue\", \"NW\"]", true);
1567 VALIDATE(s, "[1600, \"Pennsylvania\", \"Avenue\"]", true);
1568 INVALIDATE(s, "[1600, \"Pennsylvania\", \"Avenue\", \"NW\", \"Washington\"]", "", "additionalItems", "/4",
1569 "{ \"additionalItems\": {"
1570 " \"errorCode\": 12,"
1571 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1572 " \"disallowed\": 4"
1573 "}}");
1574 }
1575
1576 TEST(SchemaValidator, Array_ItemsRange) {
1577 Document sd;
1578 sd.Parse("{\"type\": \"array\",\"minItems\": 2,\"maxItems\" : 3}");
1579 SchemaDocument s(sd);
1580
1581 INVALIDATE(s, "[]", "", "minItems", "",
1582 "{ \"minItems\": {"
1583 " \"errorCode\": 10,"
1584 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1585 " \"expected\": 2, \"actual\": 0"
1586 "}}");
1587 INVALIDATE(s, "[1]", "", "minItems", "",
1588 "{ \"minItems\": {"
1589 " \"errorCode\": 10,"
1590 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1591 " \"expected\": 2, \"actual\": 1"
1592 "}}");
1593 VALIDATE(s, "[1, 2]", true);
1594 VALIDATE(s, "[1, 2, 3]", true);
1595 INVALIDATE(s, "[1, 2, 3, 4]", "", "maxItems", "",
1596 "{ \"maxItems\": {"
1597 " \"errorCode\": 9,"
1598 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1599 " \"expected\": 3, \"actual\": 4"
1600 "}}");
1601 }
1602
1603 TEST(SchemaValidator, Array_UniqueItems) {
1604 Document sd;
1605 sd.Parse("{\"type\": \"array\", \"uniqueItems\": true}");
1606 SchemaDocument s(sd);
1607
1608 VALIDATE(s, "[1, 2, 3, 4, 5]", true);
1609 INVALIDATE(s, "[1, 2, 3, 3, 4]", "", "uniqueItems", "/3",
1610 "{ \"uniqueItems\": {"
1611 " \"errorCode\": 11,"
1612 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1613 " \"duplicates\": [2, 3]"
1614 "}}");
1615 INVALIDATE(s, "[1, 2, 3, 3, 3]", "", "uniqueItems", "/3",
1616 "{ \"uniqueItems\": {"
1617 " \"errorCode\": 11,"
1618 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1619 " \"duplicates\": [2, 3]"
1620 "}}"); // fail fast
1621 VALIDATE(s, "[]", true);
1622 }
1623
1624 TEST(SchemaValidator, Boolean) {
1625 Document sd;
1626 sd.Parse("{\"type\":\"boolean\"}");
1627 SchemaDocument s(sd);
1628
1629 VALIDATE(s, "true", true);
1630 VALIDATE(s, "false", true);
1631 INVALIDATE(s, "\"true\"", "", "type", "",
1632 "{ \"type\": {"
1633 " \"errorCode\": 20,"
1634 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1635 " \"expected\": [\"boolean\"], \"actual\": \"string\""
1636 "}}");
1637 INVALIDATE(s, "0", "", "type", "",
1638 "{ \"type\": {"
1639 " \"errorCode\": 20,"
1640 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1641 " \"expected\": [\"boolean\"], \"actual\": \"integer\""
1642 "}}");
1643 }
1644
1645 TEST(SchemaValidator, Null) {
1646 Document sd;
1647 sd.Parse("{\"type\":\"null\"}");
1648 SchemaDocument s(sd);
1649
1650 VALIDATE(s, "null", true);
1651 INVALIDATE(s, "false", "", "type", "",
1652 "{ \"type\": {"
1653 " \"errorCode\": 20,"
1654 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1655 " \"expected\": [\"null\"], \"actual\": \"boolean\""
1656 "}}");
1657 INVALIDATE(s, "0", "", "type", "",
1658 "{ \"type\": {"
1659 " \"errorCode\": 20,"
1660 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1661 " \"expected\": [\"null\"], \"actual\": \"integer\""
1662 "}}");
1663 INVALIDATE(s, "\"\"", "", "type", "",
1664 "{ \"type\": {"
1665 " \"errorCode\": 20,"
1666 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
1667 " \"expected\": [\"null\"], \"actual\": \"string\""
1668 "}}");
1669 }
1670
1671 // Additional tests
1672
1673 TEST(SchemaValidator, ObjectInArray) {
1674 Document sd;
1675 sd.Parse("{\"type\":\"array\", \"items\": { \"type\":\"string\" }}");
1676 SchemaDocument s(sd);
1677
1678 VALIDATE(s, "[\"a\"]", true);
1679 INVALIDATE(s, "[1]", "/items", "type", "/0",
1680 "{ \"type\": {"
1681 " \"errorCode\": 20,"
1682 " \"instanceRef\": \"#/0\", \"schemaRef\": \"#/items\","
1683 " \"expected\": [\"string\"], \"actual\": \"integer\""
1684 "}}");
1685 INVALIDATE(s, "[{}]", "/items", "type", "/0",
1686 "{ \"type\": {"
1687 " \"errorCode\": 20,"
1688 " \"instanceRef\": \"#/0\", \"schemaRef\": \"#/items\","
1689 " \"expected\": [\"string\"], \"actual\": \"object\""
1690 "}}");
1691 }
1692
1693 TEST(SchemaValidator, MultiTypeInObject) {
1694 Document sd;
1695 sd.Parse(
1696 "{"
1697 " \"type\":\"object\","
1698 " \"properties\": {"
1699 " \"tel\" : {"
1700 " \"type\":[\"integer\", \"string\"]"
1701 " }"
1702 " }"
1703 "}");
1704 SchemaDocument s(sd);
1705
1706 VALIDATE(s, "{ \"tel\": 999 }", true);
1707 VALIDATE(s, "{ \"tel\": \"123-456\" }", true);
1708 INVALIDATE(s, "{ \"tel\": true }", "/properties/tel", "type", "/tel",
1709 "{ \"type\": {"
1710 " \"errorCode\": 20,"
1711 " \"instanceRef\": \"#/tel\", \"schemaRef\": \"#/properties/tel\","
1712 " \"expected\": [\"string\", \"integer\"], \"actual\": \"boolean\""
1713 "}}");
1714 }
1715
1716 TEST(SchemaValidator, MultiTypeWithObject) {
1717 Document sd;
1718 sd.Parse(
1719 "{"
1720 " \"type\": [\"object\",\"string\"],"
1721 " \"properties\": {"
1722 " \"tel\" : {"
1723 " \"type\": \"integer\""
1724 " }"
1725 " }"
1726 "}");
1727 SchemaDocument s(sd);
1728
1729 VALIDATE(s, "\"Hello\"", true);
1730 VALIDATE(s, "{ \"tel\": 999 }", true);
1731 INVALIDATE(s, "{ \"tel\": \"fail\" }", "/properties/tel", "type", "/tel",
1732 "{ \"type\": {"
1733 " \"errorCode\": 20,"
1734 " \"instanceRef\": \"#/tel\", \"schemaRef\": \"#/properties/tel\","
1735 " \"expected\": [\"integer\"], \"actual\": \"string\""
1736 "}}");
1737 }
1738
1739 TEST(SchemaValidator, AllOf_Nested) {
1740 Document sd;
1741 sd.Parse(
1742 "{"
1743 " \"allOf\": ["
1744 " { \"type\": \"string\", \"minLength\": 2 },"
1745 " { \"type\": \"string\", \"maxLength\": 5 },"
1746 " { \"allOf\": [ { \"enum\" : [\"ok\", \"okay\", \"OK\", \"o\"] }, { \"enum\" : [\"ok\", \"OK\", \"o\"]} ] }"
1747 " ]"
1748 "}");
1749 SchemaDocument s(sd);
1750
1751 VALIDATE(s, "\"ok\"", true);
1752 VALIDATE(s, "\"OK\"", true);
1753 INVALIDATE(s, "\"okay\"", "", "allOf", "",
1754 "{ \"allOf\": {"
1755 " \"errors\": ["
1756 " {},{},"
1757 " { \"allOf\": {"
1758 " \"errors\": ["
1759 " {},"
1760 " { \"enum\": {\"errorCode\": 19, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2/allOf/1\" }}"
1761 " ],"
1762 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2\""
1763 " }}],"
1764 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
1765 "}}");
1766 INVALIDATE(s, "\"o\"", "", "allOf", "",
1767 "{ \"allOf\": {"
1768 " \"errors\": ["
1769 " { \"minLength\": {\"actual\": \"o\", \"expected\": 2, \"errorCode\": 7, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/0\" }},"
1770 " {},{}"
1771 " ],"
1772 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
1773 "}}");
1774 INVALIDATE(s, "\"n\"", "", "allOf", "",
1775 "{ \"allOf\": {"
1776 " \"errors\": ["
1777 " { \"minLength\": {\"actual\": \"n\", \"expected\": 2, \"errorCode\": 7, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/0\" }},"
1778 " {},"
1779 " { \"allOf\": {"
1780 " \"errors\": ["
1781 " { \"enum\": {\"errorCode\": 19 ,\"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2/allOf/0\"}},"
1782 " { \"enum\": {\"errorCode\": 19, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2/allOf/1\"}}"
1783 " ],"
1784 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2\""
1785 " }}"
1786 " ],"
1787 " \"errorCode\":23,\"instanceRef\":\"#\",\"schemaRef\":\"#\""
1788 "}}");
1789 INVALIDATE(s, "\"too long\"", "", "allOf", "",
1790 "{ \"allOf\": {"
1791 " \"errors\": ["
1792 " {},"
1793 " { \"maxLength\": {\"actual\": \"too long\", \"expected\": 5, \"errorCode\": 6, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/1\" }},"
1794 " { \"allOf\": {"
1795 " \"errors\": ["
1796 " { \"enum\": {\"errorCode\": 19 ,\"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2/allOf/0\"}},"
1797 " { \"enum\": {\"errorCode\": 19, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2/allOf/1\"}}"
1798 " ],"
1799 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2\""
1800 " }}"
1801 " ],"
1802 " \"errorCode\":23,\"instanceRef\":\"#\",\"schemaRef\":\"#\""
1803 "}}");
1804 INVALIDATE(s, "123", "", "allOf", "",
1805 "{ \"allOf\": {"
1806 " \"errors\": ["
1807 " {\"type\": {\"expected\": [\"string\"], \"actual\": \"integer\", \"errorCode\": 20, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/0\"}},"
1808 " {\"type\": {\"expected\": [\"string\"], \"actual\": \"integer\", \"errorCode\": 20, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/1\"}},"
1809 " { \"allOf\": {"
1810 " \"errors\": ["
1811 " { \"enum\": {\"errorCode\": 19 ,\"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2/allOf/0\"}},"
1812 " { \"enum\": {\"errorCode\": 19, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2/allOf/1\"}}"
1813 " ],"
1814 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#/allOf/2\""
1815 " }}"
1816 " ],"
1817 " \"errorCode\":23,\"instanceRef\":\"#\",\"schemaRef\":\"#\""
1818 "}}");
1819 }
1820
1821 TEST(SchemaValidator, EscapedPointer) {
1822 Document sd;
1823 sd.Parse(
1824 "{"
1825 " \"type\": \"object\","
1826 " \"properties\": {"
1827 " \"~/\": { \"type\": \"number\" }"
1828 " }"
1829 "}");
1830 SchemaDocument s(sd);
1831 INVALIDATE(s, "{\"~/\":true}", "/properties/~0~1", "type", "/~0~1",
1832 "{ \"type\": {"
1833 " \"errorCode\": 20,"
1834 " \"instanceRef\": \"#/~0~1\", \"schemaRef\": \"#/properties/~0~1\","
1835 " \"expected\": [\"number\"], \"actual\": \"boolean\""
1836 "}}");
1837 }
1838
1839 TEST(SchemaValidator, SchemaPointer) {
1840 Document sd;
1841 sd.Parse(
1842 "{"
1843 " \"swagger\": \"2.0\","
1844 " \"paths\": {"
1845 " \"/some/path\": {"
1846 " \"post\": {"
1847 " \"parameters\": ["
1848 " {"
1849 " \"in\": \"body\","
1850 " \"name\": \"body\","
1851 " \"schema\": {"
1852 " \"properties\": {"
1853 " \"a\": {"
1854 " \"$ref\": \"#/definitions/Prop_a\""
1855 " },"
1856 " \"b\": {"
1857 " \"type\": \"integer\""
1858 " }"
1859 " },"
1860 " \"type\": \"object\""
1861 " }"
1862 " }"
1863 " ],"
1864 " \"responses\": {"
1865 " \"200\": {"
1866 " \"schema\": {"
1867 " \"$ref\": \"#/definitions/Resp_200\""
1868 " }"
1869 " }"
1870 " }"
1871 " }"
1872 " }"
1873 " },"
1874 " \"definitions\": {"
1875 " \"Prop_a\": {"
1876 " \"properties\": {"
1877 " \"c\": {"
1878 " \"enum\": ["
1879 " \"C1\","
1880 " \"C2\","
1881 " \"C3\""
1882 " ],"
1883 " \"type\": \"string\""
1884 " },"
1885 " \"d\": {"
1886 " \"$ref\": \"#/definitions/Prop_d\""
1887 " },"
1888 " \"s\": {"
1889 " \"type\": \"string\""
1890 " }"
1891 " },"
1892 " \"required\": [\"c\"],"
1893 " \"type\": \"object\""
1894 " },"
1895 " \"Prop_d\": {"
1896 " \"properties\": {"
1897 " \"a\": {"
1898 " \"$ref\": \"#/definitions/Prop_a\""
1899 " },"
1900 " \"c\": {"
1901 " \"$ref\": \"#/definitions/Prop_a/properties/c\""
1902 " }"
1903 " },"
1904 " \"type\": \"object\""
1905 " },"
1906 " \"Resp_200\": {"
1907 " \"properties\": {"
1908 " \"e\": {"
1909 " \"type\": \"string\""
1910 " },"
1911 " \"f\": {"
1912 " \"type\": \"boolean\""
1913 " }"
1914 " },"
1915 " \"type\": \"object\""
1916 " }"
1917 " }"
1918 "}");
1919 SchemaDocument s1(sd, NULL, 0, NULL, NULL, Pointer("#/paths/~1some~1path/post/parameters/0/schema"));
1920 VALIDATE(s1,
1921 "{"
1922 " \"a\": {"
1923 " \"c\": \"C1\","
1924 " \"d\": {"
1925 " \"a\": {"
1926 " \"c\": \"C2\""
1927 " },"
1928 " \"c\": \"C3\""
1929 " }"
1930 " },"
1931 " \"b\": 123"
1932 "}",
1933 true);
1934 INVALIDATE(s1,
1935 "{"
1936 " \"a\": {"
1937 " \"c\": \"C1\","
1938 " \"d\": {"
1939 " \"a\": {"
1940 " \"c\": \"C2\""
1941 " },"
1942 " \"c\": \"C3\""
1943 " }"
1944 " },"
1945 " \"b\": \"should be an int\""
1946 "}",
1947 "#/paths/~1some~1path/post/parameters/0/schema/properties/b", "type", "#/b",
1948 "{ \"type\": {"
1949 " \"errorCode\": 20,"
1950 " \"instanceRef\":\"#/b\","
1951 " \"schemaRef\":\"#/paths/~1some~1path/post/parameters/0/schema/properties/b\","
1952 " \"expected\": [\"integer\"], \"actual\":\"string\""
1953 "}}");
1954 INVALIDATE(s1,
1955 "{"
1956 " \"a\": {"
1957 " \"c\": \"C1\","
1958 " \"d\": {"
1959 " \"a\": {"
1960 " \"c\": \"should be within enum\""
1961 " },"
1962 " \"c\": \"C3\""
1963 " }"
1964 " },"
1965 " \"b\": 123"
1966 "}",
1967 "#/definitions/Prop_a/properties/c", "enum", "#/a/d/a/c",
1968 "{ \"enum\": {"
1969 " \"errorCode\": 19,"
1970 " \"instanceRef\":\"#/a/d/a/c\","
1971 " \"schemaRef\":\"#/definitions/Prop_a/properties/c\""
1972 "}}");
1973 INVALIDATE(s1,
1974 "{"
1975 " \"a\": {"
1976 " \"c\": \"C1\","
1977 " \"d\": {"
1978 " \"a\": {"
1979 " \"s\": \"required 'c' is missing\""
1980 " }"
1981 " }"
1982 " },"
1983 " \"b\": 123"
1984 "}",
1985 "#/definitions/Prop_a", "required", "#/a/d/a",
1986 "{ \"required\": {"
1987 " \"errorCode\": 15,"
1988 " \"missing\":[\"c\"],"
1989 " \"instanceRef\":\"#/a/d/a\","
1990 " \"schemaRef\":\"#/definitions/Prop_a\""
1991 "}}");
1992 SchemaDocument s2(sd, NULL, 0, NULL, NULL, Pointer("#/paths/~1some~1path/post/responses/200/schema"));
1993 VALIDATE(s2,
1994 "{ \"e\": \"some string\", \"f\": false }",
1995 true);
1996 INVALIDATE(s2,
1997 "{ \"e\": true, \"f\": false }",
1998 "#/definitions/Resp_200/properties/e", "type", "#/e",
1999 "{ \"type\": {"
2000 " \"errorCode\": 20,"
2001 " \"instanceRef\":\"#/e\","
2002 " \"schemaRef\":\"#/definitions/Resp_200/properties/e\","
2003 " \"expected\": [\"string\"], \"actual\":\"boolean\""
2004 "}}");
2005 INVALIDATE(s2,
2006 "{ \"e\": \"some string\", \"f\": 123 }",
2007 "#/definitions/Resp_200/properties/f", "type", "#/f",
2008 "{ \"type\": {"
2009 " \"errorCode\": 20,"
2010 " \"instanceRef\":\"#/f\","
2011 " \"schemaRef\":\"#/definitions/Resp_200/properties/f\","
2012 " \"expected\": [\"boolean\"], \"actual\":\"integer\""
2013 "}}");
2014 }
2015
2016 template <typename Allocator>
2017 static char* ReadFile(const char* filename, Allocator& allocator) {
2018 const char *paths[] = {
2019 "",
2020 "bin/",
2021 "../bin/",
2022 "../../bin/",
2023 "../../../bin/"
2024 };
2025 char buffer[1024];
2026 FILE *fp = 0;
2027 for (size_t i = 0; i < sizeof(paths) / sizeof(paths[0]); i++) {
2028 sprintf(buffer, "%s%s", paths[i], filename);
2029 fp = fopen(buffer, "rb");
2030 if (fp)
2031 break;
2032 }
2033
2034 if (!fp)
2035 return 0;
2036
2037 fseek(fp, 0, SEEK_END);
2038 size_t length = static_cast<size_t>(ftell(fp));
2039 fseek(fp, 0, SEEK_SET);
2040 char* json = reinterpret_cast<char*>(allocator.Malloc(length + 1));
2041 size_t readLength = fread(json, 1, length, fp);
2042 json[readLength] = '\0';
2043 fclose(fp);
2044 return json;
2045 }
2046
2047 TEST(SchemaValidator, ValidateMetaSchema) {
2048 CrtAllocator allocator;
2049 char* json = ReadFile("draft-04/schema", allocator);
2050 Document d;
2051 d.Parse(json);
2052 ASSERT_FALSE(d.HasParseError());
2053 SchemaDocument sd(d);
2054 SchemaValidator validator(sd);
2055 d.Accept(validator);
2056 if (!validator.IsValid()) {
2057 StringBuffer sb;
2058 validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
2059 printf("Invalid schema: %s\n", sb.GetString());
2060 printf("Invalid keyword: %s\n", validator.GetInvalidSchemaKeyword());
2061 printf("Invalid code: %d\n", validator.GetInvalidSchemaCode());
2062 printf("Invalid message: %s\n", GetValidateError_En(validator.GetInvalidSchemaCode()));
2063 sb.Clear();
2064 validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
2065 printf("Invalid document: %s\n", sb.GetString());
2066 sb.Clear();
2067 Writer<StringBuffer> w(sb);
2068 validator.GetError().Accept(w);
2069 printf("Validation error: %s\n", sb.GetString());
2070 ADD_FAILURE();
2071 }
2072 CrtAllocator::Free(json);
2073 }
2074
2075 TEST(SchemaValidator, ValidateMetaSchema_UTF16) {
2076 typedef GenericDocument<UTF16<> > D;
2077 typedef GenericSchemaDocument<D::ValueType> SD;
2078 typedef GenericSchemaValidator<SD> SV;
2079
2080 CrtAllocator allocator;
2081 char* json = ReadFile("draft-04/schema", allocator);
2082
2083 D d;
2084 StringStream ss(json);
2085 d.ParseStream<0, UTF8<> >(ss);
2086 ASSERT_FALSE(d.HasParseError());
2087 SD sd(d);
2088 SV validator(sd);
2089 d.Accept(validator);
2090 if (!validator.IsValid()) {
2091 GenericStringBuffer<UTF16<> > sb;
2092 validator.GetInvalidSchemaPointer().StringifyUriFragment(sb);
2093 wprintf(L"Invalid schema: %ls\n", sb.GetString());
2094 wprintf(L"Invalid keyword: %ls\n", validator.GetInvalidSchemaKeyword());
2095 sb.Clear();
2096 validator.GetInvalidDocumentPointer().StringifyUriFragment(sb);
2097 wprintf(L"Invalid document: %ls\n", sb.GetString());
2098 sb.Clear();
2099 Writer<GenericStringBuffer<UTF16<> >, UTF16<> > w(sb);
2100 validator.GetError().Accept(w);
2101 printf("Validation error: %ls\n", sb.GetString());
2102 ADD_FAILURE();
2103 }
2104 CrtAllocator::Free(json);
2105 }
2106
2107 template <typename SchemaDocumentType = SchemaDocument>
2108 class RemoteSchemaDocumentProvider : public IGenericRemoteSchemaDocumentProvider<SchemaDocumentType> {
2109 public:
2110 RemoteSchemaDocumentProvider() :
2111 documentAllocator_(documentBuffer_, sizeof(documentBuffer_)),
2112 schemaAllocator_(schemaBuffer_, sizeof(schemaBuffer_))
2113 {
2114 const char* filenames[kCount] = {
2115 "jsonschema/remotes/integer.json",
2116 "jsonschema/remotes/subSchemas.json",
2117 "jsonschema/remotes/folder/folderInteger.json",
2118 "draft-04/schema",
2119 "unittestschema/address.json"
2120 };
2121 const char* uris[kCount] = {
2122 "http://localhost:1234/integer.json",
2123 "http://localhost:1234/subSchemas.json",
2124 "http://localhost:1234/folder/folderInteger.json",
2125 "http://json-schema.org/draft-04/schema",
2126 "http://localhost:1234/address.json"
2127 };
2128
2129 for (size_t i = 0; i < kCount; i++) {
2130 sd_[i] = 0;
2131
2132 char jsonBuffer[8192];
2133 MemoryPoolAllocator<> jsonAllocator(jsonBuffer, sizeof(jsonBuffer));
2134 char* json = ReadFile(filenames[i], jsonAllocator);
2135 if (!json) {
2136 printf("json remote file %s not found", filenames[i]);
2137 ADD_FAILURE();
2138 }
2139 else {
2140 char stackBuffer[4096];
2141 MemoryPoolAllocator<> stackAllocator(stackBuffer, sizeof(stackBuffer));
2142 DocumentType d(&documentAllocator_, 1024, &stackAllocator);
2143 d.Parse(json);
2144 sd_[i] = new SchemaDocumentType(d, uris[i], static_cast<SizeType>(strlen(uris[i])), 0, &schemaAllocator_);
2145 MemoryPoolAllocator<>::Free(json);
2146 }
2147 };
2148 }
2149
2150 ~RemoteSchemaDocumentProvider() {
2151 for (size_t i = 0; i < kCount; i++)
2152 delete sd_[i];
2153 }
2154
2155 virtual const SchemaDocumentType* GetRemoteDocument(const char* uri, SizeType length) {
2156 //printf("GetRemoteDocument : %s\n", uri);
2157 for (size_t i = 0; i < kCount; i++)
2158 if (typename SchemaDocumentType::GValue(uri, length) == sd_[i]->GetURI()) {
2159 //printf("Matched document");
2160 return sd_[i];
2161 }
2162 //printf("No matched document");
2163 return 0;
2164 }
2165
2166 private:
2167 typedef GenericDocument<typename SchemaDocumentType::EncodingType, MemoryPoolAllocator<>, MemoryPoolAllocator<> > DocumentType;
2168
2169 RemoteSchemaDocumentProvider(const RemoteSchemaDocumentProvider&);
2170 RemoteSchemaDocumentProvider& operator=(const RemoteSchemaDocumentProvider&);
2171
2172 static const size_t kCount = 5;
2173 SchemaDocumentType* sd_[kCount];
2174 typename DocumentType::AllocatorType documentAllocator_;
2175 typename SchemaDocumentType::AllocatorType schemaAllocator_;
2176 char documentBuffer_[16384];
2177 char schemaBuffer_[128u * 1024];
2178 };
2179
2180 TEST(SchemaValidator, TestSuite) {
2181 const char* filenames[] = {
2182 "additionalItems.json",
2183 "additionalProperties.json",
2184 "allOf.json",
2185 "anyOf.json",
2186 "default.json",
2187 "definitions.json",
2188 "dependencies.json",
2189 "enum.json",
2190 "items.json",
2191 "maximum.json",
2192 "maxItems.json",
2193 "maxLength.json",
2194 "maxProperties.json",
2195 "minimum.json",
2196 "minItems.json",
2197 "minLength.json",
2198 "minProperties.json",
2199 "multipleOf.json",
2200 "not.json",
2201 "oneOf.json",
2202 "pattern.json",
2203 "patternProperties.json",
2204 "properties.json",
2205 "ref.json",
2206 "refRemote.json",
2207 "required.json",
2208 "type.json",
2209 "uniqueItems.json"
2210 };
2211
2212 const char* onlyRunDescription = 0;
2213 //const char* onlyRunDescription = "a string is a string";
2214
2215 unsigned testCount = 0;
2216 unsigned passCount = 0;
2217
2218 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2219 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2220
2221 char jsonBuffer[65536];
2222 char documentBuffer[65536];
2223 char documentStackBuffer[65536];
2224 char schemaBuffer[65536];
2225 char validatorBuffer[65536];
2226 MemoryPoolAllocator<> jsonAllocator(jsonBuffer, sizeof(jsonBuffer));
2227 MemoryPoolAllocator<> documentAllocator(documentBuffer, sizeof(documentBuffer));
2228 MemoryPoolAllocator<> documentStackAllocator(documentStackBuffer, sizeof(documentStackBuffer));
2229 MemoryPoolAllocator<> schemaAllocator(schemaBuffer, sizeof(schemaBuffer));
2230 MemoryPoolAllocator<> validatorAllocator(validatorBuffer, sizeof(validatorBuffer));
2231
2232 for (size_t i = 0; i < sizeof(filenames) / sizeof(filenames[0]); i++) {
2233 char filename[FILENAME_MAX];
2234 sprintf(filename, "jsonschema/tests/draft4/%s", filenames[i]);
2235 char* json = ReadFile(filename, jsonAllocator);
2236 if (!json) {
2237 printf("json test suite file %s not found", filename);
2238 ADD_FAILURE();
2239 }
2240 else {
2241 //printf("\njson test suite file %s parsed ok\n", filename);
2242 GenericDocument<UTF8<>, MemoryPoolAllocator<>, MemoryPoolAllocator<> > d(&documentAllocator, 1024, &documentStackAllocator);
2243 d.Parse(json);
2244 if (d.HasParseError()) {
2245 printf("json test suite file %s has parse error", filename);
2246 ADD_FAILURE();
2247 }
2248 else {
2249 for (Value::ConstValueIterator schemaItr = d.Begin(); schemaItr != d.End(); ++schemaItr) {
2250 {
2251 const char* description1 = (*schemaItr)["description"].GetString();
2252 //printf("\ncompiling schema for json test %s \n", description1);
2253 SchemaDocumentType schema((*schemaItr)["schema"], filenames[i], static_cast<SizeType>(strlen(filenames[i])), &provider, &schemaAllocator);
2254 GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > validator(schema, &validatorAllocator);
2255 const Value& tests = (*schemaItr)["tests"];
2256 for (Value::ConstValueIterator testItr = tests.Begin(); testItr != tests.End(); ++testItr) {
2257 const char* description2 = (*testItr)["description"].GetString();
2258 //printf("running json test %s \n", description2);
2259 if (!onlyRunDescription || strcmp(description2, onlyRunDescription) == 0) {
2260 const Value& data = (*testItr)["data"];
2261 bool expected = (*testItr)["valid"].GetBool();
2262 testCount++;
2263 validator.Reset();
2264 data.Accept(validator);
2265 bool actual = validator.IsValid();
2266 if (expected != actual)
2267 printf("Fail: %30s \"%s\" \"%s\"\n", filename, description1, description2);
2268 else {
2269 //printf("Passed: %30s \"%s\" \"%s\"\n", filename, description1, description2);
2270 passCount++;
2271 }
2272 }
2273 }
2274 //printf("%zu %zu %zu\n", documentAllocator.Size(), schemaAllocator.Size(), validatorAllocator.Size());
2275 }
2276 schemaAllocator.Clear();
2277 validatorAllocator.Clear();
2278 }
2279 }
2280 }
2281 documentAllocator.Clear();
2282 MemoryPoolAllocator<>::Free(json);
2283 jsonAllocator.Clear();
2284 }
2285 printf("%d / %d passed (%2d%%)\n", passCount, testCount, passCount * 100 / testCount);
2286 if (passCount != testCount)
2287 ADD_FAILURE();
2288 }
2289
2290 TEST(SchemaValidatingReader, Simple) {
2291 Document sd;
2292 sd.Parse("{ \"type\": \"string\", \"enum\" : [\"red\", \"amber\", \"green\"] }");
2293 SchemaDocument s(sd);
2294
2295 Document d;
2296 StringStream ss("\"red\"");
2297 SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s);
2298 d.Populate(reader);
2299 EXPECT_TRUE(reader.GetParseResult());
2300 EXPECT_TRUE(reader.IsValid());
2301 EXPECT_TRUE(d.IsString());
2302 EXPECT_STREQ("red", d.GetString());
2303 }
2304
2305 TEST(SchemaValidatingReader, Invalid) {
2306 Document sd;
2307 sd.Parse("{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}");
2308 SchemaDocument s(sd);
2309
2310 Document d;
2311 StringStream ss("\"ABCD\"");
2312 SchemaValidatingReader<kParseDefaultFlags, StringStream, UTF8<> > reader(ss, s);
2313 d.Populate(reader);
2314 EXPECT_FALSE(reader.GetParseResult());
2315 EXPECT_FALSE(reader.IsValid());
2316 EXPECT_EQ(kParseErrorTermination, reader.GetParseResult().Code());
2317 EXPECT_STREQ("maxLength", reader.GetInvalidSchemaKeyword());
2318 EXPECT_TRUE(reader.GetInvalidSchemaCode() == kValidateErrorMaxLength);
2319 EXPECT_TRUE(reader.GetInvalidSchemaPointer() == SchemaDocument::PointerType(""));
2320 EXPECT_TRUE(reader.GetInvalidDocumentPointer() == SchemaDocument::PointerType(""));
2321 EXPECT_TRUE(d.IsNull());
2322 Document e;
2323 e.Parse(
2324 "{ \"maxLength\": {"
2325 " \"errorCode\": 6,"
2326 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
2327 " \"expected\": 3, \"actual\": \"ABCD\""
2328 "}}");
2329 if (e != reader.GetError()) {
2330 ADD_FAILURE();
2331 }
2332 }
2333
2334 TEST(SchemaValidatingWriter, Simple) {
2335 Document sd;
2336 sd.Parse("{\"type\":\"string\",\"minLength\":2,\"maxLength\":3}");
2337 SchemaDocument s(sd);
2338
2339 Document d;
2340 StringBuffer sb;
2341 Writer<StringBuffer> writer(sb);
2342 GenericSchemaValidator<SchemaDocument, Writer<StringBuffer> > validator(s, writer);
2343
2344 d.Parse("\"red\"");
2345 EXPECT_TRUE(d.Accept(validator));
2346 EXPECT_TRUE(validator.IsValid());
2347 EXPECT_STREQ("\"red\"", sb.GetString());
2348
2349 sb.Clear();
2350 validator.Reset();
2351 d.Parse("\"ABCD\"");
2352 EXPECT_FALSE(d.Accept(validator));
2353 EXPECT_FALSE(validator.IsValid());
2354 EXPECT_TRUE(validator.GetInvalidSchemaPointer() == SchemaDocument::PointerType(""));
2355 EXPECT_TRUE(validator.GetInvalidDocumentPointer() == SchemaDocument::PointerType(""));
2356 EXPECT_TRUE(validator.GetInvalidSchemaCode() == kValidateErrorMaxLength);
2357 Document e;
2358 e.Parse(
2359 "{ \"maxLength\": {"
2360 " \"errorCode\": 6,"
2361 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
2362 " \"expected\": 3, \"actual\": \"ABCD\""
2363 "}}");
2364 EXPECT_EQ(e, validator.GetError());
2365 }
2366
2367 TEST(Schema, Issue848) {
2368 rapidjson::Document d;
2369 rapidjson::SchemaDocument s(d);
2370 rapidjson::GenericSchemaValidator<rapidjson::SchemaDocument, rapidjson::Document> v(s);
2371 }
2372
2373 #if RAPIDJSON_HAS_CXX11_RVALUE_REFS
2374
2375 static SchemaDocument ReturnSchemaDocument() {
2376 Document sd;
2377 sd.Parse("{ \"type\": [\"number\", \"string\"] }");
2378 SchemaDocument s(sd);
2379 return s;
2380 }
2381
2382 TEST(Schema, Issue552) {
2383 SchemaDocument s = ReturnSchemaDocument();
2384 VALIDATE(s, "42", true);
2385 VALIDATE(s, "\"Life, the universe, and everything\"", true);
2386 INVALIDATE(s, "[\"Life\", \"the universe\", \"and everything\"]", "", "type", "",
2387 "{ \"type\": {"
2388 " \"errorCode\": 20,"
2389 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
2390 " \"expected\": [\"string\", \"number\"], \"actual\": \"array\""
2391 "}}");
2392 }
2393
2394 #endif // RAPIDJSON_HAS_CXX11_RVALUE_REFS
2395
2396 TEST(SchemaValidator, Issue608) {
2397 Document sd;
2398 sd.Parse("{\"required\": [\"a\", \"b\"] }");
2399 SchemaDocument s(sd);
2400
2401 VALIDATE(s, "{\"a\" : null, \"b\": null}", true);
2402 INVALIDATE(s, "{\"a\" : null, \"a\" : null}", "", "required", "",
2403 "{ \"required\": {"
2404 " \"errorCode\": 15,"
2405 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
2406 " \"missing\": [\"b\"]"
2407 "}}");
2408 }
2409
2410 // Fail to resolve $ref in allOf causes crash in SchemaValidator::StartObject()
2411 TEST(SchemaValidator, Issue728_AllOfRef) {
2412 Document sd;
2413 sd.Parse("{\"allOf\": [{\"$ref\": \"#/abc\"}]}");
2414 SchemaDocument s(sd);
2415 SCHEMAERROR(s, "{\"RefUnknown\":{\"errorCode\":5,\"instanceRef\":\"#/allOf/0\",\"value\":\"#/abc\"}}");
2416
2417 VALIDATE_(s, "{\"key1\": \"abc\", \"key2\": \"def\"}", true, false);
2418 }
2419
2420 TEST(SchemaValidator, Issue1017_allOfHandler) {
2421 Document sd;
2422 sd.Parse("{\"allOf\": [{\"type\": \"object\",\"properties\": {\"cyanArray2\": {\"type\": \"array\",\"items\": { \"type\": \"string\" }}}},{\"type\": \"object\",\"properties\": {\"blackArray\": {\"type\": \"array\",\"items\": { \"type\": \"string\" }}},\"required\": [ \"blackArray\" ]}]}");
2423 SchemaDocument s(sd);
2424 StringBuffer sb;
2425 Writer<StringBuffer> writer(sb);
2426 GenericSchemaValidator<SchemaDocument, Writer<StringBuffer> > validator(s, writer);
2427 EXPECT_TRUE(validator.StartObject());
2428 EXPECT_TRUE(validator.Key("cyanArray2", 10, false));
2429 EXPECT_TRUE(validator.StartArray());
2430 EXPECT_TRUE(validator.EndArray(0));
2431 EXPECT_TRUE(validator.Key("blackArray", 10, false));
2432 EXPECT_TRUE(validator.StartArray());
2433 EXPECT_TRUE(validator.EndArray(0));
2434 EXPECT_TRUE(validator.EndObject(0));
2435 EXPECT_TRUE(validator.IsValid());
2436 EXPECT_STREQ("{\"cyanArray2\":[],\"blackArray\":[]}", sb.GetString());
2437 }
2438
2439 TEST(SchemaValidator, Ref_remote) {
2440 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2441 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2442 Document sd;
2443 sd.Parse("{\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}");
2444 SchemaDocumentType s(sd, 0, 0, &provider);
2445 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2446 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2447 INVALIDATE_(s, "null", "/integer", "type", "",
2448 "{ \"type\": {"
2449 " \"errorCode\": 20,"
2450 " \"instanceRef\": \"#\","
2451 " \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
2452 " \"expected\": [\"integer\"], \"actual\": \"null\""
2453 "}}",
2454 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2455 }
2456
2457 // Merge with id where $ref is full URI
2458 TEST(SchemaValidator, Ref_remote_change_resolution_scope_uri) {
2459 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2460 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2461 Document sd;
2462 sd.Parse("{\"id\": \"http://ignore/blah#/ref\", \"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"http://localhost:1234/subSchemas.json#/integer\"}}}");
2463 SchemaDocumentType s(sd, 0, 0, &provider);
2464 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2465 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2466 INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
2467 "{ \"type\": {"
2468 " \"errorCode\": 20,"
2469 " \"instanceRef\": \"#/myInt\","
2470 " \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
2471 " \"expected\": [\"integer\"], \"actual\": \"null\""
2472 "}}",
2473 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2474 }
2475
2476 // Merge with id where $ref is a relative path
2477 TEST(SchemaValidator, Ref_remote_change_resolution_scope_relative_path) {
2478 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2479 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2480 Document sd;
2481 sd.Parse("{\"id\": \"http://localhost:1234/\", \"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"subSchemas.json#/integer\"}}}");
2482 SchemaDocumentType s(sd, 0, 0, &provider);
2483 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2484 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2485 INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
2486 "{ \"type\": {"
2487 " \"errorCode\": 20,"
2488 " \"instanceRef\": \"#/myInt\","
2489 " \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
2490 " \"expected\": [\"integer\"], \"actual\": \"null\""
2491 "}}",
2492 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2493 }
2494
2495 // Merge with id where $ref is an absolute path
2496 TEST(SchemaValidator, Ref_remote_change_resolution_scope_absolute_path) {
2497 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2498 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2499 Document sd;
2500 sd.Parse("{\"id\": \"http://localhost:1234/xxxx\", \"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#/integer\"}}}");
2501 SchemaDocumentType s(sd, 0, 0, &provider);
2502 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2503 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2504 INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
2505 "{ \"type\": {"
2506 " \"errorCode\": 20,"
2507 " \"instanceRef\": \"#/myInt\","
2508 " \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
2509 " \"expected\": [\"integer\"], \"actual\": \"null\""
2510 "}}",
2511 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2512 }
2513
2514 // Merge with id where $ref is an absolute path, and the document has a base URI
2515 TEST(SchemaValidator, Ref_remote_change_resolution_scope_absolute_path_document) {
2516 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2517 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2518 Document sd;
2519 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#/integer\"}}}");
2520 SchemaDocumentType s(sd, "http://localhost:1234/xxxx", 26, &provider);
2521 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2522 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2523 INVALIDATE_(s, "{\"myInt\": null}", "/integer", "type", "/myInt",
2524 "{ \"type\": {"
2525 " \"errorCode\": 20,"
2526 " \"instanceRef\": \"#/myInt\","
2527 " \"schemaRef\": \"http://localhost:1234/subSchemas.json#/integer\","
2528 " \"expected\": [\"integer\"], \"actual\": \"null\""
2529 "}}",
2530 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2531 }
2532
2533 // $ref is a non-JSON pointer fragment and there a matching id
2534 TEST(SchemaValidator, Ref_internal_id_1) {
2535 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2536 Document sd;
2537 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myStr\": {\"type\": \"string\", \"id\": \"#myStrId\"}, \"myInt2\": {\"type\": \"integer\", \"id\": \"#myId\"}}}");
2538 SchemaDocumentType s(sd);
2539 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2540 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2541 INVALIDATE_(s, "{\"myInt1\": null}", "/properties/myInt2", "type", "/myInt1",
2542 "{ \"type\": {"
2543 " \"errorCode\": 20,"
2544 " \"instanceRef\": \"#/myInt1\","
2545 " \"schemaRef\": \"#/properties/myInt2\","
2546 " \"expected\": [\"integer\"], \"actual\": \"null\""
2547 "}}",
2548 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2549 }
2550
2551 // $ref is a non-JSON pointer fragment and there are two matching ids so we take the first
2552 TEST(SchemaValidator, Ref_internal_id_2) {
2553 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2554 Document sd;
2555 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myInt2\": {\"type\": \"integer\", \"id\": \"#myId\"}, \"myStr\": {\"type\": \"string\", \"id\": \"#myId\"}}}");
2556 SchemaDocumentType s(sd);
2557 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2558 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2559 INVALIDATE_(s, "{\"myInt1\": null}", "/properties/myInt2", "type", "/myInt1",
2560 "{ \"type\": {"
2561 " \"errorCode\": 20,"
2562 " \"instanceRef\": \"#/myInt1\","
2563 " \"schemaRef\": \"#/properties/myInt2\","
2564 " \"expected\": [\"integer\"], \"actual\": \"null\""
2565 "}}",
2566 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2567 }
2568
2569 // $ref is a non-JSON pointer fragment and there is a matching id within array
2570 TEST(SchemaValidator, Ref_internal_id_in_array) {
2571 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2572 Document sd;
2573 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myInt2\": {\"anyOf\": [{\"type\": \"string\", \"id\": \"#myStrId\"}, {\"type\": \"integer\", \"id\": \"#myId\"}]}}}");
2574 SchemaDocumentType s(sd);
2575 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2576 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2577 INVALIDATE_(s, "{\"myInt1\": null}", "/properties/myInt2/anyOf/1", "type", "/myInt1",
2578 "{ \"type\": {"
2579 " \"errorCode\": 20,"
2580 " \"instanceRef\": \"#/myInt1\","
2581 " \"schemaRef\": \"#/properties/myInt2/anyOf/1\","
2582 " \"expected\": [\"integer\"], \"actual\": \"null\""
2583 "}}",
2584 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2585 }
2586
2587 // $ref is a non-JSON pointer fragment and there is a matching id, and the schema is embedded in the document
2588 TEST(SchemaValidator, Ref_internal_id_and_schema_pointer) {
2589 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2590 Document sd;
2591 sd.Parse("{ \"schema\": {\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myInt2\": {\"anyOf\": [{\"type\": \"integer\", \"id\": \"#myId\"}]}}}}");
2592 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2593 SchemaDocumentType s(sd, 0, 0, 0, 0, PointerType("/schema"));
2594 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2595 INVALIDATE_(s, "{\"myInt1\": null}", "/schema/properties/myInt2/anyOf/0", "type", "/myInt1",
2596 "{ \"type\": {"
2597 " \"errorCode\": 20,"
2598 " \"instanceRef\": \"#/myInt1\","
2599 " \"schemaRef\": \"#/schema/properties/myInt2/anyOf/0\","
2600 " \"expected\": [\"integer\"], \"actual\": \"null\""
2601 "}}",
2602 kValidateDefaultFlags, SchemaValidatorType, PointerType);
2603 }
2604
2605 // Test that $refs are correctly resolved when intermediate multiple ids are present
2606 // Includes $ref to a part of the document with a different in-scope id, which also contains $ref..
2607 TEST(SchemaValidator, Ref_internal_multiple_ids) {
2608 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2609 //RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2610 CrtAllocator allocator;
2611 char* schema = ReadFile("unittestschema/idandref.json", allocator);
2612 Document sd;
2613 sd.Parse(schema);
2614 ASSERT_FALSE(sd.HasParseError());
2615 SchemaDocumentType s(sd, "http://xyz", 10/*, &provider*/);
2616 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2617 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2618 INVALIDATE_(s, "{\"PA1\": \"s\", \"PA2\": \"t\", \"PA3\": \"r\", \"PX1\": 1, \"PX2Y\": 2, \"PX3Z\": 3, \"PX4\": 4, \"PX5\": 5, \"PX6\": 6, \"PX7W\": 7, \"PX8N\": { \"NX\": 8}}", "#", "errors", "#",
2619 "{ \"type\": ["
2620 " {\"errorCode\": 20, \"instanceRef\": \"#/PA1\", \"schemaRef\": \"http://xyz#/definitions/A\", \"expected\": [\"integer\"], \"actual\": \"string\"},"
2621 " {\"errorCode\": 20, \"instanceRef\": \"#/PA2\", \"schemaRef\": \"http://xyz#/definitions/A\", \"expected\": [\"integer\"], \"actual\": \"string\"},"
2622 " {\"errorCode\": 20, \"instanceRef\": \"#/PA3\", \"schemaRef\": \"http://xyz#/definitions/A\", \"expected\": [\"integer\"], \"actual\": \"string\"},"
2623 " {\"errorCode\": 20, \"instanceRef\": \"#/PX1\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
2624 " {\"errorCode\": 20, \"instanceRef\": \"#/PX2Y\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
2625 " {\"errorCode\": 20, \"instanceRef\": \"#/PX3Z\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
2626 " {\"errorCode\": 20, \"instanceRef\": \"#/PX4\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
2627 " {\"errorCode\": 20, \"instanceRef\": \"#/PX5\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
2628 " {\"errorCode\": 20, \"instanceRef\": \"#/PX6\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
2629 " {\"errorCode\": 20, \"instanceRef\": \"#/PX7W\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"},"
2630 " {\"errorCode\": 20, \"instanceRef\": \"#/PX8N/NX\", \"schemaRef\": \"http://xyz#/definitions/B/definitions/X\", \"expected\": [\"boolean\"], \"actual\": \"integer\"}"
2631 "]}",
2632 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidatorType, PointerType);
2633 CrtAllocator::Free(schema);
2634 }
2635
2636 TEST(SchemaValidator, Ref_remote_issue1210) {
2637 class SchemaDocumentProvider : public IRemoteSchemaDocumentProvider {
2638 SchemaDocument** collection;
2639
2640 // Dummy private copy constructor & assignment operator.
2641 // Function bodies added so that they compile in MSVC 2019.
2642 SchemaDocumentProvider(const SchemaDocumentProvider&) : collection(NULL) {
2643 }
2644 SchemaDocumentProvider& operator=(const SchemaDocumentProvider&) {
2645 return *this;
2646 }
2647
2648 public:
2649 SchemaDocumentProvider(SchemaDocument** collection) : collection(collection) { }
2650 virtual const SchemaDocument* GetRemoteDocument(const char* uri, SizeType length) {
2651 int i = 0;
2652 while (collection[i] && SchemaDocument::GValue(uri, length) != collection[i]->GetURI()) ++i;
2653 return collection[i];
2654 }
2655 };
2656 SchemaDocument* collection[] = { 0, 0, 0 };
2657 SchemaDocumentProvider provider(collection);
2658
2659 Document x, y, z;
2660 x.Parse("{\"properties\":{\"country\":{\"$ref\":\"y.json#/definitions/country_remote\"}},\"type\":\"object\"}");
2661 y.Parse("{\"definitions\":{\"country_remote\":{\"$ref\":\"z.json#/definitions/country_list\"}}}");
2662 z.Parse("{\"definitions\":{\"country_list\":{\"enum\":[\"US\"]}}}");
2663
2664 SchemaDocument sz(z, "z.json", 6, &provider);
2665 collection[0] = &sz;
2666 SchemaDocument sy(y, "y.json", 6, &provider);
2667 collection[1] = &sy;
2668 SchemaDocument sx(x, "x.json", 6, &provider);
2669
2670 VALIDATE(sx, "{\"country\":\"UK\"}", false);
2671 VALIDATE(sx, "{\"country\":\"US\"}", true);
2672 }
2673
2674 // Test that when kValidateContinueOnErrorFlag is set, all errors are reported.
2675 TEST(SchemaValidator, ContinueOnErrors) {
2676 CrtAllocator allocator;
2677 char* schema = ReadFile("unittestschema/address.json", allocator);
2678 Document sd;
2679 sd.Parse(schema);
2680 ASSERT_FALSE(sd.HasParseError());
2681 SchemaDocument s(sd);
2682 VALIDATE(s, "{\"version\": 1.0, \"address\": {\"number\": 24, \"street1\": \"The Woodlands\", \"street3\": \"Ham\", \"city\": \"Romsey\", \"area\": \"Kent\", \"country\": \"UK\", \"postcode\": \"SO51 0GP\"}, \"phones\": [\"0111-222333\", \"0777-666888\"], \"names\": [\"Fred\", \"Bloggs\"]}", true);
2683 INVALIDATE_(s, "{\"version\": 1.01, \"address\": {\"number\": 0, \"street2\": false, \"street3\": \"Ham\", \"city\": \"RomseyTownFC\", \"area\": \"Narnia\", \"country\": \"USA\", \"postcode\": \"999ABC\"}, \"phones\": [], \"planet\": \"Earth\", \"extra\": {\"S_xxx\": 123}}", "#", "errors", "#",
2684 "{ \"multipleOf\": {"
2685 " \"errorCode\": 1, \"instanceRef\": \"#/version\", \"schemaRef\": \"#/definitions/decimal_type\", \"expected\": 1.0, \"actual\": 1.01"
2686 " },"
2687 " \"minimum\": {"
2688 " \"errorCode\": 5, \"instanceRef\": \"#/address/number\", \"schemaRef\": \"#/definitions/positiveInt_type\", \"expected\": 0, \"actual\": 0, \"exclusiveMinimum\": true"
2689 " },"
2690 " \"type\": ["
2691 " {\"expected\": [\"null\", \"string\"], \"actual\": \"boolean\", \"errorCode\": 20, \"instanceRef\": \"#/address/street2\", \"schemaRef\": \"#/definitions/address_type/properties/street2\"},"
2692 " {\"expected\": [\"string\"], \"actual\": \"integer\", \"errorCode\": 20, \"instanceRef\": \"#/extra/S_xxx\", \"schemaRef\": \"#/properties/extra/patternProperties/%5ES_\"}"
2693 " ],"
2694 " \"maxLength\": {"
2695 " \"actual\": \"RomseyTownFC\", \"expected\": 10, \"errorCode\": 6, \"instanceRef\": \"#/address/city\", \"schemaRef\": \"#/definitions/address_type/properties/city\""
2696 " },"
2697 " \"anyOf\": {"
2698 " \"errors\":["
2699 " {\"pattern\": {\"actual\": \"999ABC\", \"errorCode\": 8, \"instanceRef\": \"#/address/postcode\", \"schemaRef\": \"#/definitions/address_type/properties/postcode/anyOf/0\"}},"
2700 " {\"pattern\": {\"actual\": \"999ABC\", \"errorCode\": 8, \"instanceRef\": \"#/address/postcode\", \"schemaRef\": \"#/definitions/address_type/properties/postcode/anyOf/1\"}}"
2701 " ],"
2702 " \"errorCode\": 24, \"instanceRef\": \"#/address/postcode\", \"schemaRef\": \"#/definitions/address_type/properties/postcode\""
2703 " },"
2704 " \"allOf\": {"
2705 " \"errors\":["
2706 " {\"enum\":{\"errorCode\":19,\"instanceRef\":\"#/address/country\",\"schemaRef\":\"#/definitions/country_type\"}}"
2707 " ],"
2708 " \"errorCode\":23,\"instanceRef\":\"#/address/country\",\"schemaRef\":\"#/definitions/address_type/properties/country\""
2709 " },"
2710 " \"minItems\": {"
2711 " \"actual\": 0, \"expected\": 1, \"errorCode\": 10, \"instanceRef\": \"#/phones\", \"schemaRef\": \"#/properties/phones\""
2712 " },"
2713 " \"additionalProperties\": {"
2714 " \"disallowed\": \"planet\", \"errorCode\": 16, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
2715 " },"
2716 " \"required\": {"
2717 " \"missing\": [\"street1\"], \"errorCode\": 15, \"instanceRef\": \"#/address\", \"schemaRef\": \"#/definitions/address_type\""
2718 " },"
2719 " \"oneOf\": {"
2720 " \"matches\": [0, 1], \"errorCode\": 22, \"instanceRef\": \"#/address/area\", \"schemaRef\": \"#/definitions/address_type/properties/area\""
2721 " }"
2722 "}",
2723 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2724 INVALIDATE_(s, "{\"address\": {\"number\": 200, \"street1\": {}, \"street3\": null, \"city\": \"Rom\", \"area\": \"Dorset\", \"postcode\": \"SO51 0GP\"}, \"phones\": [\"0111-222333\", \"0777-666888\", \"0777-666888\"], \"names\": [\"Fred\", \"S\", \"M\", \"Bloggs\"]}", "#", "errors", "#",
2725 "{ \"maximum\": {"
2726 " \"errorCode\": 3, \"instanceRef\": \"#/address/number\", \"schemaRef\": \"#/definitions/positiveInt_type\", \"expected\": 100, \"actual\": 200, \"exclusiveMaximum\": true"
2727 " },"
2728 " \"type\": {"
2729 " \"expected\": [\"string\"], \"actual\": \"object\", \"errorCode\": 20, \"instanceRef\": \"#/address/street1\", \"schemaRef\": \"#/definitions/address_type/properties/street1\""
2730 " },"
2731 " \"not\": {"
2732 " \"errorCode\": 25, \"instanceRef\": \"#/address/street3\", \"schemaRef\": \"#/definitions/address_type/properties/street3\""
2733 " },"
2734 " \"minLength\": {"
2735 " \"actual\": \"Rom\", \"expected\": 4, \"errorCode\": 7, \"instanceRef\": \"#/address/city\", \"schemaRef\": \"#/definitions/address_type/properties/city\""
2736 " },"
2737 " \"maxItems\": {"
2738 " \"actual\": 3, \"expected\": 2, \"errorCode\": 9, \"instanceRef\": \"#/phones\", \"schemaRef\": \"#/properties/phones\""
2739 " },"
2740 " \"uniqueItems\": {"
2741 " \"duplicates\": [1, 2], \"errorCode\": 11, \"instanceRef\": \"#/phones\", \"schemaRef\": \"#/properties/phones\""
2742 " },"
2743 " \"minProperties\": {\"actual\": 6, \"expected\": 7, \"errorCode\": 14, \"instanceRef\": \"#/address\", \"schemaRef\": \"#/definitions/address_type\""
2744 " },"
2745 " \"additionalItems\": ["
2746 " {\"disallowed\": 2, \"errorCode\": 12, \"instanceRef\": \"#/names\", \"schemaRef\": \"#/properties/names\"},"
2747 " {\"disallowed\": 3, \"errorCode\": 12, \"instanceRef\": \"#/names\", \"schemaRef\": \"#/properties/names\"}"
2748 " ],"
2749 " \"dependencies\": {"
2750 " \"errors\": {"
2751 " \"address\": {\"required\": {\"missing\": [\"version\"], \"errorCode\": 15, \"instanceRef\": \"#\", \"schemaRef\": \"#/dependencies/address\"}},"
2752 " \"names\": {\"required\": {\"missing\": [\"version\"], \"errorCode\": 15, \"instanceRef\": \"#\", \"schemaRef\": \"#/dependencies/names\"}}"
2753 " },"
2754 " \"errorCode\": 18, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
2755 " },"
2756 " \"oneOf\": {"
2757 " \"errors\": ["
2758 " {\"enum\": {\"errorCode\": 19, \"instanceRef\": \"#/address/area\", \"schemaRef\": \"#/definitions/county_type\"}},"
2759 " {\"enum\": {\"errorCode\": 19, \"instanceRef\": \"#/address/area\", \"schemaRef\": \"#/definitions/province_type\"}}"
2760 " ],"
2761 " \"errorCode\": 21, \"instanceRef\": \"#/address/area\", \"schemaRef\": \"#/definitions/address_type/properties/area\""
2762 " }"
2763 "}",
2764 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2765
2766 CrtAllocator::Free(schema);
2767 }
2768
2769 // Test that when kValidateContinueOnErrorFlag is set, it is not propagated to oneOf sub-validator so we only get the first error.
2770 TEST(SchemaValidator, ContinueOnErrors_OneOf) {
2771 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2772 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2773 CrtAllocator allocator;
2774 char* schema = ReadFile("unittestschema/oneOf_address.json", allocator);
2775 Document sd;
2776 sd.Parse(schema);
2777 ASSERT_FALSE(sd.HasParseError());
2778 SchemaDocumentType s(sd, 0, 0, &provider);
2779 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2780 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2781 INVALIDATE_(s, "{\"version\": 1.01, \"address\": {\"number\": 0, \"street2\": false, \"street3\": \"Ham\", \"city\": \"RomseyTownFC\", \"area\": \"BC\", \"country\": \"USA\", \"postcode\": \"999ABC\"}, \"phones\": [], \"planet\": \"Earth\", \"extra\": {\"S_xxx\": 123}}", "#", "errors", "#",
2782 "{ \"oneOf\": {"
2783 " \"errors\": [{"
2784 " \"multipleOf\": {"
2785 " \"errorCode\": 1, \"instanceRef\": \"#/version\", \"schemaRef\": \"http://localhost:1234/address.json#/definitions/decimal_type\", \"expected\": 1.0, \"actual\": 1.01"
2786 " }"
2787 " }],"
2788 " \"errorCode\": 21, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
2789 " }"
2790 "}",
2791 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidatorType, PointerType);
2792 CrtAllocator::Free(schema);
2793 }
2794
2795 // Test that when kValidateContinueOnErrorFlag is set, it is not propagated to allOf sub-validator so we only get the first error.
2796 TEST(SchemaValidator, ContinueOnErrors_AllOf) {
2797 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2798 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2799 CrtAllocator allocator;
2800 char* schema = ReadFile("unittestschema/allOf_address.json", allocator);
2801 Document sd;
2802 sd.Parse(schema);
2803 ASSERT_FALSE(sd.HasParseError());
2804 SchemaDocumentType s(sd, 0, 0, &provider);
2805 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2806 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2807 INVALIDATE_(s, "{\"version\": 1.01, \"address\": {\"number\": 0, \"street2\": false, \"street3\": \"Ham\", \"city\": \"RomseyTownFC\", \"area\": \"BC\", \"country\": \"USA\", \"postcode\": \"999ABC\"}, \"phones\": [], \"planet\": \"Earth\", \"extra\": {\"S_xxx\": 123}}", "#", "errors", "#",
2808 "{ \"allOf\": {"
2809 " \"errors\": [{"
2810 " \"multipleOf\": {"
2811 " \"errorCode\": 1, \"instanceRef\": \"#/version\", \"schemaRef\": \"http://localhost:1234/address.json#/definitions/decimal_type\", \"expected\": 1.0, \"actual\": 1.01"
2812 " }"
2813 " }],"
2814 " \"errorCode\": 23, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
2815 " }"
2816 "}",
2817 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidatorType, PointerType);
2818 CrtAllocator::Free(schema);
2819 }
2820
2821 // Test that when kValidateContinueOnErrorFlag is set, it is not propagated to anyOf sub-validator so we only get the first error.
2822 TEST(SchemaValidator, ContinueOnErrors_AnyOf) {
2823 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
2824 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
2825 CrtAllocator allocator;
2826 char* schema = ReadFile("unittestschema/anyOf_address.json", allocator);
2827 Document sd;
2828 sd.Parse(schema);
2829 ASSERT_FALSE(sd.HasParseError());
2830 SchemaDocumentType s(sd, 0, 0, &provider);
2831 typedef GenericSchemaValidator<SchemaDocumentType, BaseReaderHandler<UTF8<> >, MemoryPoolAllocator<> > SchemaValidatorType;
2832 typedef GenericPointer<Value, MemoryPoolAllocator<> > PointerType;
2833 INVALIDATE_(s, "{\"version\": 1.01, \"address\": {\"number\": 0, \"street2\": false, \"street3\": \"Ham\", \"city\": \"RomseyTownFC\", \"area\": \"BC\", \"country\": \"USA\", \"postcode\": \"999ABC\"}, \"phones\": [], \"planet\": \"Earth\", \"extra\": {\"S_xxx\": 123}}", "#", "errors", "#",
2834 "{ \"anyOf\": {"
2835 " \"errors\": [{"
2836 " \"multipleOf\": {"
2837 " \"errorCode\": 1, \"instanceRef\": \"#/version\", \"schemaRef\": \"http://localhost:1234/address.json#/definitions/decimal_type\", \"expected\": 1.0, \"actual\": 1.01"
2838 " }"
2839 " }],"
2840 " \"errorCode\": 24, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
2841 " }"
2842 "}",
2843 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidatorType, PointerType);
2844
2845 CrtAllocator::Free(schema);
2846 }
2847
2848 // Test that when kValidateContinueOnErrorFlag is set, arrays with uniqueItems:true are correctly processed when an item is invalid.
2849 // This tests that we don't blow up if a hasher does not get created.
2850 TEST(SchemaValidator, ContinueOnErrors_UniqueItems) {
2851 CrtAllocator allocator;
2852 char* schema = ReadFile("unittestschema/address.json", allocator);
2853 Document sd;
2854 sd.Parse(schema);
2855 ASSERT_FALSE(sd.HasParseError());
2856 SchemaDocument s(sd);
2857 VALIDATE(s, "{\"phones\":[\"12-34\",\"56-78\"]}", true);
2858 INVALIDATE_(s, "{\"phones\":[\"12-34\",\"12-34\"]}", "#", "errors", "#",
2859 "{\"uniqueItems\": {\"duplicates\": [0,1], \"errorCode\": 11, \"instanceRef\": \"#/phones\", \"schemaRef\": \"#/properties/phones\"}}",
2860 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2861 INVALIDATE_(s, "{\"phones\":[\"ab-34\",\"cd-78\"]}", "#", "errors", "#",
2862 "{\"pattern\": ["
2863 " {\"actual\": \"ab-34\", \"errorCode\": 8, \"instanceRef\": \"#/phones/0\", \"schemaRef\": \"#/definitions/phone_type\"},"
2864 " {\"actual\": \"cd-78\", \"errorCode\": 8, \"instanceRef\": \"#/phones/1\", \"schemaRef\": \"#/definitions/phone_type\"}"
2865 "]}",
2866 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2867 CrtAllocator::Free(schema);
2868 }
2869
2870 // Test that when kValidateContinueOnErrorFlag is set, an enum field is correctly processed when it has an invalid value.
2871 // This tests that we don't blow up if a hasher does not get created.
2872 TEST(SchemaValidator, ContinueOnErrors_Enum) {
2873 CrtAllocator allocator;
2874 char* schema = ReadFile("unittestschema/address.json", allocator);
2875 Document sd;
2876 sd.Parse(schema);
2877 ASSERT_FALSE(sd.HasParseError());
2878 SchemaDocument s(sd);
2879 VALIDATE(s, "{\"gender\":\"M\"}", true);
2880 INVALIDATE_(s, "{\"gender\":\"X\"}", "#", "errors", "#",
2881 "{\"enum\": {\"errorCode\": 19, \"instanceRef\": \"#/gender\", \"schemaRef\": \"#/properties/gender\"}}",
2882 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2883 INVALIDATE_(s, "{\"gender\":1}", "#", "errors", "#",
2884 "{\"type\": {\"expected\":[\"string\"], \"actual\": \"integer\", \"errorCode\": 20, \"instanceRef\": \"#/gender\", \"schemaRef\": \"#/properties/gender\"}}",
2885 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2886 CrtAllocator::Free(schema);
2887 }
2888
2889 // Test that when kValidateContinueOnErrorFlag is set, an array appearing for an object property is handled
2890 // This tests that we don't blow up when there is a type mismatch.
2891 TEST(SchemaValidator, ContinueOnErrors_RogueArray) {
2892 CrtAllocator allocator;
2893 char* schema = ReadFile("unittestschema/address.json", allocator);
2894 Document sd;
2895 sd.Parse(schema);
2896 ASSERT_FALSE(sd.HasParseError());
2897 SchemaDocument s(sd);
2898 INVALIDATE_(s, "{\"address\":[{\"number\": 0}]}", "#", "errors", "#",
2899 "{\"type\": {\"expected\":[\"object\"], \"actual\": \"array\", \"errorCode\": 20, \"instanceRef\": \"#/address\", \"schemaRef\": \"#/definitions/address_type\"},"
2900 " \"dependencies\": {"
2901 " \"errors\": {"
2902 " \"address\": {\"required\": {\"missing\": [\"version\"], \"errorCode\": 15, \"instanceRef\": \"#\", \"schemaRef\": \"#/dependencies/address\"}}"
2903 " },\"errorCode\": 18, \"instanceRef\": \"#\", \"schemaRef\": \"#\"}}",
2904 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2905 CrtAllocator::Free(schema);
2906 }
2907
2908 // Test that when kValidateContinueOnErrorFlag is set, an object appearing for an array property is handled
2909 // This tests that we don't blow up when there is a type mismatch.
2910 TEST(SchemaValidator, ContinueOnErrors_RogueObject) {
2911 CrtAllocator allocator;
2912 char* schema = ReadFile("unittestschema/address.json", allocator);
2913 Document sd;
2914 sd.Parse(schema);
2915 ASSERT_FALSE(sd.HasParseError());
2916 SchemaDocument s(sd);
2917 INVALIDATE_(s, "{\"phones\":{\"number\": 0}}", "#", "errors", "#",
2918 "{\"type\": {\"expected\":[\"array\"], \"actual\": \"object\", \"errorCode\": 20, \"instanceRef\": \"#/phones\", \"schemaRef\": \"#/properties/phones\"}}",
2919 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2920 CrtAllocator::Free(schema);
2921 }
2922
2923 // Test that when kValidateContinueOnErrorFlag is set, a string appearing for an array or object property is handled
2924 // This tests that we don't blow up when there is a type mismatch.
2925 TEST(SchemaValidator, ContinueOnErrors_RogueString) {
2926 CrtAllocator allocator;
2927 char* schema = ReadFile("unittestschema/address.json", allocator);
2928 Document sd;
2929 sd.Parse(schema);
2930 ASSERT_FALSE(sd.HasParseError());
2931 SchemaDocument s(sd);
2932 INVALIDATE_(s, "{\"address\":\"number\"}", "#", "errors", "#",
2933 "{\"type\": {\"expected\":[\"object\"], \"actual\": \"string\", \"errorCode\": 20, \"instanceRef\": \"#/address\", \"schemaRef\": \"#/definitions/address_type\"},"
2934 " \"dependencies\": {"
2935 " \"errors\": {"
2936 " \"address\": {\"required\": {\"missing\": [\"version\"], \"errorCode\": 15, \"instanceRef\": \"#\", \"schemaRef\": \"#/dependencies/address\"}}"
2937 " },\"errorCode\": 18, \"instanceRef\": \"#\", \"schemaRef\": \"#\"}}",
2938 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2939 INVALIDATE_(s, "{\"phones\":\"number\"}", "#", "errors", "#",
2940 "{\"type\": {\"expected\":[\"array\"], \"actual\": \"string\", \"errorCode\": 20, \"instanceRef\": \"#/phones\", \"schemaRef\": \"#/properties/phones\"}}",
2941 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2942 CrtAllocator::Free(schema);
2943 }
2944
2945 // Test that when kValidateContinueOnErrorFlag is set, an incorrect simple type with a sub-schema is handled correctly.
2946 // This tests that we don't blow up when there is a type mismatch but there is a sub-schema present
2947 TEST(SchemaValidator, ContinueOnErrors_BadSimpleType) {
2948 Document sd;
2949 sd.Parse("{\"type\":\"string\", \"anyOf\":[{\"maxLength\":2}]}");
2950 ASSERT_FALSE(sd.HasParseError());
2951 SchemaDocument s(sd);
2952 VALIDATE(s, "\"AB\"", true);
2953 INVALIDATE_(s, "\"ABC\"", "#", "errors", "#",
2954 "{ \"anyOf\": {"
2955 " \"errors\": [{"
2956 " \"maxLength\": {"
2957 " \"errorCode\": 6, \"instanceRef\": \"#\", \"schemaRef\": \"#/anyOf/0\", \"expected\": 2, \"actual\": \"ABC\""
2958 " }"
2959 " }],"
2960 " \"errorCode\": 24, \"instanceRef\": \"#\", \"schemaRef\": \"#\""
2961 " }"
2962 "}",
2963 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2964 // Invalid type
2965 INVALIDATE_(s, "333", "#", "errors", "#",
2966 "{ \"type\": {"
2967 " \"errorCode\": 20, \"instanceRef\": \"#\", \"schemaRef\": \"#\", \"expected\": [\"string\"], \"actual\": \"integer\""
2968 " }"
2969 "}",
2970 kValidateDefaultFlags | kValidateContinueOnErrorFlag, SchemaValidator, Pointer);
2971 }
2972
2973
2974 TEST(SchemaValidator, UnknownValidationError) {
2975 ASSERT_TRUE(SchemaValidator::SchemaType::GetValidateErrorKeyword(kValidateErrors).GetString() == std::string("null"));
2976 }
2977
2978 // The first occurrence of a duplicate keyword is taken
2979 TEST(SchemaValidator, DuplicateKeyword) {
2980 Document sd;
2981 sd.Parse("{ \"title\": \"test\",\"type\": \"number\", \"type\": \"string\" }");
2982 EXPECT_FALSE(sd.HasParseError());
2983 SchemaDocument s(sd);
2984 VALIDATE(s, "42", true);
2985 INVALIDATE(s, "\"Life, the universe, and everything\"", "", "type", "",
2986 "{ \"type\": {"
2987 " \"errorCode\": 20,"
2988 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
2989 " \"expected\": [\"number\"], \"actual\": \"string\""
2990 "}}");
2991 }
2992
2993
2994 // SchemaDocument tests
2995
2996 // Specification (schema draft, open api version)
2997 TEST(SchemaValidator, Schema_SupportedNotObject) {
2998 Document sd;
2999 sd.Parse("true");
3000 ASSERT_FALSE(sd.HasParseError());
3001 SchemaDocument s(sd);
3002 ASSERT_TRUE(s.IsSupportedSpecification());
3003 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3004 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3005 EXPECT_TRUE(s.GetError().ObjectEmpty());
3006 }
3007
3008 TEST(SchemaValidator, Schema_SupportedNoSpec) {
3009 Document sd;
3010 sd.Parse("{\"type\": \"integer\"}");
3011 ASSERT_FALSE(sd.HasParseError());
3012 SchemaDocument s(sd);
3013 ASSERT_TRUE(s.IsSupportedSpecification());
3014 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3015 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3016 EXPECT_TRUE(s.GetError().ObjectEmpty());
3017 }
3018
3019 TEST(SchemaValidator, Schema_SupportedNoSpecStatic) {
3020 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3021 Document sd;
3022 sd.Parse("{\"type\": \"integer\"}");
3023 ASSERT_FALSE(sd.HasParseError());
3024 Specification spec = SchemaDocumentType::GetSpecification(sd);
3025 ASSERT_FALSE(spec.IsSupported());
3026 ASSERT_TRUE(spec.draft == kDraftNone);
3027 ASSERT_TRUE(spec.oapi == kVersionNone);
3028 }
3029
3030 TEST(SchemaValidator, Schema_SupportedDraft5Static) {
3031 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3032 Document sd;
3033 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-05/schema#\", \"type\": \"integer\"}");
3034 ASSERT_FALSE(sd.HasParseError());
3035 Specification spec = SchemaDocumentType::GetSpecification(sd);
3036 ASSERT_TRUE(spec.IsSupported());
3037 ASSERT_TRUE(spec.draft == kDraft05);
3038 ASSERT_TRUE(spec.oapi == kVersionNone);
3039 }
3040
3041 TEST(SchemaValidator, Schema_SupportedDraft4) {
3042 Document sd;
3043 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-04/schema#\", \"type\": \"integer\"}");
3044 ASSERT_FALSE(sd.HasParseError());
3045 SchemaDocument s(sd);
3046 ASSERT_TRUE(s.IsSupportedSpecification());
3047 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3048 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3049 EXPECT_TRUE(s.GetError().ObjectEmpty());
3050 }
3051
3052 TEST(SchemaValidator, Schema_SupportedDraft4NoFrag) {
3053 Document sd;
3054 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-04/schema\", \"type\": \"integer\"}");
3055 ASSERT_FALSE(sd.HasParseError());
3056 SchemaDocument s(sd);
3057 ASSERT_TRUE(s.IsSupportedSpecification());
3058 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3059 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3060 EXPECT_TRUE(s.GetError().ObjectEmpty());
3061 }
3062
3063 TEST(SchemaValidator, Schema_SupportedDraft5) {
3064 Document sd;
3065 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-05/schema#\", \"type\": \"integer\"}");
3066 ASSERT_FALSE(sd.HasParseError());
3067 SchemaDocument s(sd);
3068 ASSERT_TRUE(s.IsSupportedSpecification());
3069 ASSERT_TRUE(s.GetSpecification().draft == kDraft05);
3070 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3071 EXPECT_TRUE(s.GetError().ObjectEmpty());
3072 }
3073
3074 TEST(SchemaValidator, Schema_SupportedDraft5NoFrag) {
3075 Document sd;
3076 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-05/schema\", \"type\": \"integer\"}");
3077 ASSERT_FALSE(sd.HasParseError());
3078 SchemaDocument s(sd);
3079 ASSERT_TRUE(s.IsSupportedSpecification());
3080 ASSERT_TRUE(s.GetSpecification().draft == kDraft05);
3081 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3082 EXPECT_TRUE(s.GetError().ObjectEmpty());
3083 }
3084
3085 TEST(SchemaValidator, Schema_IgnoreDraftEmbedded) {
3086 Document sd;
3087 sd.Parse("{\"root\": {\"$schema\":\"http://json-schema.org/draft-05/schema#\", \"type\": \"integer\"}}");
3088 ASSERT_FALSE(sd.HasParseError());
3089 SchemaDocument s(sd, 0, 0, 0, 0, SchemaDocument::PointerType("/root"));
3090 ASSERT_TRUE(s.IsSupportedSpecification());
3091 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3092 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3093 EXPECT_TRUE(s.GetError().ObjectEmpty());
3094 }
3095
3096 TEST(SchemaValidator, Schema_SupportedDraftOverride) {
3097 Document sd;
3098 sd.Parse("{\"type\": \"integer\"}");
3099 ASSERT_FALSE(sd.HasParseError());
3100 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kDraft04));
3101 ASSERT_TRUE(s.IsSupportedSpecification());
3102 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3103 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3104 EXPECT_TRUE(s.GetError().ObjectEmpty());
3105 }
3106
3107 TEST(SchemaValidator, Schema_UnknownDraftOverride) {
3108 Document sd;
3109 sd.Parse("{\"type\": \"integer\"}");
3110 ASSERT_FALSE(sd.HasParseError());
3111 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kDraftUnknown));
3112 ASSERT_FALSE(s.IsSupportedSpecification());
3113 ASSERT_TRUE(s.GetSpecification().draft == kDraftUnknown);
3114 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3115 SCHEMAERROR(s, "{\"SpecUnknown\":{\"errorCode\":10,\"instanceRef\":\"#\"}}");
3116 }
3117
3118 TEST(SchemaValidator, Schema_UnsupportedDraftOverride) {
3119 Document sd;
3120 sd.Parse("{\"type\": \"integer\"}");
3121 ASSERT_FALSE(sd.HasParseError());
3122 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kDraft03));
3123 ASSERT_FALSE(s.IsSupportedSpecification());
3124 ASSERT_TRUE(s.GetSpecification().draft == kDraft03);
3125 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3126 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3127 }
3128
3129 TEST(SchemaValidator, Schema_UnknownDraft) {
3130 Document sd;
3131 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-xxx/schema#\", \"type\": \"integer\"}");
3132 ASSERT_FALSE(sd.HasParseError());
3133 SchemaDocument s(sd);
3134 ASSERT_FALSE(s.IsSupportedSpecification());
3135 ASSERT_TRUE(s.GetSpecification().draft == kDraftUnknown);
3136 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3137 SCHEMAERROR(s, "{\"SpecUnknown\":{\"errorCode\":10,\"instanceRef\":\"#\"}}");
3138 }
3139
3140 TEST(SchemaValidator, Schema_UnknownDraftNotString) {
3141 Document sd;
3142 sd.Parse("{\"$schema\": 4, \"type\": \"integer\"}");
3143 ASSERT_FALSE(sd.HasParseError());
3144 SchemaDocument s(sd);
3145 ASSERT_FALSE(s.IsSupportedSpecification());
3146 ASSERT_TRUE(s.GetSpecification().draft == kDraftUnknown);
3147 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3148 SCHEMAERROR(s, "{\"SpecUnknown\":{\"errorCode\":10,\"instanceRef\":\"#\"}}");
3149 }
3150
3151 TEST(SchemaValidator, Schema_UnsupportedDraft3) {
3152 Document sd;
3153 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-03/schema#\", \"type\": \"integer\"}");
3154 ASSERT_FALSE(sd.HasParseError());
3155 SchemaDocument s(sd);
3156 ASSERT_FALSE(s.IsSupportedSpecification());
3157 ASSERT_TRUE(s.GetSpecification().draft == kDraft03);
3158 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3159 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3160 }
3161
3162 TEST(SchemaValidator, Schema_UnsupportedDraft6) {
3163 Document sd;
3164 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-06/schema#\", \"type\": \"integer\"}");
3165 ASSERT_FALSE(sd.HasParseError());
3166 SchemaDocument s(sd);
3167 ASSERT_FALSE(s.IsSupportedSpecification());
3168 ASSERT_TRUE(s.GetSpecification().draft == kDraft06);
3169 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3170 }
3171
3172 TEST(SchemaValidator, Schema_UnsupportedDraft7) {
3173 Document sd;
3174 sd.Parse("{\"$schema\":\"http://json-schema.org/draft-07/schema#\", \"type\": \"integer\"}");
3175 ASSERT_FALSE(sd.HasParseError());
3176 SchemaDocument s(sd);
3177 ASSERT_FALSE(s.IsSupportedSpecification());
3178 ASSERT_TRUE(s.GetSpecification().draft == kDraft07);
3179 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3180 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3181 }
3182
3183 TEST(SchemaValidator, Schema_UnsupportedDraft2019_09) {
3184 Document sd;
3185 sd.Parse("{\"$schema\":\"https://json-schema.org/draft/2019-09/schema\", \"type\": \"integer\"}");
3186 ASSERT_FALSE(sd.HasParseError());
3187 SchemaDocument s(sd);
3188 ASSERT_FALSE(s.IsSupportedSpecification());
3189 ASSERT_TRUE(s.GetSpecification().draft == kDraft2019_09);
3190 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3191 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3192 }
3193
3194 TEST(SchemaValidator, Schema_UnsupportedDraft2020_12) {
3195 Document sd;
3196 sd.Parse("{\"$schema\":\"https://json-schema.org/draft/2020-12/schema\", \"type\": \"integer\"}");
3197 ASSERT_FALSE(sd.HasParseError());
3198 SchemaDocument s(sd);
3199 ASSERT_FALSE(s.IsSupportedSpecification());
3200 ASSERT_TRUE(s.GetSpecification().draft == kDraft2020_12);
3201 ASSERT_TRUE(s.GetSpecification().oapi == kVersionNone);
3202 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3203 }
3204
3205 TEST(SchemaValidator, Schema_SupportedVersion20Static) {
3206 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3207 Document sd;
3208 sd.Parse("{\"swagger\":\"2.0\"}");
3209 ASSERT_FALSE(sd.HasParseError());
3210 Specification spec = SchemaDocumentType::GetSpecification(sd);
3211 ASSERT_TRUE(spec.IsSupported());
3212 ASSERT_TRUE(spec.draft == kDraft04);
3213 ASSERT_TRUE(spec.oapi == kVersion20);
3214 }
3215
3216 TEST(SchemaValidator, Schema_SupportedVersion20) {
3217 Document sd;
3218 sd.Parse("{\"swagger\":\"2.0\"}");
3219 ASSERT_FALSE(sd.HasParseError());
3220 SchemaDocument s(sd);
3221 ASSERT_TRUE(s.IsSupportedSpecification());
3222 ASSERT_TRUE(s.GetSpecification().oapi == kVersion20);
3223 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3224 EXPECT_TRUE(s.GetError().ObjectEmpty());
3225 }
3226
3227 TEST(SchemaValidator, Schema_SupportedVersion30x) {
3228 Document sd;
3229 sd.Parse("{\"openapi\":\"3.0.0\"}");
3230 ASSERT_FALSE(sd.HasParseError());
3231 SchemaDocument s(sd);
3232 ASSERT_TRUE(s.IsSupportedSpecification());
3233 ASSERT_TRUE(s.GetSpecification().oapi == kVersion30);
3234 ASSERT_TRUE(s.GetSpecification().draft == kDraft05);
3235 EXPECT_TRUE(s.GetError().ObjectEmpty());
3236 }
3237
3238 TEST(SchemaValidator, Schema_SupportedVersionOverride) {
3239 Document sd;
3240 sd.Parse("{\"type\": \"integer\"}");
3241 ASSERT_FALSE(sd.HasParseError());
3242 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kVersion20));
3243 ASSERT_TRUE(s.IsSupportedSpecification());
3244 ASSERT_TRUE(s.GetSpecification().oapi == kVersion20);
3245 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3246 EXPECT_TRUE(s.GetError().ObjectEmpty());
3247 }
3248
3249 TEST(SchemaValidator, Schema_UnknownVersionOverride) {
3250 Document sd;
3251 sd.Parse("{\"type\": \"integer\"}");
3252 ASSERT_FALSE(sd.HasParseError());
3253 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kVersionUnknown));
3254 ASSERT_FALSE(s.IsSupportedSpecification());
3255 ASSERT_TRUE(s.GetSpecification().oapi == kVersionUnknown);
3256 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3257 SCHEMAERROR(s, "{\"SpecUnknown\":{\"errorCode\":10,\"instanceRef\":\"#\"}}");
3258 }
3259
3260 TEST(SchemaValidator, Schema_UnsupportedVersionOverride) {
3261 Document sd;
3262 sd.Parse("{\"type\": \"integer\"}");
3263 ASSERT_FALSE(sd.HasParseError());
3264 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kVersion31));
3265 ASSERT_FALSE(s.IsSupportedSpecification());
3266 ASSERT_TRUE(s.GetSpecification().oapi == kVersion31);
3267 ASSERT_TRUE(s.GetSpecification().draft == kDraft2020_12);
3268 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3269 }
3270
3271 TEST(SchemaValidator, Schema_UnknownVersion) {
3272 Document sd;
3273 sd.Parse("{\"openapi\":\"1.0\"}");
3274 ASSERT_FALSE(sd.HasParseError());
3275 SchemaDocument s(sd);
3276 ASSERT_FALSE(s.IsSupportedSpecification());
3277 ASSERT_TRUE(s.GetSpecification().oapi == kVersionUnknown);
3278 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3279 SCHEMAERROR(s, "{\"SpecUnknown\":{\"errorCode\":10,\"instanceRef\":\"#\"}}");
3280 }
3281
3282 TEST(SchemaValidator, Schema_UnknownVersionShort) {
3283 Document sd;
3284 sd.Parse("{\"openapi\":\"3.0.\"}");
3285 ASSERT_FALSE(sd.HasParseError());
3286 SchemaDocument s(sd);
3287 ASSERT_FALSE(s.IsSupportedSpecification());
3288 ASSERT_TRUE(s.GetSpecification().oapi == kVersionUnknown);
3289 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3290 SCHEMAERROR(s, "{\"SpecUnknown\":{\"errorCode\":10,\"instanceRef\":\"#\"}}");
3291 }
3292
3293 TEST(SchemaValidator, Schema_UnknownVersionNotString) {
3294 Document sd;
3295 sd.Parse("{\"swagger\": 2}");
3296 ASSERT_FALSE(sd.HasParseError());
3297 SchemaDocument s(sd);
3298 ASSERT_FALSE(s.IsSupportedSpecification());
3299 ASSERT_TRUE(s.GetSpecification().oapi == kVersionUnknown);
3300 ASSERT_TRUE(s.GetSpecification().draft == kDraft04);
3301 SCHEMAERROR(s, "{\"SpecUnknown\":{\"errorCode\":10,\"instanceRef\":\"#\"}}");
3302 }
3303
3304 TEST(SchemaValidator, Schema_UnsupportedVersion31) {
3305 Document sd;
3306 sd.Parse("{\"openapi\":\"3.1.0\"}");
3307 ASSERT_FALSE(sd.HasParseError());
3308 SchemaDocument s(sd);
3309 ASSERT_FALSE(s.IsSupportedSpecification());
3310 ASSERT_TRUE(s.GetSpecification().oapi == kVersion31);
3311 ASSERT_TRUE(s.GetSpecification().draft == kDraft2020_12);
3312 SCHEMAERROR(s, "{\"SpecUnsupported\":{\"errorCode\":11,\"instanceRef\":\"#\"}}");
3313 }
3314
3315 TEST(SchemaValidator, Schema_DraftAndVersion) {
3316 Document sd;
3317 sd.Parse("{\"swagger\": \"2.0\", \"$schema\": \"http://json-schema.org/draft-04/schema#\"}");
3318 ASSERT_FALSE(sd.HasParseError());
3319 SchemaDocument s(sd);
3320 ASSERT_TRUE(s.IsSupportedSpecification());
3321 SCHEMAERROR(s, "{\"SpecIllegal\":{\"errorCode\":12,\"instanceRef\":\"#\"}}");
3322 }
3323
3324 TEST(SchemaValidator, Schema_StartUnknown) {
3325 Document sd;
3326 sd.Parse("{\"type\": \"integer\"}");
3327 ASSERT_FALSE(sd.HasParseError());
3328 SchemaDocument s(sd, 0, 0, 0, 0, SchemaDocument::PointerType("/nowhere"));
3329 SCHEMAERROR(s, "{\"StartUnknown\":{\"errorCode\":1,\"instanceRef\":\"#\", \"value\":\"#/nowhere\"}}");
3330 }
3331
3332 TEST(SchemaValidator, Schema_MultipleErrors) {
3333 Document sd;
3334 sd.Parse("{\"swagger\": \"foo\", \"$schema\": \"bar\"}");
3335 ASSERT_FALSE(sd.HasParseError());
3336 SchemaDocument s(sd);
3337 SCHEMAERROR(s, "{ \"SpecUnknown\": {\"errorCode\":10,\"instanceRef\":\"#\"},"
3338 " \"SpecIllegal\": {\"errorCode\":12,\"instanceRef\":\"#\"}"
3339 "}");
3340 }
3341
3342 // $ref is a non-JSON pointer fragment - not allowed when OpenAPI
3343 TEST(SchemaValidator, Schema_RefPlainNameOpenApi) {
3344 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3345 Document sd;
3346 sd.Parse("{\"swagger\": \"2.0\", \"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"#myId\"}, \"myStr\": {\"type\": \"string\", \"id\": \"#myStrId\"}, \"myInt2\": {\"type\": \"integer\", \"id\": \"#myId\"}}}");
3347 SchemaDocumentType s(sd);
3348 SCHEMAERROR(s, "{\"RefPlainName\":{\"errorCode\":2,\"instanceRef\":\"#/properties/myInt1\",\"value\":\"#myId\"}}");
3349 }
3350
3351 // $ref is a non-JSON pointer fragment - not allowed when remote document
3352 TEST(SchemaValidator, Schema_RefPlainNameRemote) {
3353 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3354 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
3355 Document sd;
3356 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#plainname\"}}}");
3357 SchemaDocumentType s(sd, "http://localhost:1234/xxxx", 26, &provider);
3358 SCHEMAERROR(s, "{\"RefPlainName\":{\"errorCode\":2,\"instanceRef\":\"#/properties/myInt\",\"value\":\"#plainname\"}}");
3359 }
3360
3361 // $ref is an empty string
3362 TEST(SchemaValidator, Schema_RefEmptyString) {
3363 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3364 Document sd;
3365 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt1\": {\"$ref\": \"\"}}}");
3366 SchemaDocumentType s(sd);
3367 SCHEMAERROR(s, "{\"RefInvalid\":{\"errorCode\":3,\"instanceRef\":\"#/properties/myInt1\"}}");
3368 }
3369
3370 // $ref is remote but no provider
3371 TEST(SchemaValidator, Schema_RefNoRemoteProvider) {
3372 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3373 Document sd;
3374 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#plainname\"}}}");
3375 SchemaDocumentType s(sd, "http://localhost:1234/xxxx", 26, 0);
3376 SCHEMAERROR(s, "{\"RefNoRemoteProvider\":{\"errorCode\":7,\"instanceRef\":\"#/properties/myInt\"}}");
3377 }
3378
3379 // $ref is remote but no schema returned
3380 TEST(SchemaValidator, Schema_RefNoRemoteSchema) {
3381 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3382 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
3383 Document sd;
3384 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/will-not-resolve.json\"}}}");
3385 SchemaDocumentType s(sd, "http://localhost:1234/xxxx", 26, &provider);
3386 SCHEMAERROR(s, "{\"RefNoRemoteSchema\":{\"errorCode\":8,\"instanceRef\":\"#/properties/myInt\",\"value\":\"http://localhost:1234/will-not-resolve.json\"}}");
3387 }
3388
3389 // $ref pointer is invalid
3390 TEST(SchemaValidator, Schema_RefPointerInvalid) {
3391 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3392 Document sd;
3393 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"#/&&&&&\"}}}");
3394 SchemaDocumentType s(sd);
3395 SCHEMAERROR(s, "{\"RefPointerInvalid\":{\"errorCode\":4,\"instanceRef\":\"#/properties/myInt\",\"value\":\"#/&&&&&\",\"offset\":2}}");
3396 }
3397
3398 // $ref is remote and pointer is invalid
3399 TEST(SchemaValidator, Schema_RefPointerInvalidRemote) {
3400 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3401 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
3402 Document sd;
3403 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#/abc&&&&&\"}}}");
3404 SchemaDocumentType s(sd, "http://localhost:1234/xxxx", 26, &provider);
3405 SCHEMAERROR(s, "{\"RefPointerInvalid\":{\"errorCode\":4,\"instanceRef\":\"#/properties/myInt\",\"value\":\"#/abc&&&&&\",\"offset\":5}}");
3406 }
3407
3408 // $ref is unknown non-pointer
3409 TEST(SchemaValidator, Schema_RefUnknownPlainName) {
3410 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3411 Document sd;
3412 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"#plainname\"}}}");
3413 SchemaDocumentType s(sd);
3414 SCHEMAERROR(s, "{\"RefUnknown\":{\"errorCode\":5,\"instanceRef\":\"#/properties/myInt\",\"value\":\"#plainname\"}}");
3415 }
3416
3417 /// $ref is unknown pointer
3418 TEST(SchemaValidator, Schema_RefUnknownPointer) {
3419 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3420 Document sd;
3421 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"#/a/b\"}}}");
3422 SchemaDocumentType s(sd);
3423 SCHEMAERROR(s, "{\"RefUnknown\":{\"errorCode\":5,\"instanceRef\":\"#/properties/myInt\",\"value\":\"#/a/b\"}}");
3424 }
3425
3426 // $ref is remote and unknown pointer
3427 TEST(SchemaValidator, Schema_RefUnknownPointerRemote) {
3428 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3429 RemoteSchemaDocumentProvider<SchemaDocumentType> provider;
3430 Document sd;
3431 sd.Parse("{\"type\": \"object\", \"properties\": {\"myInt\": {\"$ref\": \"/subSchemas.json#/a/b\"}}}");
3432 SchemaDocumentType s(sd, "http://localhost:1234/xxxx", 26, &provider);
3433 SCHEMAERROR(s, "{\"RefUnknown\":{\"errorCode\":5,\"instanceRef\":\"#/properties/myInt\",\"value\":\"http://localhost:1234/subSchemas.json#/a/b\"}}");
3434 }
3435
3436 // $ref is cyclical
3437 TEST(SchemaValidator, Schema_RefCyclical) {
3438 typedef GenericSchemaDocument<Value, MemoryPoolAllocator<> > SchemaDocumentType;
3439 Document sd;
3440 sd.Parse("{\"type\": \"object\", \"properties\": {"
3441 " \"cyclic_source\": {"
3442 " \"$ref\": \"#/properties/cyclic_target\""
3443 " },"
3444 " \"cyclic_target\": {"
3445 " \"$ref\": \"#/properties/cyclic_source\""
3446 " }"
3447 "}}");
3448 SchemaDocumentType s(sd);
3449 SCHEMAERROR(s, "{\"RefCyclical\":{\"errorCode\":6,\"instanceRef\":\"#/properties/cyclic_target\",\"value\":\"#/properties/cyclic_source\"}}");
3450 }
3451
3452 TEST(SchemaValidator, Schema_ReadOnlyAndWriteOnly) {
3453 Document sd;
3454 sd.Parse("{\"type\": \"integer\", \"readOnly\": true, \"writeOnly\": true}");
3455 ASSERT_FALSE(sd.HasParseError());
3456 SchemaDocument s1(sd, 0, 0, 0, 0, 0, Specification(kDraft04));
3457 EXPECT_TRUE(s1.GetError().ObjectEmpty());
3458 SchemaDocument s2(sd, 0, 0, 0, 0, 0, Specification(kVersion30));
3459 SCHEMAERROR(s2, "{\"ReadOnlyAndWriteOnly\":{\"errorCode\":13,\"instanceRef\":\"#\"}}");
3460 }
3461
3462 TEST(SchemaValidator, ReadOnlyWhenWriting) {
3463 Document sd;
3464 sd.Parse(
3465 "{"
3466 " \"type\":\"object\","
3467 " \"properties\": {"
3468 " \"rprop\" : {"
3469 " \"type\": \"string\","
3470 " \"readOnly\": true"
3471 " }"
3472 " }"
3473 "}");
3474 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kVersion20));
3475 VALIDATE(s, "{ \"rprop\": \"hello\" }", true);
3476 INVALIDATE_(s, "{ \"rprop\": \"hello\" }", "/properties/rprop", "readOnly", "/rprop",
3477 "{ \"readOnly\": {"
3478 " \"errorCode\": 26, \"instanceRef\": \"#/rprop\", \"schemaRef\": \"#/properties/rprop\""
3479 " }"
3480 "}",
3481 kValidateDefaultFlags | kValidateWriteFlag, SchemaValidator, Pointer);
3482 }
3483
3484 TEST(SchemaValidator, WriteOnlyWhenReading) {
3485 Document sd;
3486 sd.Parse(
3487 "{"
3488 " \"type\":\"object\","
3489 " \"properties\": {"
3490 " \"wprop\" : {"
3491 " \"type\": \"boolean\","
3492 " \"writeOnly\": true"
3493 " }"
3494 " }"
3495 "}");
3496 SchemaDocument s(sd, 0, 0, 0, 0, 0, Specification(kVersion30));
3497 VALIDATE(s, "{ \"wprop\": true }", true);
3498 INVALIDATE_(s, "{ \"wprop\": true }", "/properties/wprop", "writeOnly", "/wprop",
3499 "{ \"writeOnly\": {"
3500 " \"errorCode\": 27, \"instanceRef\": \"#/wprop\", \"schemaRef\": \"#/properties/wprop\""
3501 " }"
3502 "}",
3503 kValidateDefaultFlags | kValidateReadFlag, SchemaValidator, Pointer);
3504 }
3505
3506 TEST(SchemaValidator, NullableTrue) {
3507 Document sd;
3508 sd.Parse("{\"type\": \"string\", \"nullable\": true}");
3509 SchemaDocument s(sd, 0, 0, 0, 0, 0, kVersion20);
3510
3511 VALIDATE(s, "\"hello\"", true);
3512 INVALIDATE(s, "null", "", "type", "",
3513 "{ \"type\": {"
3514 " \"errorCode\": 20,"
3515 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
3516 " \"expected\": [\"string\"], \"actual\": \"null\""
3517 "}}");
3518 INVALIDATE(s, "false", "", "type", "",
3519 "{ \"type\": {"
3520 " \"errorCode\": 20,"
3521 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
3522 " \"expected\": [\"string\"], \"actual\": \"boolean\""
3523 "}}");
3524
3525 SchemaDocument s30(sd, 0, 0, 0, 0, 0, kVersion30);
3526
3527 VALIDATE(s30, "\"hello\"", true);
3528 VALIDATE(s30, "null", true);
3529 INVALIDATE(s30, "false", "", "type", "",
3530 "{ \"type\": {"
3531 " \"errorCode\": 20,"
3532 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
3533 " \"expected\": [\"null\", \"string\"], \"actual\": \"boolean\""
3534 "}}");
3535 }
3536
3537 TEST(SchemaValidator, NullableFalse) {
3538 Document sd;
3539 sd.Parse("{\"type\": \"string\", \"nullable\": false}");
3540 SchemaDocument s(sd, 0, 0, 0, 0, 0, kVersion20);
3541
3542 VALIDATE(s, "\"hello\"", true);
3543 INVALIDATE(s, "null", "", "type", "",
3544 "{ \"type\": {"
3545 " \"errorCode\": 20,"
3546 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
3547 " \"expected\": [\"string\"], \"actual\": \"null\""
3548 "}}");
3549 INVALIDATE(s, "false", "", "type", "",
3550 "{ \"type\": {"
3551 " \"errorCode\": 20,"
3552 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
3553 " \"expected\": [\"string\"], \"actual\": \"boolean\""
3554 "}}");
3555
3556 SchemaDocument s30(sd, 0, 0, 0, 0, 0, kVersion30);
3557
3558 VALIDATE(s30, "\"hello\"", true);
3559 INVALIDATE(s, "null", "", "type", "",
3560 "{ \"type\": {"
3561 " \"errorCode\": 20,"
3562 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
3563 " \"expected\": [\"string\"], \"actual\": \"null\""
3564 "}}");
3565 INVALIDATE(s30, "false", "", "type", "",
3566 "{ \"type\": {"
3567 " \"errorCode\": 20,"
3568 " \"instanceRef\": \"#\", \"schemaRef\": \"#\","
3569 " \"expected\": [\"string\"], \"actual\": \"boolean\""
3570 "}}");
3571 }
3572
3573 #if defined(_MSC_VER) || defined(__clang__)
3574 RAPIDJSON_DIAG_POP
3575 #endif