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
  
 
#pike __REAL_VERSION__ 
#require constant(@module@) 
 
//! @ignore 
inherit @module@; 
//! @endignore 
 
//! @appears Standards.JSON module 
//! 
//! Tools for handling the JSON structured data format. See 
//! @url{http://www.json.org/@} and @rfc{4627@}. 
 
//! @ignore 
// Use getters and Val-> to ensure dynamic resolving in case the 
// values in Val.pmod are replaced. 
Val.True `->true() {return Val->true;} 
Val.False `->false() {return Val->false;} 
Val.Null `->null() {return Val->null;} 
//! @endignore 
 
//! @decl Val.True true 
//! @decl Val.False false 
//! @decl Val.Null null 
//! 
//! Compat aliases for the corresponding @[Val] objects. These are 
//! used to represent the three JSON literals @expr{true@}, 
//! @expr{false@} and @expr{null@}. 
//! 
//! @deprecated Val.true, Val.false, Val.null 
 
//! Error thrown when JSON decode fails. 
class DecodeError { 
    inherit Error.Generic; 
 
    constant error_type = "json_decode"; 
    constant is_json_decode_error = 1; 
 
    //! The string that failed to be decoded. 
    string err_str; 
 
    //! The failing position in @[err_str]. 
    int err_pos; 
 
    protected void create(string err_str, int err_pos, void|string reason, void|array bt) { 
        this::err_str = err_str; 
        this::err_pos = err_pos; 
 
        string pre_context = err_pos > 13 ? 
            sprintf ("...%O", err_str[err_pos - 10..err_pos - 1]) : 
            err_pos > 0 ? 
            sprintf ("%O", err_str[..err_pos - 1]) : 
            ""; 
        if (err_pos >= sizeof (err_str)) 
          err_str = sprintf ("%s-><-", pre_context); 
        else { 
          string post_context = err_pos < sizeof (err_str) - 14 ? 
            sprintf ("%O...", err_str[err_pos + 1..err_pos + 10]) : 
            err_pos + 1 < sizeof (err_str) ? 
            sprintf ("%O", err_str[err_pos + 1..]) : 
            ""; 
          err_str = sprintf ("%s->[%c]<-%s", 
                             pre_context, err_str[err_pos], post_context); 
        } 
 
        if (reason) 
            ::create(sprintf("Error decoding JSON at position %d %s: %s.\n", 
                             err_pos, err_str, reason), bt); 
        else 
            ::create(sprintf("Error decoding JSON at position %d %s\n", 
                             err_pos, err_str), bt); 
    } 
} 
 
void decode_error(string err_str, int err_pos, void|string reason, void|mixed ... args) { 
    if (sizeof(args)) reason = sprintf(reason, @args); 
    throw(DecodeError(err_str, err_pos, reason, backtrace()[..<1])); 
} 
 
//! An instance of this class can be used to validate a JSON object against a 
//! JSON schema. 
//! 
//! @example 
//!   string schema_s = "{\n" + 
//!       "  \"name\": \"SomeExample\",\n" + 
//!       "  \"type\": \"object\",\n" + 
//!       "  \"properties\": {\n" + 
//!       "      \"name\": { \"type\": \"string\" },\n" + 
//!       "      \"id\": {\n" + 
//!       "          \"type\": \"integer\",\n" + 
//!       "          \"minimum\": 0\n" + 
//!       "      }\n" + 
//!       "  }\n" + 
//!       "}"; 
//!   string example_s = "{\n" + 
//!       "  \"name\": \"An Example\",\n" + 
//!       "  \"id\": 17\n" + 
//!       "}"; 
//!   mixed schema = @[Standards.JSON.decode](schema_s); 
//!   mixed example = @[Standards.JSON.decode](example_s); 
//!   if (string error = @[Standards.JSON.Validator(schema).validate](example)) 
//!      werror("Error: JSON string %O did not validate: %s\n", example_s, error); 
//!   else 
//!      write("JSON ok\n"); 
//! 
//! @note 
//!   This class validates only a subset of the JSON schema specification. 
//!   Currently dependencies and references are not handled and regular 
//!   expressions (for pattern properties) are limited to those that can be 
//!   handled by @[Regexp.SimpleRegexp]. 
//! 
//! For more information of JSON schema look at 
//! @url{http://json-schema.org/documentation.html@} "The home of JSON Schema". 
class Validator 
{ 
    private mixed schema; 
 
    //! Create a JSON validator for some JSON schema. 
    //! @param _schema 
    //!   The JSON schema to use in @[validate()]. This must be a valid JSON 
    //!   object. 
    //! @throws 
    //!   Throws an error if the schema is invalid. 
    void create(mixed _schema) 
    { 
        schema = _schema; 
        if (!schema || !mappingp(schema)) 
            error("Invalid schema: Expected a schema got %O\n", _schema); 
    } 
 
    //! This function validates a JSON object against the JSON schema that was 
    //! specified in the Validator's constructor. 
    //! If the JSON object is not valid, a string with an error-message is 
    //! returned. 
    //! If the JSON object is valid, 0 is returned. 
    //! 
    //! @param json 
    //!   The JSON object to validate. 
    //! @returns 
    //!   0, if the json object is valid, and an error-message if it is not valid. 
    string validate(mixed json) 
    { 
        if (undefinedp(json) || zero_type(json)) 
            return sprintf("JSON invalid: Expected a json object got %O\n", json); 
 
        // TODO: handle schema->dependencies and references 
 
        return validate_properties(schema->name || "<unknown JSON object>", 
                                   json, schema); 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is a number (integer or float). 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no number. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_number(mapping(string:mixed) schema, string property) 
    { 
        if (has_index(schema, property)) 
        { 
            if (!intp(schema[property]) && !floatp(schema[property])) 
                error("Invalid schema: Expected %s to be a number, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is an integer. 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no integer. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_integer(mapping(string:mixed) schema, string property) 
    { 
        if (has_index(schema, property)) 
        { 
            if (!intp(schema[property])) 
                error("Invalid schema: Expected %s to be an integer, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! @returns 
    //!   1 if the specified @tt{value@} is either @[Standards.JSON.true] or 
    //!   @[Standards.JSON.false]. 
    private int(0..1) is_JSON_boolean(mixed value) 
    { 
        return equal(value, true) || equal(value, false); 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is a boolean (with values @[Standards.JSON.true] 
    //! or @[Standards.JSON.false]). 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no boolean. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_boolean(mapping(string:mixed) schema, string property) 
    { 
        if (has_index(schema, property)) 
        { 
            if (!is_JSON_boolean(schema[property])) 
                error("Invalid schema: Expected %s to be a boolean, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is a string. 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no string. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_string(mapping(string:mixed) schema, string property) 
    { 
        if (has_index(schema, property)) 
        { 
            if (!stringp(schema[property])) 
                error("Invalid schema: Expected %s to be a string, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is an array. 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no array. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_array(mapping(string:mixed) schema, string property) 
    { 
        if (has_index(schema, property)) 
        { 
            if (!arrayp(schema[property])) 
                error("Invalid schema: Expected %s to be an array, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is an @tt{array(string)@}. 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no 
    //! @tt{array(string)@}. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_array_string(mapping(string:mixed) schema, string property) 
    { 
        if (has_schema_array(schema, property)) 
        {   // all array entries should be strings: 
            if (!Array.reduce(`&, map(schema[property], stringp), 1)) 
                error("Invalid schema: Expected %s to be an array of strings, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is an @tt{array(mapping)@}. 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no 
    //! @tt{array(mapping)@}. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_array_mapping(mapping(string:mixed) schema, string property) 
    { 
        if (has_schema_array(schema, property)) 
        {   // all array items must be mappings: 
            if (!Array.reduce(`&, map(schema[property], mappingp), 1)) 
                error("Invalid schema: Expected %s to be an array of JSON schemata, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is a mapping. 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no mapping. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_mapping(mapping(string:mixed) schema, string property) 
    { 
        if (has_index(schema, property)) 
        { 
            if (!mappingp(schema[property])) 
                error("Invalid schema: Expected %s to be an object, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Test if the @tt{schema@} has the specified @tt{property@} and the value 
    //! of the @tt{property@} is a mapping(string:mapping). 
    //! @throws 
    //!   Throws an error if the value of the @tt{property@} is no 
    //! @tt{mapping(string:mapping)@}. 
    //! @returns 
    //!   1 if the @tt{schema@} has the specified @tt{property@}. 
    private int(0..1) has_schema_mapping_mapping(mapping(string:mixed) schema, string property) 
    { 
        if (has_schema_mapping(schema, property)) 
        { 
            if (// all mapping keys must be strings: 
                !Array.reduce(`&, map(indices(schema[property]), stringp), 1) || 
                // all mapping values must be mappings: 
                !Array.reduce(`&, map(values(schema[property]), mappingp), 1)) 
                error("Invalid schema: Expected %s to be an object, got %O\n", 
                      property, schema[property]); 
            return 1; 
        } 
        else 
            return 0; 
    } 
 
    //! Verify that the specified value is a number (integer or float) according 
    //! to the specified @tt{schema@}. 
    //! The following properties of @tt{schema@} are verified: 
    //! @dl 
    //! @item minimum 
    //!   If the @tt{schema@} has the property @tt{"minimum"@}, then the 
    //!   @tt{value@} must be greater than or equal to the specified minimum. 
    //! @item exclusiveMinimum 
    //!   If the @tt{schema@} has the properties @tt{"minimum"@} and 
    //!   @tt{"exclusiveMinimum"@} is @[Standards.JSON.true], then the 
    //!   @tt{value@} must be greater than the specified minimum. 
    //! @item maximum 
    //!   If the @tt{schema@} has the property @tt{"maximum"@}, then the 
    //!   @tt{value@} must be lower than or equal to the specified maximum. 
    //! @item exclusiveMaximum 
    //!   If the @tt{schema@} has the properties @tt{"maximum"@} and 
    //!   @tt{"exclusiveMaximum"@} is @[Standards.JSON.true], then the 
    //!   @tt{value@} must be lower than the specified minimum. 
    //! @item multipleOf 
    //!   If @tt{schema@} has the property @tt{"multipleOf"@}, then the 
    //!   @tt{value@} must be an integer multiple of the specified multpleOf. 
    //! @enddl 
    private string validate_number(string key, mixed value, mapping(string:mixed) schema) 
    { 
        if (!intp(value) && !floatp(value)) 
            return sprintf("JSON invalid: Value for %O is expected to be a number, got %O\n", 
                           key, value); 
 
        array(string) errors = ({}); 
 
        if (has_schema_number(schema, "minimum")) 
        { 
            if (has_schema_boolean(schema, "exclusiveMinimum") && 
                schema->exclusiveMinimum) 
            {   // schema->minimum is not an allowed value 
                if (value <= schema->minimum) 
                    errors += ({ 
                        sprintf("JSON invalid: Number %O=%O is lower than or equal to the specified minimum %O\n", 
                                key, value, schema->minimum) }); 
            } 
            else 
            {   // schema->minimum is an allowed value 
                if (value < schema->minimum) 
                    errors += ({ 
                        sprintf("JSON invalid: Number %O=%O is lower than the specified minimum %O\n", 
                                key, value, schema->minimum) }); 
            } 
        } 
 
        if (has_schema_number(schema, "maximum")) 
        { 
            if (has_schema_boolean(schema, "exclusiveMaximum") && 
                schema->exclusiveMaximum) 
            {   // schema->maximum is not an allowed value 
                if (value >= schema->maximum) 
                    errors += ({ 
                        sprintf("JSON invalid: Number %O=%O is greater than or equal to the specified maximum %O\n", 
                                key, value, schema->maximum) }); 
            } 
            else 
            {   // schema->maximum is an allowed value 
                if (value > schema->maximum) 
                    errors += ({ 
                        sprintf("JSON invalid: Number %O=%O is greater than the specified maximum %O\n", 
                                key, value, schema->maximum) }); 
            } 
        } 
 
        if (has_schema_number(schema, "multipleOf")) 
        { 
            int(0..1) is_multipleOf; 
            if (intp(value) && intp(schema->multipleOf)) 
                is_multipleOf = (value % schema->multipleOf == 0); 
            else 
            { 
                float divisor = value / schema->multipleOf; 
                is_multipleOf = (divisor == round(divisor)); 
            } 
 
            if (!is_multipleOf) 
                errors += ({ 
                    sprintf("JSON invalid: Number %O=%O is not a multiple of %O\n", 
                            key, value, schema->multipleOf) }); 
        } 
 
        errors = filter(errors, stringp); // remove all non-errors 
        if (sizeof(errors)) return errors * ""; 
        else return 0; 
    } 
 
    //! Verify that the specified value is an integer according to the specified 
    //! @tt{schema@}. This is the similar to @[validate_number()], but the 
    //! @tt{value@} must be an int and not a float. 
    //! The following properties of @tt{schema@} are verified: 
    //! @dl 
    //! @item minimum 
    //!   If the @tt{schema@} has the property @tt{"minimum"@}, then the 
    //!   @tt{value@} must be greater than or equal to the specified minimum. 
    //! @item exclusiveMinimum 
    //!   If the @tt{schema@} has the properties @tt{"minimum"@} and 
    //!   @tt{"exclusiveMinimum"@} is @[Standards.JSON.true], then the 
    //!   @tt{value@} must be greater than the specified minimum. 
    //! @item maximum 
    //!   If the @tt{schema@} has the property @tt{"maximum"@}, then the 
    //!   @tt{value@} must be lower than or equal to the specified maximum. 
    //! @item exclusiveMaximum 
    //!   If the @tt{schema@} has the properties @tt{"maximum"@} and 
    //!   @tt{"exclusiveMaximum"@} is @[Standards.JSON.true], then the 
    //!   @tt{value@} must be lower than the specified minimum. 
    //! @item multipleOf 
    //!   If @tt{schema@} has the property @tt{"multipleOf"@}, then the 
    //!   @tt{value@} must be an integer multiple of the specified multpleOf. 
    //! @enddl 
    private string validate_integer(string key, mixed value, mapping(string:mixed) schema) 
    { 
        if (!intp(value)) 
            return sprintf("JSON invalid: Value for %O is expected to be an integer, got %O\n", 
                           key, value); 
        return validate_number(key, value, schema); 
    } 
 
    //! Verify that the specified value is a string according to the specified 
    //! @tt{schema@}. The following properties of @tt{schema@} are verified: 
    //! @dl 
    //! @item minLength 
    //!   If @tt{schema@} has the property @tt{"minLength"@}, then the 
    //!   @tt{value@} must not be shorter than the specified length. 
    //! @item maxLength 
    //!   If @tt{schema@} has the property @tt{"maxLength"@}, then the 
    //!   @tt{value@} must not be longer than the specified length. 
    //! @item pattern 
    //!   If @tt{schema@} has the property @tt{"pattern"@}, then the 
    //!   @tt{value@} must match the specified pattern. 
    //! @enddl 
    //! 
    //! @note 
    //!   TODO: We use @[Regexp.SimpleRegexp] to handle @tt{schema->pattern@}, 
    //!   but that covers only some part of the possible regular expressions. 
    private string validate_string(string key, mixed value, mapping(string:mixed) schema) 
    { 
        if (!stringp(value)) 
            return sprintf("JSON invalid: Value for %O is expected to be a string, got %O\n", 
                           key, value); 
 
        array(string) errors = ({}); 
 
        if (has_schema_integer(schema, "minLength") && 
            strlen(value) < schema->minLength) 
            errors += ({ 
                sprintf("JSON invalid: String %O=%O is shorter than the minimum length %O\n", 
                        key, value, schema->minLength) }); 
 
        if (has_schema_integer(schema, "maxLength") && 
            strlen(value) > schema->maxLength) 
            errors += ({ 
                sprintf("JSON invalid: String %O=%O is longer than the maximum length %O\n", 
                        key, value, schema->maxLength) }); 
 
        if (has_schema_string(schema, "pattern")) 
        { 
            Regexp.SimpleRegexp re = Regexp.SimpleRegexp(schema->pattern); 
            if (!re->match(value)) 
                errors += ({ 
                    sprintf("JSON invalid: String %O=%O does not match the pattern %O\n", 
                            key, value, schema->pattern) }); 
        } 
 
        errors = filter(errors, stringp); // remove all non-errors 
        if (sizeof(errors)) return errors * ""; 
        else return 0; 
    } 
 
    //! Verify that the specified @tt{value@} has the correct type that is 
    //! defined by @tt{schema->type@}. @tt{schema->type@} can be any of 
    //! @ul 
    //! @item 
    //!   "boolean", 
    //! @item 
    //!   "integer", 
    //! @item 
    //!   "number", 
    //! @item 
    //!   "string", 
    //! @item 
    //!   "array", 
    //! @item 
    //!   "object", 
    //! @item 
    //!   "null", 
    //! @endul 
    //! or an array of these. 
    private string validate_item_type(string key, mixed value, mapping schema) 
    { 
        array(string) expected_types = Array.arrayify(schema->type); 
        if (sizeof(expected_types) == 0) 
        {   // guess the type: 
            int(0..1) is_index_of(string i, mixed obj) { return has_index(obj, i); }; 
            if (Array.any(({ "multipleOf", "minimum", "maximum" }), 
                          is_index_of, schema)) 
                expected_types = ({ "number" }); 
 
            else if (Array.any(({ "maxLength", "minLength", "pattern" }), 
                               is_index_of, schema)) 
                expected_types = ({ "string" }); 
 
            else if (Array.any(({ "items", "additionalItems", "maxItems", 
                                  "minItems", "uniqueItems" }), 
                               is_index_of, schema)) 
                expected_types = ({ "array" }); 
 
            else if (Array.any(({ "properties", "maxProperties", 
                                  "minProperties", "required", 
                                  "additionalProperties", 
                                  "patternProperties" }), 
                               is_index_of, schema)) 
                expected_types = ({ "object" }); 
 
            else // we can't guess the type, so we can't validate it: 
                return 0; 
        } 
 
        // all expected_types entries should be strings: 
        if (!Array.reduce(`&, map(expected_types, stringp), 1)) 
            error("Invalid schema: Expected type for %s to be a string or an array of strings, got %O\n", 
                  key, schema->type); 
 
        if (is_JSON_boolean(value) && has_value(expected_types, "boolean")) 
            return 0; 
 
        else if (equal(value, null) && 
                 has_value(expected_types, "null")) 
            return 0; 
 
        else if (intp(value) && has_value(expected_types, "integer")) 
            return validate_integer(key, value, schema); 
 
        else if ((intp(value) || floatp(value)) && 
                 has_value(expected_types, "number")) 
            return validate_number(key, value, schema); 
 
        else if (stringp(value) && has_value(expected_types, "string")) 
            return validate_string(key, value, schema); 
 
        else if (arrayp(value) && has_value(expected_types, "array")) 
            return validate_array(key, value, schema); 
 
        else if (mappingp(value) && has_value(expected_types, "object")) 
            return validate_object(key, value, schema); 
 
        else 
            return sprintf("JSON invalid: Unexpected type for %O=%O, expected type is %O\n", 
                           key, value, expected_types * ","); 
    } 
 
    //! Verify that the specified value matches the specified @tt{schema@}. The 
    //! following properties of @tt{schema@} are verified: 
    //! @dl 
    //! @item type 
    //!   If the @tt{schema@} has a property @tt{"type"@}, then the @tt{value@} 
    //!   must match the specified type (see @[validate_item_type()]). 
    //! @item allOf 
    //!   If the @tt{schema@} has a property @tt{"allOf"@}, which is an array of 
    //!   schema objects, then the @tt{value@} must match each schema specified 
    //!   in that array (via another call to @[validate_properties()]). 
    //! @item anyOf 
    //!   If the @tt{schema@} has a property @tt{"anyOf"@}, which is an array of 
    //!   schema objects, then the @tt{value@} must match at least one schema 
    //!   specified in that array (via another call to 
    //!   @[validate_properties()]). 
    //! @item oneOf 
    //!   If the @tt{schema@} has a property @tt{"oneOf"@}, which is an array of 
    //!   schema objects, then the @tt{value@} must match exactly one schema 
    //!   specified in that array (via another call to 
    //!   @[validate_properties()]). 
    //! @item not 
    //!   If the @tt{schema@} has a property @tt{"not"@}, then the @tt{value@} 
    //!   must not match the schema specified by that property (via another 
    //!   call to @[validate_properties()]). 
    //! @item enum 
    //!   If the @tt{schema@} has a property @tt{"enum"@}, then the @tt{value@} 
    //!   must be equal to any of the values specified in the enum array. 
    //! @enddl 
    //! 
    //! @note 
    //!   If the @tt{schema@} is empty (i.e., it has none of the above specified 
    //!   properties, then the @tt{value@} is valid). 
    private string validate_properties(string key, mixed value, mapping schema) 
    { 
        array(string) errors = ({ 
            validate_item_type(key, value, schema) 
        }); 
 
        if (has_schema_array_mapping(schema, "allOf")) 
            // value must match all of the specified schema, i.e., no error 
            // allowed: 
            errors += 
                map(schema->allOf, 
                    lambda(mapping s) { return validate_properties(key, value, s); }); 
 
        // value must match at least one of the specified schema: 
        if (has_schema_array_mapping(schema, "anyOf") && 
            !Array.any(schema->anyOf, 
                       lambda(mapping s) { return validate_properties(key, value, s) == 0; })) 
            errors += ({ 
                sprintf("JSON invalid: Value for %O is expected to match any of %O, got %O\n", 
                        key, schema->anyOf, value) }); 
 
        // value must match exactly one of the specified schema: 
        if (has_schema_array_mapping(schema, "oneOf") && 
            Array.count(map(schema->oneOf, 
                            lambda(mapping s) { 
                                return validate_properties(key, value, s); 
                            }), 0) != 1) 
            errors += ({ 
                sprintf("JSON invalid: Value for %O is expected to match one of %O, got %O\n", 
                        key, schema->oneOf, value) }); 
 
        if (has_schema_mapping(schema, "not") && 
            validate_properties(key, value, schema->not) == 0) 
            errors += ({ 
                sprintf("JSON invalid: Value for %O is expected not to match %O, got %O\n", 
                        key, schema->not, value) }); 
 
        if (has_schema_array(schema, "enum") && 
            !Array.any(schema->enum, Function.curry(equal)(value))) 
            errors += ({ 
                sprintf("JSON invalid: %O should be one of %O, got %O\n", 
                        key, schema->enum, value) }); 
 
        errors = filter(errors, stringp); // remove all non-errors 
        if (sizeof(errors)) return errors * ""; 
        else return 0; 
    } 
 
    //! Verify that the specified @tt{value@} is an array according to the 
    //! specified @tt{schema@}. 
    //! The following properties of @tt{schema@} are verified: 
    //! @dl 
    //! @item minItems 
    //!   If @tt{schema@} has the property @tt{"minItems"@}, then the array must 
    //!   have at least the specified number of items. 
    //! @item maxItems 
    //!   If @tt{schema@} has the property @tt{"maxItems"@}, then the array must 
    //!   not have more than the specified number of items. 
    //! @item items 
    //!   If @tt{schema@} has the property @tt{"items"@}, which is an array of 
    //!   schema objects, then each element in the value array must be valid 
    //!   according the corresponding schema in the @tt{"items"@} array. 
    //! @enddl 
    private string validate_array(string key, mixed value, mapping(string:mixed) schema) 
    { 
        if (!arrayp(value)) 
            return sprintf("JSON invalid: Value for %O is expected to be an array, got %O\n", 
                           key, value); 
 
        array(string) errors = ({}); 
 
        if (has_schema_integer(schema, "minItems") && 
            sizeof(value) < schema->minItems) 
            errors += ({ 
                sprintf("JSON invalid: Array %O has less than the minimum number of items: %O\n", 
                        key, schema->minItems) }); 
 
        if (has_schema_integer(schema, "maxItems") && 
            sizeof(value) > schema->maxItems) 
            errors += ({ 
                sprintf("JSON invalid: Array %O has more than the maximum number of items: %O\n", 
                        key, schema->maxItems) }); 
 
        if (has_index(schema, "items")) 
        { 
            if (mappingp(schema->items)) 
            {   // schema->items is a JSON schema, so validate all items in value 
                // against it 
                errors += map(value, 
                              lambda(mixed item) { 
                                  return validate_properties( 
                                      key, item, schema->items); 
                              }); 
            } 
            else if (arrayp(schema->items) && 
                     // all entries of items must be objects 
                     Array.reduce(`&, map(schema->items, mappingp), 1)) 
            {   // validate each item in value against the corresponding JSON schema 
                // in schema->items 
                array value_items = value[..(sizeof(schema->items)-1)]; 
                array value_additional = value[sizeof(schema->items)..]; 
                foreach (value_items; int i; mixed item) 
                    errors += ({ validate_properties( 
                                     key, item, schema->items[i]) }); 
 
                // if there are more values than in schema->items, then either 
                // schema->additionalItems must be a boolean with value true, 
                // or it must be a schema and all remaining items must match it. 
                if (sizeof(value_additional)) 
                { 
                    if (!has_index(schema, "additionalItems") || 
                        (is_JSON_boolean(schema->additionalItems) && 
                         !schema->additionalItems)) 
                        errors += ({ 
                            sprintf("JSON invalid: Array %O has more items than schema %O\n", 
                                    key, schema->items) }); 
                    else if (mappingp(schema->additionalItems)) 
                        foreach (value_additional, mixed item) 
                            errors += ({ validate_properties( 
                                             key, item, schema->additionalItems) }); 
                    else if (!is_JSON_boolean(schema->additionalItems)) 
                        error("Invalid schema: Expected %O->additionalItems to be a boolean or a schema, got %O\n", 
                              key, schema->additionalItems); 
                } 
            } 
            else 
                error("Invalid schema: Expected %O->schema to be an object or an array of objects, got %O\n", 
                      key, schema->items); 
        } 
 
        if (has_schema_boolean(schema, "uniqueItems") && schema->uniqueItems) 
        {   // verify that there are no duplicate items 
            foreach (enumerate(sizeof(value)), int i) 
                foreach (enumerate(sizeof(value)-(i+1), 1, i+1), int j) 
                    if (equal(value[i], value[j])) 
                        errors += ({ 
                            sprintf("JSON invalid: Array %O should have unique items, but index %d and %d are equal: %O\n", 
                                    key, i, j, value[i]) }); 
        } 
 
        errors = filter(errors, stringp); // remove all non-errors 
        if (sizeof(errors)) return errors * ""; 
        else return 0; 
    } 
 
    //! Verify that the specified @tt{value@} is an object according to the 
    //! specified @tt{schema@}. 
    //! The following properties of @tt{schema@} are verified: 
    //! @dl 
    //! @item minProperties 
    //!   If @tt{schema@} has the property @tt{"minProperties"@}, then the 
    //!   object must have at least the specified number of properties. 
    //! @item maxProperties 
    //!   If @tt{schema@} has the property @tt{"maxProperties"@}, then the 
    //!   object must not have more than the specified number of items. 
    //! @item required 
    //!   If @tt{schema@} has the property @tt{"required"@}, which is an array 
    //!   of strings, then the object must have all properties that are listed 
    //!   in the specified array. 
    //! @item properties 
    //!   If @tt{schema@} has the property @tt{"properties"@}, which is a 
    //!   mapping of property-name to a schema, then each property of the object 
    //!   that has a corresponding schema in @tt{"properties"@} must be valid 
    //!   according to that schema. 
    //! @item patternProperties 
    //!   If @tt{schema@} has the property @tt{"properties"@}, which is a 
    //!   mapping of property-name-pattern to a schema, then each property of 
    //!   the object must be valid according to all schema objects for which 
    //!   the pattern matches the property-name. 
    //! @item additionalProperties 
    //!   If @tt{schema@} has the property @tt{"additionalProperties"@}, it can 
    //!   be either a boolean value, or a schema. 
    //!   @ul 
    //!   @item 
    //!     If it is a boolean with value @[Standards.JSON.false], then all 
    //!     properties of the object must be validated either by a schema from 
    //!     @tt{"properties"@} or @tt{"patternProperties"@}. 
    //!   @item 
    //!     If it is a boolean with value @[Standards.JSON.true], then the 
    //!     object is allowed to have additional properties without validation. 
    //!   @item 
    //!     If it is a schema, then any propery of the object that is not 
    //!     validated by a schema from @tt{"properties"@} or 
    //!     @tt{"patternProperties"@} must be validated by the specified schema. 
    //!   @endul 
    //! @enddl 
    //! 
    //! @note 
    //!  TODO: We use @[Regexp.SimpleRegexp] to handle 
    //!  @tt{schema->patternProperties@}, but that covers only some part of the 
    //!  possible regular expressions. 
    private string validate_object(string key, mixed value, mapping(string:mixed) schema) 
    { 
        if (!mappingp(value)) 
            return sprintf("JSON invalid: Value for %O is expected to be a mapping, got %O\n", 
                           key, value); 
 
        array(string) errors = ({}); 
        if (has_schema_integer(schema, "minProperties") && 
            sizeof(value) < schema->minProperties) 
            errors += ({ 
                sprintf("JSON invalid: Object %O has less than the minimum number of properties %O\n", 
                        key, schema->minProperties) }); 
 
        if (has_schema_integer(schema, "maxProperties") && 
            sizeof(value) > schema->maxProperties) 
            errors += ({ 
                sprintf("JSON invalid: Object %O has more than the maximum number of properties %O\n", 
                        key, schema->maxProperties) }); 
 
        if (has_schema_array_string(schema, "required")) 
            errors += map(schema->required, 
                          lambda(string item) { 
                              if (has_index(value, item)) 
                                  return 0; 
                              else 
                                  return sprintf("JSON invalid: Object %O has not the required item %O\n", 
                                                 key, item); 
                          }); 
 
        // Verify that each property of the mapping value 
        // - either has a matching JSON schema in schema->properties, 
        // - or matches schema->additionalProperties, 
        // - or matches all schema from schema->patternProperites where the 
        //   pattern matches the property-key 
        // - or schema->additionalProperties is a boolean with value true 
        int(0..1) additionalProperties_allowed = 0; 
        mapping additionalProperties; 
        if (has_index(schema, "additionalProperties")) 
        { 
            if (is_JSON_boolean(schema->additionalProperties)) 
                additionalProperties_allowed = schema->additionalProperties; 
            else if (mappingp(schema->additionalProperties)) 
                additionalProperties = schema->additionalProperties; 
            else// if additionalProperties is available it must be a boolean or 
                // a schema: 
                error("Invalid schema: Expected %O->additionalProperties to be an object or a boolean, got %O\n", 
                      key, schema->additionalProperties); 
        } 
 
        mapping patternProperties = ([]); 
        if (has_schema_mapping_mapping(schema, "patternProperties")) 
            foreach (schema->patternProperties; string pattern; mixed props) 
                patternProperties[Regexp.SimpleRegexp(pattern)] = props; 
 
        foreach (value; mixed prop_key; mixed property) 
        { 
            if (!stringp(prop_key)) 
                errors += ({ sprintf("JSON invalid: Object %O, expected property to be a string got %O\n", key, prop_key) }); 
 
            array(mapping) prop_schema = ({}); 
            if (has_schema_mapping_mapping(schema, "properties") && 
                has_index(schema->properties, prop_key)) 
                prop_schema = ({ schema->properties[prop_key] }); 
            else 
                foreach (patternProperties; 
                         Regexp.SimpleRegexp re; mixed prop_s) 
                    if (re->match(prop_key)) 
                        prop_schema += ({ prop_s }); 
            if (sizeof(prop_schema) == 0 && additionalProperties) 
                // if there is no properties for prop_key and no matching 
                // patter, then fallback to additionalProperties: 
                prop_schema = ({ additionalProperties }); 
 
            if (sizeof(prop_schema)) 
                errors += map(prop_schema, 
                              lambda(mixed s) { 
                                  return validate_properties( 
                                      prop_key, property, s); }); 
            else if (!additionalProperties_allowed) 
                // If schema->additionalProperties is a JSON boolean with value 
                // true, then we don't need to verify the items without a 
                // matching schema 
                errors += ({ sprintf("JSON invalid: Object %O has unexpected property %O\n", 
                                     key, prop_key) }); 
        } 
 
        errors = filter(errors, stringp); // remove all non-errors 
        if (sizeof(errors)) return errors * ""; 
        else return 0; 
    } 
}