-
-
Notifications
You must be signed in to change notification settings - Fork 7
/
is_jsonb_valid.c
1203 lines (1072 loc) · 46.4 KB
/
is_jsonb_valid.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include "postgres.h"
#include "catalog/pg_type.h"
#include "fmgr.h"
#include <limits.h>
#include "utils/builtins.h"
#include "catalog/pg_collation.h"
#include "utils/jsonb.h"
#define DEBUG_IS_JSONB_VALID false
// Not very nice, but it is only defined internally
#define JsonContainerIsArray(jc) (((jc)->header & JB_FARRAY) != 0)
#define JsonContainerSize(jc) ((jc)->header & JB_CMASK)
#define _unused(x) ((void)(x))
PG_MODULE_MAGIC;
static bool _is_jsonb_valid (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_required (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_type (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_properties (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_items (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_min (Jsonb * schemaJb, Jsonb * dataJb);
static bool validate_max (Jsonb * schemaJb, Jsonb * dataJb);
static bool validate_any_of (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_all_of (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_one_of (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_unique_items (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_ref (JsonbValue * refJbv, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_enum (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_length (Jsonb * schemaJb, Jsonb * dataJb);
static bool validate_not (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_num_properties (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_num_items (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_dependencies (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_pattern (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema);
static bool validate_multiple_of (Jsonb * schemaJb, Jsonb * dataJb);
static JsonbValue * get_jbv_from_key (Jsonb * in, const char * key);
PG_FUNCTION_INFO_V1(is_jsonb_valid);
Datum
is_jsonb_valid(PG_FUNCTION_ARGS)
{
#ifdef PG_GETARG_JSONB_P
Jsonb *my_schema = PG_GETARG_JSONB_P(0);
Jsonb *my_jsonb = PG_GETARG_JSONB_P(1);
#else
Jsonb *my_schema = PG_GETARG_JSONB(0);
Jsonb *my_jsonb = PG_GETARG_JSONB(1);
#endif
bool is_valid = _is_jsonb_valid(my_schema, my_jsonb, my_schema);
PG_RETURN_BOOL(is_valid);
}
static bool _is_jsonb_valid (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * requiredValue, * refJbv;
bool isValid = true;
if (schemaJb == NULL)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Schema cannot be undefined")));
if (!JB_ROOT_IS_OBJECT(schemaJb))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Schema must be an object")));
requiredValue = get_jbv_from_key(schemaJb, "required");
refJbv = get_jbv_from_key(schemaJb, "$ref");
// $ref overrides rest of properties
if (refJbv != NULL) {
return validate_ref(refJbv, dataJb, root_schema);
}
// If jb is null then we still have to check for required
if (dataJb == NULL) {
if (requiredValue != NULL && requiredValue->type == jbvBool) {
return requiredValue->val.boolean != true;
}
return true;
}
isValid = isValid && validate_required(schemaJb, dataJb, root_schema);
isValid = isValid && validate_type(schemaJb, dataJb, root_schema);
isValid = isValid && validate_properties(schemaJb, dataJb, root_schema);
isValid = isValid && validate_items(schemaJb, dataJb, root_schema);
isValid = isValid && validate_min(schemaJb, dataJb);
isValid = isValid && validate_max(schemaJb, dataJb);
isValid = isValid && validate_any_of(schemaJb, dataJb, root_schema);
isValid = isValid && validate_all_of(schemaJb, dataJb, root_schema);
isValid = isValid && validate_one_of(schemaJb, dataJb, root_schema);
isValid = isValid && validate_unique_items(schemaJb, dataJb, root_schema);
isValid = isValid && validate_enum(schemaJb, dataJb, root_schema);
isValid = isValid && validate_length(schemaJb, dataJb);
isValid = isValid && validate_not(schemaJb, dataJb, root_schema);
isValid = isValid && validate_num_properties(schemaJb, dataJb, root_schema);
isValid = isValid && validate_num_items(schemaJb, dataJb, root_schema);
isValid = isValid && validate_dependencies(schemaJb, dataJb, root_schema);
isValid = isValid && validate_pattern(schemaJb, dataJb, root_schema);
isValid = isValid && validate_multiple_of(schemaJb, dataJb);
return isValid;
}
/* Taken from src/backend/adt/jsonb_utils.c
* Compare two jbvString JsonbValue values, a and b.
*
* This is a special qsort() comparator used to sort strings in certain
* internal contexts where it is sufficient to have a well-defined sort order.
* In particular, object pair keys are sorted according to this criteria to
* facilitate cheap binary searches where we don't care about lexical sort
* order.
*
* a and b are first sorted based on their length. If a tie-breaker is
* required, only then do we consider string binary equality.
*/
static int
lengthCompareJsonbStringValue(const void *a, const void *b)
{
const JsonbValue *va = (const JsonbValue *) a;
const JsonbValue *vb = (const JsonbValue *) b;
int res;
Assert(va->type == jbvString);
Assert(vb->type == jbvString);
if (va->val.string.len == vb->val.string.len)
{
res = memcmp(va->val.string.val, vb->val.string.val, va->val.string.len);
}
else
{
res = (va->val.string.len > vb->val.string.len) ? 1 : -1;
}
return res;
}
/**
* Scalar jsonb are stored in an array
*/
static bool
root_is_really_an_array (Jsonb * jb) {
return JB_ROOT_IS_ARRAY(jb) && !JB_ROOT_IS_SCALAR(jb);
}
// Taken from jsonb_typeof
static bool is_type_correct(Jsonb * in, char * type, int typeLen)
{
JsonbIterator *it;
JsonbValue v;
if (JB_ROOT_IS_OBJECT(in))
return strncmp(type, "object", typeLen) == 0;
else if (JB_ROOT_IS_ARRAY(in) && !JB_ROOT_IS_SCALAR(in))
return strncmp(type, "array", typeLen) == 0;
else
{
Assert(JB_ROOT_IS_SCALAR(in));
it = JsonbIteratorInit(&in->root);
/*
* A root scalar is stored as an array of one element, so we get the
* array and then its first (and only) member.
*/
(void) JsonbIteratorNext(&it, &v, true);
Assert(v.type == jbvArray);
(void) JsonbIteratorNext(&it, &v, true);
switch (v.type)
{
case jbvNull:
return strncmp(type, "null", typeLen) == 0;
case jbvString:
return strncmp(type, "string", typeLen) == 0;
case jbvNumeric:
if (strncmp(type, "number", typeLen) == 0) {
return true;
} else if (strncmp(type, "integer", typeLen) == 0) {
return DatumGetBool(DirectFunctionCall2(
numeric_eq,
PointerGetDatum(v.val.numeric),
DirectFunctionCall1(numeric_floor, PointerGetDatum(v.val.numeric))));
} else {
return false;
}
case jbvBool:
return strncmp(type, "boolean", typeLen) == 0;
default:
elog(ERROR, "unknown jsonb scalar type");
}
}
}
static bool validate_required (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * requiredJbv;
Jsonb * requiredJb;
JsonbIterator *it;
JsonbIteratorToken r;
JsonbValue v;
bool isValid = true;
requiredJbv = get_jbv_from_key(schemaJb, "required");
if (!JB_ROOT_IS_OBJECT(dataJb) || requiredJbv == NULL || requiredJbv->type != jbvBinary)
return true;
requiredJb = JsonbValueToJsonb(requiredJbv);
if (!root_is_really_an_array(requiredJb))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Required must be boolean or array")));
it = JsonbIteratorInit(&requiredJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_ARRAY);
while (isValid) {
JsonbValue *propertyJbv;
r = JsonbIteratorNext(&it, &v, true);
if (r == WJB_END_ARRAY)
break;
if (v.type != jbvString)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Required items must be strings")));
propertyJbv = findJsonbValueFromContainer(&dataJb->root, JB_FOBJECT, &v);
isValid = isValid && (propertyJbv != NULL);
}
return isValid;
}
static bool validate_type (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue *typeJbv;
Jsonb * typeJb;
typeJbv = get_jbv_from_key(schemaJb, "type");
if (typeJbv == NULL)
return true;
if (typeJbv->type == jbvString) {
bool isValid = is_type_correct(dataJb, typeJbv->val.string.val, typeJbv->val.string.len);
return isValid;
} else if (typeJbv->type == jbvBinary) {
bool isValid = false;
JsonbIteratorToken r;
JsonbIterator * it;
JsonbValue v;
typeJb = JsonbValueToJsonb(typeJbv);
if (!root_is_really_an_array(typeJb))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("type must be string or array")));
it = JsonbIteratorInit(&typeJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_ARRAY);
while (!isValid) {
Jsonb * subSchemaJb;
r = JsonbIteratorNext(&it, &v, true);
if (r == WJB_END_ARRAY)
break;
if (v.type == jbvString) {
isValid = isValid || is_type_correct(dataJb, v.val.string.val, v.val.string.len);
} else if (v.type == jbvBinary) {
subSchemaJb = JsonbValueToJsonb(&v);
if (!JB_ROOT_IS_OBJECT(subSchemaJb))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("type elements must be strings or objects")));
isValid = isValid || _is_jsonb_valid(subSchemaJb, dataJb, root_schema);
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("type elements must be strings or objects")));
}
}
return isValid;
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("type elements must be strings or objects")));
}
}
static bool validate_properties (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema) {
JsonbValue * propertiesJbv, * additionalPropertiesJbv, *patternPropertiesJbv;
bool isValid = true;
Jsonb * propertiesJb = NULL;
Jsonb * patternPropertiesJb = NULL;
Jsonb * additionalPropertiesJb = NULL;
JsonbIterator * it, *pIt, *ppIt;
JsonbIteratorToken r, pR, ppR;
if (!JB_ROOT_IS_OBJECT(dataJb))
return true;
propertiesJbv = get_jbv_from_key(schemaJb, "properties");
additionalPropertiesJbv = get_jbv_from_key(schemaJb, "additionalProperties");
patternPropertiesJbv = get_jbv_from_key(schemaJb, "patternProperties");
if (propertiesJbv == NULL && additionalPropertiesJbv == NULL && patternPropertiesJbv == NULL)
return true;
// Lots of cases here because if patternProperties is not present we can optimize a lot
if (patternPropertiesJbv == NULL && propertiesJbv == NULL) {
JsonbValue v;
// If additionalProperties is false check there are no properties
if (additionalPropertiesJbv->type == jbvBool) {
if (additionalPropertiesJbv->val.boolean == true) {
return true;
}
Assert(additionalPropertiesJbv->val.boolean == false);
it = JsonbIteratorInit(&dataJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_OBJECT);
Assert(v.type == jbvObject);
return v.val.object.nPairs == 0;
// We check all properties against additional properties
} else if (additionalPropertiesJbv->type == jbvBinary) {
JsonbValue k;
additionalPropertiesJb = JsonbValueToJsonb(additionalPropertiesJbv);
if (!JB_ROOT_IS_OBJECT(additionalPropertiesJb)) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("additionalProperties must be object or boolean")));
}
it = JsonbIteratorInit(&dataJb->root);
r = JsonbIteratorNext(&it, &k, true);
Assert(r == WJB_BEGIN_OBJECT);
while (isValid) {
Jsonb * subDataJb;
r = JsonbIteratorNext(&it, &k, true);
if (r == WJB_END_OBJECT)
break;
r = JsonbIteratorNext(&it, &v, true);
subDataJb = JsonbValueToJsonb(&v);
isValid = isValid && _is_jsonb_valid(additionalPropertiesJb, subDataJb, root_schema);
}
return isValid;
}
// Unknown model
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("additionalProperties must be object or boolean")));
/*
* Sorted merged join to validate properties so O(#keys)
* we traverse dataJb and propertiesJb simultaneously.
* If a property is present in the schema but not in the object (difference > 0) we check if the property was required
* If the property is present in the object but not in the schema (difference < 0) we check the value against additionalProperties
*/
} else if (patternPropertiesJbv == NULL) {
JsonbValue v, pV, k, pK;
propertiesJb = JsonbValueToJsonb(propertiesJbv);
if (!JB_ROOT_IS_OBJECT(propertiesJb))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("properties must be an object")));
if (additionalPropertiesJbv != NULL)
additionalPropertiesJb = JsonbValueToJsonb(additionalPropertiesJbv);
it = JsonbIteratorInit(&dataJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_OBJECT);
pIt = JsonbIteratorInit(&propertiesJb->root);
pR = JsonbIteratorNext(&pIt, &pV, true);
Assert(pR == WJB_BEGIN_OBJECT);
r = JsonbIteratorNext(&it, &k, true);
pR = JsonbIteratorNext(&pIt, &pK, true);
while (isValid && !(r == WJB_END_OBJECT && pR == WJB_END_OBJECT)) {
Jsonb * subDataJb, * subSchemaJb;
// keys are sorted, difference tells us which one we should iterate (0 means they are even)
int difference;
if (pR == WJB_END_OBJECT) {
difference = -1;
} else if (r == WJB_END_OBJECT) {
difference = 1;
} else {
difference = lengthCompareJsonbStringValue(&k, &pK);
}
// Additional property
if (difference < 0) {
// Iterate once more to get the value (we had the key)
r = JsonbIteratorNext(&it, &v, true);
if (additionalPropertiesJbv != NULL) {
if (additionalPropertiesJbv->type == jbvBool && additionalPropertiesJbv->val.boolean == false) {
if (DEBUG_IS_JSONB_VALID)
elog(INFO, "additional property %*.*s", k.val.string.len, k.val.string.len, k.val.string.val);
isValid = false;
} else if (JB_ROOT_IS_OBJECT(additionalPropertiesJb)) {
subDataJb = JsonbValueToJsonb(&v);
isValid = isValid && _is_jsonb_valid(additionalPropertiesJb, subDataJb, root_schema);
}
}
r = JsonbIteratorNext(&it, &k, true);
} else if (difference > 0) {
pR = JsonbIteratorNext(&pIt, &pV, true);
// Mainly checking that property is not required
subSchemaJb = JsonbValueToJsonb(&pV);
isValid = isValid && _is_jsonb_valid(subSchemaJb, NULL, root_schema);
pR = JsonbIteratorNext(&pIt, &pK, true);
} else {
bool isPropertyValid;
r = JsonbIteratorNext(&it, &v, true);
pR = JsonbIteratorNext(&pIt, &pV, true);
subDataJb = JsonbValueToJsonb(&v);
subSchemaJb = JsonbValueToJsonb(&pV);
isPropertyValid = _is_jsonb_valid(subSchemaJb, subDataJb, root_schema);
if (DEBUG_IS_JSONB_VALID && !isPropertyValid) elog(INFO, "property is not valid %*.*s", k.val.string.len, k.val.string.len, k.val.string.val);
isValid = isValid && isPropertyValid;
r = JsonbIteratorNext(&it, &k, true);
pR = JsonbIteratorNext(&pIt, &pK, true);
}
}
return isValid;
/**
* The last case is when patternProperties is defined. This case is highly unoptimal.
* We iterate over all properties of object. We check against all keys of patterProperties for the regex.
* Then we check if the property is present in propertiesJb.
* If neither of the previous happened, then we check against additionalProperties
*/
} else {
JsonbValue k, ppK, v, ppV;
if (additionalPropertiesJbv != NULL)
additionalPropertiesJb = JsonbValueToJsonb(additionalPropertiesJbv);
if (propertiesJbv != NULL) {
propertiesJb = JsonbValueToJsonb(propertiesJbv);
if (!JB_ROOT_IS_OBJECT(propertiesJb))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("properties must be object or boolean")));
}
patternPropertiesJb = JsonbValueToJsonb(patternPropertiesJbv);
if (!JB_ROOT_IS_OBJECT(patternPropertiesJb)) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("patternProperties must be object or boolean")));
}
it = JsonbIteratorInit(&dataJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_OBJECT);
// Iterate over all keys of object, then all keys of patternProperties, then check property in properties. If none, then additionalProperties
while (isValid) {
bool keyMatched = false;
Jsonb * subDataJbv;
r = JsonbIteratorNext(&it, &k, true);
if (r == WJB_END_OBJECT)
break;
r = JsonbIteratorNext(&it, &v, true);
subDataJbv = JsonbValueToJsonb(&v);
ppIt = JsonbIteratorInit(&patternPropertiesJb->root);
ppR = JsonbIteratorNext(&ppIt, &ppK, true);
Assert(ppR = WJB_BEGIN_OBJECT);
while (isValid) {
bool keyMatches;
ppR = JsonbIteratorNext(&ppIt, &ppK, true);
if (ppR == WJB_END_OBJECT)
break;
ppR = JsonbIteratorNext(&ppIt, &ppV, true);
keyMatches = DatumGetBool(DirectFunctionCall2Coll(textregexeq, DEFAULT_COLLATION_OID,
PointerGetDatum(cstring_to_text_with_len(k.val.string.val, k.val.string.len)),
PointerGetDatum(cstring_to_text_with_len(ppK.val.string.val, ppK.val.string.len))));
if (DEBUG_IS_JSONB_VALID) elog(INFO, keyMatches ? "regex matched" : "regex did not matched");
if (keyMatches) {
Jsonb * subSchemaJb;
subSchemaJb = JsonbValueToJsonb(&ppV);
isValid = isValid && _is_jsonb_valid(subSchemaJb, subDataJbv, root_schema);
keyMatched = true;
}
}
if (propertiesJbv != NULL && propertiesJb != NULL) {
JsonbValue * subSchemaJbv;
Jsonb * subSchemaJb;
subSchemaJbv = findJsonbValueFromContainer(&propertiesJb->root, JB_FOBJECT, &k);
if (subSchemaJbv != NULL) {
subSchemaJb = JsonbValueToJsonb(subSchemaJbv);
isValid = isValid && _is_jsonb_valid(subSchemaJb, subDataJbv, root_schema);
keyMatched = true;
}
}
if (keyMatched != true && additionalPropertiesJbv != NULL) {
if (additionalPropertiesJbv->type == jbvBool && additionalPropertiesJbv->val.boolean == false) {
isValid = false;
} else if (JB_ROOT_IS_OBJECT(additionalPropertiesJb)) {
isValid = isValid && _is_jsonb_valid(additionalPropertiesJb, subDataJbv, root_schema);
}
}
}
return isValid;
}
}
static bool validate_items (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema) {
JsonbValue * itemsJbv, * additionalItemsJbv;
bool isValid = true;
Jsonb * itemsJb;
JsonbIterator * it;
JsonbIteratorToken r;
JsonbValue itemJbv;
if (!root_is_really_an_array(dataJb)) return isValid;
itemsJbv = get_jbv_from_key(schemaJb, "items");
if (itemsJbv == NULL)
return true;
additionalItemsJbv = get_jbv_from_key(schemaJb, "additionalItems");
itemsJb = JsonbValueToJsonb(itemsJbv);
if (JB_ROOT_IS_OBJECT(itemsJb)) {
it = JsonbIteratorInit(&dataJb->root);
r = JsonbIteratorNext(&it, &itemJbv, true);
Assert(r == WJB_BEGIN_ARRAY);
while (isValid) {
Jsonb * subDataJb;
bool isItemValid;
r = JsonbIteratorNext(&it, &itemJbv, true);
if (r == WJB_END_ARRAY) {
break;
}
subDataJb = JsonbValueToJsonb(&itemJbv);
isItemValid = _is_jsonb_valid(itemsJb, subDataJb, root_schema);
if (DEBUG_IS_JSONB_VALID && !isItemValid) elog(INFO, "Item is not valid");
isValid = isValid && isItemValid;
}
} else if (root_is_really_an_array(itemsJb)) {
JsonbIterator *schemaIt;
JsonbIteratorToken schemaR;
JsonbValue schemaJbv;
Jsonb * additionalItemsJb;
bool additionalItemsBuilt = false;
bool isItemsObjectFinished = false;
if (DEBUG_IS_JSONB_VALID) elog(INFO, "Items is array");
it = JsonbIteratorInit(&dataJb->root);
r = JsonbIteratorNext(&it, &itemJbv, true);
schemaIt = JsonbIteratorInit(&itemsJb->root);
schemaR = JsonbIteratorNext(&schemaIt, &schemaJbv, true);
Assert(r == WJB_BEGIN_ARRAY);
Assert(schemaR == WJB_BEGIN_ARRAY);
while (isValid) {
bool isItemValid;
r = JsonbIteratorNext(&it, &itemJbv, true);
if (!isItemsObjectFinished)
schemaR = JsonbIteratorNext(&schemaIt, &schemaJbv, true);
if (schemaR == WJB_END_ARRAY)
isItemsObjectFinished = true;
if (r == WJB_END_ARRAY) {
break;
}
if (!isItemsObjectFinished) {
Jsonb * subDataJb, * subSchemaJb;
subDataJb = JsonbValueToJsonb(&itemJbv);
subSchemaJb = JsonbValueToJsonb(&schemaJbv);
isItemValid = _is_jsonb_valid(subSchemaJb, subDataJb, root_schema);
if (DEBUG_IS_JSONB_VALID && !isItemValid) elog(INFO, "Item is not valid");
} else {
Jsonb * subDataJb;
if (DEBUG_IS_JSONB_VALID) elog(INFO, "Validating against additionalItems");
// No more condition on items
if (additionalItemsJbv == NULL) {
break;
} else if (additionalItemsJbv->type == jbvBool) {
isValid = additionalItemsJbv->val.boolean;
if (DEBUG_IS_JSONB_VALID && !isValid) elog(INFO, "There were additional items");
break;
} else if (additionalItemsBuilt) {
subDataJb = JsonbValueToJsonb(&itemJbv);
isItemValid = _is_jsonb_valid(additionalItemsJb, subDataJb, root_schema);
} else if (additionalItemsJbv->type == jbvBinary) {
additionalItemsBuilt = true;
additionalItemsJb = JsonbValueToJsonb(additionalItemsJbv);
subDataJb = JsonbValueToJsonb(&itemJbv);
isItemValid = _is_jsonb_valid(additionalItemsJb, subDataJb, root_schema);
if (DEBUG_IS_JSONB_VALID && !isItemValid) elog(INFO, "Item does not validate against additionalItems");
} else {
break;
}
}
isValid = isValid && isItemValid;
}
} else {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Items must be array or object")));
}
return isValid;
}
static bool validate_min (Jsonb * schemaJb, Jsonb * dataJb)
{
JsonbIterator *it;
JsonbValue v;
JsonbValue *minJbv, *exclusiveMinJbv;
// bool isValid = true;
if (!is_type_correct(dataJb, "number", 6))
return true;
minJbv = get_jbv_from_key(schemaJb, "minimum");
if (minJbv == NULL || minJbv->type != jbvNumeric) {
return true;
}
it = JsonbIteratorInit(&dataJb->root);
// scalar is saved as array of one element
(void) JsonbIteratorNext(&it, &v, true);
Assert(v.type == jbvArray);
(void) JsonbIteratorNext(&it, &v, true);
if (DatumGetBool(DirectFunctionCall2(numeric_lt, PointerGetDatum(v.val.numeric), PointerGetDatum(minJbv->val.numeric)))) {
if (DEBUG_IS_JSONB_VALID) elog(INFO, "Value is not bigger than minimum");
return false;
}
exclusiveMinJbv = get_jbv_from_key(schemaJb, "exclusiveMinimum");
if (exclusiveMinJbv == NULL || exclusiveMinJbv->type != jbvBool || exclusiveMinJbv->val.boolean != true)
return true;
if (DatumGetBool(DirectFunctionCall2(numeric_eq, PointerGetDatum(v.val.numeric), PointerGetDatum(minJbv->val.numeric)))) {
if (DEBUG_IS_JSONB_VALID) elog(INFO, "Value is not strictly bigger than minimum");
return false;
}
return true;
}
static bool validate_max (Jsonb * schemaJb, Jsonb * dataJb)
{
JsonbIterator *it;
JsonbValue v;
JsonbValue *maxJbv, *exclusiveMaxJbv;
// bool isValid = true;
if (!is_type_correct(dataJb, "number", 6))
return true;
maxJbv = get_jbv_from_key(schemaJb, "maximum");
if (maxJbv == NULL || maxJbv->type != jbvNumeric)
return true;
it = JsonbIteratorInit(&dataJb->root);
// scalar is saved as array of one element
(void) JsonbIteratorNext(&it, &v, true);
Assert(v.type == jbvArray);
(void) JsonbIteratorNext(&it, &v, true);
if (DatumGetBool(DirectFunctionCall2(numeric_gt, PointerGetDatum(v.val.numeric), PointerGetDatum(maxJbv->val.numeric)))) {
if (DEBUG_IS_JSONB_VALID) elog(INFO, "Value is not smaller than maximum");
return false;
}
exclusiveMaxJbv = get_jbv_from_key(schemaJb, "exclusiveMaximum");
if (exclusiveMaxJbv == NULL || exclusiveMaxJbv->type != jbvBool || exclusiveMaxJbv->val.boolean != true)
return true;
if (DatumGetBool(DirectFunctionCall2(numeric_eq, PointerGetDatum(v.val.numeric), PointerGetDatum(maxJbv->val.numeric)))) {
if (DEBUG_IS_JSONB_VALID) elog(INFO, "Value is not strictly smaller than maximum");
return false;
}
return true;
}
static bool validate_any_of (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * propertyJbv;
JsonbIterator *it;
JsonbValue v;
JsonbIteratorToken r;
Jsonb * anyOfJb;
bool isValid = false;
propertyJbv = get_jbv_from_key(schemaJb, "anyOf");
// It cannot be array
if (propertyJbv == NULL || propertyJbv->type != jbvBinary) {
return true;
}
anyOfJb = JsonbValueToJsonb(propertyJbv);
if (!root_is_really_an_array(anyOfJb)) {
return true;
}
it = JsonbIteratorInit(&anyOfJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_ARRAY);
while (!isValid) {
Jsonb * subSchemaJb;
r = JsonbIteratorNext(&it, &v, true);
if (r == WJB_END_ARRAY)
break;
subSchemaJb = JsonbValueToJsonb(&v);
isValid = isValid || _is_jsonb_valid(subSchemaJb, dataJb, root_schema);
}
return isValid;
}
static bool validate_all_of (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * propertyJbv;
JsonbIterator *it;
JsonbValue v;
JsonbIteratorToken r;
Jsonb * allOfJb;
bool isValid = true;
propertyJbv = get_jbv_from_key(schemaJb, "allOf");
// It cannot be array
if (propertyJbv == NULL || propertyJbv->type != jbvBinary) {
return true;
}
allOfJb = JsonbValueToJsonb(propertyJbv);
if (!root_is_really_an_array(allOfJb)) {
return true;
}
it = JsonbIteratorInit(&allOfJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_ARRAY);
while (isValid) {
Jsonb * subSchemaJb;
r = JsonbIteratorNext(&it, &v, true);
if (r == WJB_END_ARRAY)
break;
subSchemaJb = JsonbValueToJsonb(&v);
isValid = isValid && _is_jsonb_valid(subSchemaJb, dataJb, root_schema);
}
return isValid;
}
static bool validate_one_of (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * propertyJbv;
JsonbIterator *it;
JsonbValue v;
JsonbIteratorToken r;
Jsonb * oneOfJb;
int countValid = 0;
propertyJbv = get_jbv_from_key(schemaJb, "oneOf");
// It cannot be array
if (propertyJbv == NULL || propertyJbv->type != jbvBinary) {
return true;
}
oneOfJb = JsonbValueToJsonb(propertyJbv);
if (!root_is_really_an_array(oneOfJb)) {
return true;
}
it = JsonbIteratorInit(&oneOfJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_ARRAY);
while (countValid < 2) {
Jsonb * subSchemaJb;
r = JsonbIteratorNext(&it, &v, true);
if (r == WJB_END_ARRAY)
break;
subSchemaJb = JsonbValueToJsonb(&v);
if (_is_jsonb_valid(subSchemaJb, dataJb, root_schema))
countValid += 1;
}
return countValid == 1;
}
/*
* Unique items is far from optimal O(n2).
*/
static bool validate_unique_items (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * propertyJbv;
JsonbIterator *it;
JsonbValue v;
JsonbIteratorToken r;
bool isValid = true;
int i = 0;
propertyJbv = get_jbv_from_key(schemaJb, "uniqueItems");
// It cannot be array
if (!root_is_really_an_array(dataJb) || propertyJbv == NULL || propertyJbv->type != jbvBool || propertyJbv->val.boolean == false) {
return true;
}
it = JsonbIteratorInit(&dataJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_ARRAY);
while (isValid) {
JsonbIterator *it2;
JsonbValue v2;
JsonbIteratorToken r2;
Jsonb * subDataJb1;
int j = 0;
r = JsonbIteratorNext(&it, &v, true);
i++;
if (r == WJB_END_ARRAY)
break;
subDataJb1 = JsonbValueToJsonb(&v);
it2 = JsonbIteratorInit(&dataJb->root);
r2 = JsonbIteratorNext(&it2, &v2, true);
Assert(r2 == WJB_BEGIN_ARRAY);
_unused(r2);
while (isValid) {
Jsonb * subDataJb2;
r2 = JsonbIteratorNext(&it2, &v2, true);
j++;
if (j >= i)
break;
subDataJb2 = JsonbValueToJsonb(&v2);
isValid = isValid && (compareJsonbContainers(&subDataJb1->root, &subDataJb2->root) != 0);
}
}
return isValid;
}
static bool validate_ref (JsonbValue * refJbv, Jsonb * dataJb, Jsonb * root_schema)
{
ArrayType *path;
Datum *pathtext;
JsonbValue * refSchemaJbv = NULL;
Jsonb * refSchemaJb;
bool *pathnulls;
bool have_object = true, have_array = false;
int npath;
int i;
JsonbContainer * container;
Assert(refJbv != NULL);
if (refJbv->type != jbvString)
{
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("$ref must be a string")));
}
path = DatumGetArrayTypeP(DirectFunctionCall2Coll(
regexp_split_to_array,
DEFAULT_COLLATION_OID,
PointerGetDatum(cstring_to_text_with_len(refJbv->val.string.val, refJbv->val.string.len)),
PointerGetDatum(cstring_to_text("/"))));
/*
* Code from here is very similar to get_jsonb_path_all
*/
deconstruct_array(path, TEXTOID, -1, false, 'i',
&pathtext, &pathnulls, &npath);
if (npath <= 0)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("$ref must not be an empty string"))); // Not sure if npath = 0 can actually happen. Even for empty strings
// We only support refs anchored at root
if (!DatumGetBool(DirectFunctionCall2Coll(texteq,
DEFAULT_COLLATION_OID,
PointerGetDatum((const void *)((Datum *)pathtext)[0]), PointerGetDatum(cstring_to_text("#")))))
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("$ref must be anchored at root")));
Assert(JB_ROOT_IS_OBJECT(root_schema));
container = &root_schema->root;
for (i = 1; i < npath; i++) {
text * route;
// TODO textregexreplace_noopt replaces only first instance. We should replace all instances
route = DatumGetTextP(DirectFunctionCall3Coll(
textregexreplace_noopt,
DEFAULT_COLLATION_OID,
DirectFunctionCall3Coll(
textregexreplace_noopt,
DEFAULT_COLLATION_OID,
PointerGetDatum((const void *)((Datum *)pathtext)[i]),
CStringGetTextDatum("~1"),
CStringGetTextDatum("/")),
CStringGetTextDatum("~0"),
CStringGetTextDatum("~")));
if (have_object) {
JsonbValue k;
k.type = jbvString;
k.val.string.val = VARDATA(route);
k.val.string.len = VARSIZE(route) - VARHDRSZ;
refSchemaJbv = findJsonbValueFromContainer(container, JB_FOBJECT, &k);
} else if (have_array) {
long lindex;
uint32 index;
char *indextext = TextDatumGetCString((Datum)route);
char *endptr;
errno = 0;
lindex = strtol(indextext, &endptr, 10);
if (endptr == indextext || *endptr != '\0' || errno != 0 ||
lindex > INT_MAX || lindex < INT_MIN)
return true;
if (lindex >= 0)
{
index = (uint32) lindex;
}
else
{
/* Handle negative subscript */
uint32 nelements;
/* Container must be array, but make sure */
if (!JsonContainerIsArray(container))
elog(ERROR, "not a jsonb array");
nelements = JsonContainerSize(container);
if (-lindex > nelements)
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("$ref wrong path, index out of bounds")));
else
index = nelements + lindex;
}
refSchemaJbv = getIthJsonbValueFromContainer(container, index);
} else {
// scalar access
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("$ref must point to a schema, not to a scalar")));
}
if (refSchemaJbv == NULL) {
ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE), errmsg("Missing references $ref")));
}
if (i == npath -1 ) {
// No need to compute have_array or have_object now
break;
}
if (refSchemaJbv->type == jbvBinary)
{
JsonbIterator *it = JsonbIteratorInit((JsonbContainer *) refSchemaJbv->val.binary.data);
JsonbIteratorToken r;
JsonbValue tv;
r = JsonbIteratorNext(&it, &tv, true);
container = (JsonbContainer *) refSchemaJbv->val.binary.data;
have_object = r == WJB_BEGIN_OBJECT;
have_array = r == WJB_BEGIN_ARRAY;
}
else
{
// Not sure when this can happen
elog(INFO, "Type in $ref was not jbvBinary");
have_object = refSchemaJbv->type == jbvObject;
have_array = refSchemaJbv->type == jbvArray;
}
}
refSchemaJb = npath == 1 ? root_schema : JsonbValueToJsonb(refSchemaJbv);
return _is_jsonb_valid(refSchemaJb, dataJb, root_schema);
}
static bool validate_enum (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * propertyJbv;
Jsonb * enumJb;
JsonbIterator *it;
JsonbValue v;
JsonbIteratorToken r;
bool isValid = false;
propertyJbv = get_jbv_from_key(schemaJb, "enum");
if (propertyJbv == NULL || propertyJbv->type != jbvBinary)
{
return true;
}
enumJb = JsonbValueToJsonb(propertyJbv);
if (!root_is_really_an_array(enumJb))
return true;
it = JsonbIteratorInit(&enumJb->root);
r = JsonbIteratorNext(&it, &v, true);
Assert(r == WJB_BEGIN_ARRAY);
while (!isValid) {
Jsonb * subSchemaJb;
r = JsonbIteratorNext(&it, &v, true);
if (r == WJB_END_ARRAY)
break;
subSchemaJb = JsonbValueToJsonb(&v);
isValid = isValid || (compareJsonbContainers(&dataJb->root, &subSchemaJb->root) == 0);
}
return isValid;
}
static bool validate_length (Jsonb * schemaJb, Jsonb * dataJb)
{
JsonbIterator *it;
JsonbValue v;
JsonbValue *minLenghtJbv, *maxLengthJbv;
bool isValid = true;
// bool isValid = true;
if (!is_type_correct(dataJb, "string", 6))
return true;
it = JsonbIteratorInit(&dataJb->root);
// scalar is saved as array of one element
(void) JsonbIteratorNext(&it, &v, true);
Assert(v.type == jbvArray);
(void) JsonbIteratorNext(&it, &v, true);
minLenghtJbv = get_jbv_from_key(schemaJb, "minLength");
if (minLenghtJbv != NULL && minLenghtJbv->type == jbvNumeric) {
int length = DatumGetInt32(DirectFunctionCall1(textlen, PointerGetDatum(cstring_to_text_with_len(v.val.string.val, v.val.string.len))));
if (DEBUG_IS_JSONB_VALID) elog(INFO, "Length is %d", length);
isValid = isValid && DatumGetBool(DirectFunctionCall2(numeric_ge, DirectFunctionCall1(int4_numeric, length), PointerGetDatum(minLenghtJbv->val.numeric)));
}
maxLengthJbv = get_jbv_from_key(schemaJb, "maxLength");
if (maxLengthJbv != NULL && maxLengthJbv->type == jbvNumeric) {
int length = DatumGetInt32(DirectFunctionCall1(textlen, PointerGetDatum(cstring_to_text_with_len(v.val.string.val, v.val.string.len))));
isValid = isValid && DatumGetBool(DirectFunctionCall2(numeric_le, DirectFunctionCall1(int4_numeric, length), PointerGetDatum(maxLengthJbv->val.numeric)));
}
return isValid;
}
static bool validate_not (Jsonb * schemaJb, Jsonb * dataJb, Jsonb * root_schema)
{
JsonbValue * propertyJbv;
Jsonb * notJb;
propertyJbv = get_jbv_from_key(schemaJb, "not");
// It cannot be array
if (propertyJbv == NULL || propertyJbv->type != jbvBinary) {
return true;
}
notJb = JsonbValueToJsonb(propertyJbv);
if (!JB_ROOT_IS_OBJECT(notJb)) {
return true;
}
return !_is_jsonb_valid(notJb, dataJb, root_schema);