Source file json_schema_validator.ml

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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
module Json = Chatoyant_runtime.Json
module Ast = Json_schema_ast
module Output = Json_schema_output
module Regex = Json_schema_regex
module Resolver = Json_schema_resolver

type options = { format_assertion : bool; resources : Resolver.resource list }

let default_options = { format_assertion = false; resources = [] }

type eval_result = {
  valid : bool;
  errors : Output.error list;
  evaluated_properties : string list;
  evaluated_items : int list;
}

type context = {
  options : options;
  resolver : Resolver.t;
  mutable ref_stack : (string * string) list;
  mutable validation_enabled : bool;
}

let draft202012_meta_schema = "https://json-schema.org/draft/2020-12/schema"

let validation_vocabulary =
  "https://json-schema.org/draft/2020-12/vocab/validation"

let empty =
  { valid = true; errors = []; evaluated_properties = []; evaluated_items = [] }

let fail ~instance_path ~schema_path ~keyword message =
  {
    empty with
    valid = false;
    errors = [ Output.error ~instance_path ~schema_path ~keyword ~message ];
  }

let uniq values =
  List.fold_left
    (fun acc value -> if List.mem value acc then acc else value :: acc)
    [] values
  |> List.rev

let merge results =
  {
    valid = List.for_all (fun result -> result.valid) results;
    errors = List.concat (List.map (fun result -> result.errors) results);
    evaluated_properties =
      results
      |> List.concat_map (fun result -> result.evaluated_properties)
      |> uniq;
    evaluated_items =
      results |> List.concat_map (fun result -> result.evaluated_items) |> uniq;
  }

let as_child_property name result =
  if result.valid then
    { result with evaluated_properties = [ name ]; evaluated_items = [] }
  else result

let as_child_item index result =
  if result.valid then
    { result with evaluated_properties = []; evaluated_items = [ index ] }
  else result

let append_dynamic_scope (ctx : context) base_uri
    (dynamic_scope : (string * Resolver.target) list) =
  Resolver.dynamic_anchors_for_resource ctx.resolver base_uri
  |> List.fold_left
       (fun (scope : (string * Resolver.target) list)
            (anchor, (target : Resolver.target)) ->
         if
           List.exists
             (fun (_, (existing : Resolver.target)) ->
               existing.Resolver.uri = target.Resolver.uri)
             scope
         then scope
         else scope @ [ (anchor, target) ])
       dynamic_scope

let json_field name = function
  | Json.Object fields -> List.assoc_opt name fields
  | _ -> None

let json_object = function Json.Object fields -> Some fields | _ -> None
let json_array = function Json.Array values -> Some values | _ -> None
let json_string = function Json.String value -> Some value | _ -> None
let json_float = function Json.Float value -> Some value | _ -> None

let json_int = function
  | Json.Float value when Float.is_integer value -> Some (Int.of_float value)
  | _ -> None

let json_bool = function Json.Bool value -> Some value | _ -> None

let vocabulary_validation_enabled ctx ~base_uri schema =
  match Option.bind (json_field "$schema" schema) json_string with
  | None -> ctx.validation_enabled
  | Some schema_uri -> (
      match Resolver.resolve ctx.resolver ~base_uri schema_uri with
      | None -> ctx.validation_enabled
      | Some target -> (
          match
            Option.bind (json_field "$vocabulary" target.schema) json_object
          with
          | None -> true
          | Some vocabularies -> (
              match
                Option.bind
                  (List.assoc_opt validation_vocabulary vocabularies)
                  json_bool
              with
              | Some true -> true
              | Some false | None -> false)))

let object_keys fields = List.map fst fields

let kind = function
  | Json.Null -> "null"
  | Bool _ -> "boolean"
  | Float value when Float.is_integer value -> "integer"
  | Float _ -> "number"
  | String _ -> "string"
  | Array _ -> "array"
  | Object _ -> "object"

let type_matches expected value =
  match (expected, value) with
  | "null", Json.Null -> true
  | "boolean", Json.Bool _ -> true
  | "object", Json.Object _ -> true
  | "array", Json.Array _ -> true
  | "number", Json.Float _ -> true
  | "integer", Json.Float value -> Float.is_integer value
  | "string", Json.String _ -> true
  | _ -> false

let rec json_equal left right =
  match (left, right) with
  | Json.Null, Json.Null -> true
  | Bool a, Bool b -> a = b
  | Float a, Float b -> Float.equal a b
  | String a, String b -> String.equal a b
  | Array a, Array b ->
      List.length a = List.length b && List.for_all2 json_equal a b
  | Object a, Object b ->
      let keys_a = object_keys a |> List.sort_uniq String.compare in
      let keys_b = object_keys b |> List.sort_uniq String.compare in
      keys_a = keys_b
      && List.for_all
           (fun key ->
             match (List.assoc_opt key a, List.assoc_opt key b) with
             | Some a, Some b -> json_equal a b
             | _ -> false)
           keys_a
  | _ -> false

let utf8_length value =
  let count = ref 0 in
  String.iter
    (fun ch -> if Char.code ch land 0xC0 <> 0x80 then incr count)
    value;
  !count

let pointer_append base token =
  let escaped =
    token |> String.split_on_char '~' |> String.concat "~0"
    |> String.split_on_char '/' |> String.concat "~1"
  in
  if base = "" then "/" ^ escaped else base ^ "/" ^ escaped

let schema_append base keyword = pointer_append base keyword
let index_path base index = pointer_append base (string_of_int index)

let multiple_of number divisor =
  if divisor = 0.0 then true
  else
    let quotient = number /. divisor in
    let nearest = Float.round quotient in
    Float.abs (quotient -. nearest) < 0.000000001

let schema_valued_keyword = function
  | "additionalProperties" | "unevaluatedProperties" | "propertyNames" | "items"
  | "contains" | "unevaluatedItems" | "not" | "if" | "then" | "else" ->
      true
  | _ -> false

let schema_array_keyword = function
  | "prefixItems" | "allOf" | "anyOf" | "oneOf" -> true
  | _ -> false

let schema_map_keyword = function
  | "$defs" | "definitions" | "properties" | "patternProperties"
  | "dependentSchemas" ->
      true
  | _ -> false

let valid_type_name = function
  | "null" | "boolean" | "object" | "array" | "number" | "string" | "integer" ->
      true
  | _ -> false

let non_negative_integer_keyword = function
  | "minLength" | "maxLength" | "minItems" | "maxItems" | "minProperties"
  | "maxProperties" | "minContains" | "maxContains" ->
      true
  | _ -> false

let rec validate_schema_document ~instance_path ~schema_path schema =
  match schema with
  | Json.Bool _ -> empty
  | Json.Object fields ->
      fields
      |> List.map (fun (name, value) ->
          let path = pointer_append instance_path name in
          if name = "type" then
            validate_type_keyword_shape ~instance_path:path ~schema_path value
          else if non_negative_integer_keyword name then
            validate_non_negative_integer_shape ~instance_path:path ~schema_path
              ~keyword:name value
          else if schema_valued_keyword name then
            validate_schema_document ~instance_path:path ~schema_path value
          else if schema_array_keyword name then
            validate_schema_array_shape ~instance_path:path ~schema_path value
          else if schema_map_keyword name then
            validate_schema_map_shape ~instance_path:path ~schema_path value
          else empty)
      |> merge
  | _ ->
      fail ~instance_path ~schema_path ~keyword:"$schema"
        "schema document must be a boolean or object"

and validate_type_keyword_shape ~instance_path ~schema_path = function
  | Json.String value when valid_type_name value -> empty
  | Json.Array values
    when values <> []
         && List.for_all
              (function
                | Json.String value -> valid_type_name value | _ -> false)
              values ->
      empty
  | _ ->
      fail ~instance_path ~schema_path ~keyword:"type"
        "type must be a valid JSON Schema primitive name or array of names"

and validate_non_negative_integer_shape ~instance_path ~schema_path ~keyword =
  function
  | Json.Float value when Float.is_integer value && value >= 0.0 -> empty
  | _ ->
      fail ~instance_path ~schema_path ~keyword
        (keyword ^ " must be a non-negative integer")

and validate_schema_array_shape ~instance_path ~schema_path = function
  | Json.Array values ->
      values
      |> List.mapi (fun index value ->
          validate_schema_document
            ~instance_path:(index_path instance_path index)
            ~schema_path value)
      |> merge
  | _ ->
      fail ~instance_path ~schema_path ~keyword:"schema-array"
        "keyword must contain an array of schemas"

and validate_schema_map_shape ~instance_path ~schema_path = function
  | Json.Object fields ->
      fields
      |> List.map (fun (name, value) ->
          validate_schema_document
            ~instance_path:(pointer_append instance_path name)
            ~schema_path value)
      |> merge
  | _ ->
      fail ~instance_path ~schema_path ~keyword:"schema-map"
        "keyword must contain an object whose values are schemas"

let rec validate_schema ctx ~base_uri ~schema_path ~instance_path ~dynamic_scope
    schema instance =
  match schema with
  | Json.Bool true -> empty
  | Json.Bool false ->
      fail ~instance_path ~schema_path ~keyword:"false" "boolean schema false"
  | Json.Object _ ->
      validate_object_schema ctx ~base_uri ~schema_path ~instance_path
        ~dynamic_scope schema instance
  | _ ->
      fail ~instance_path ~schema_path ~keyword:"schema"
        "schema position must contain a boolean or object"

and validate_object_schema ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema instance =
  let base_uri = Resolver.child_base_uri ctx.resolver ~base_uri schema in
  let previous_validation_enabled = ctx.validation_enabled in
  ctx.validation_enabled <- vocabulary_validation_enabled ctx ~base_uri schema;
  let finish result =
    ctx.validation_enabled <- previous_validation_enabled;
    result
  in
  try
    let dynamic_scope = append_dynamic_scope ctx base_uri dynamic_scope in
    let ref_results =
      [
        validate_ref_keyword ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema instance;
        validate_dynamic_ref_keyword ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema instance;
      ]
      |> List.filter_map Fun.id
    in
    let assertion_results =
      (if ctx.validation_enabled then
         [
           validate_type ~schema_path ~instance_path schema instance;
           validate_enum ~schema_path ~instance_path schema instance;
           validate_const ~schema_path ~instance_path schema instance;
           validate_number ~schema_path ~instance_path schema instance;
           validate_string ~schema_path ~instance_path ctx.options schema
             instance;
         ]
       else [])
      @ [
          validate_object_keywords ctx ~base_uri ~schema_path ~instance_path
            ~dynamic_scope schema instance;
          validate_array_keywords ctx ~base_uri ~schema_path ~instance_path
            ~dynamic_scope schema instance;
          validate_dependent_keywords ctx ~base_uri ~schema_path ~instance_path
            ~dynamic_scope schema instance;
        ]
      |> List.filter_map Fun.id
    in
    let applicator_results =
      validate_applicators ctx ~base_uri ~schema_path ~instance_path
        ~dynamic_scope schema instance
    in
    let before_unevaluated =
      merge (ref_results @ assertion_results @ applicator_results)
    in
    let unevaluated_results =
      [
        validate_unevaluated_properties ctx ~base_uri ~schema_path
          ~instance_path ~dynamic_scope
          ~evaluated:before_unevaluated.evaluated_properties schema instance;
        validate_unevaluated_items ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope ~evaluated:before_unevaluated.evaluated_items schema
          instance;
      ]
      |> List.filter_map Fun.id
    in
    finish
      (merge
         (ref_results @ assertion_results @ applicator_results
        @ unevaluated_results))
  with exn ->
    ctx.validation_enabled <- previous_validation_enabled;
    raise exn

and validate_ref_keyword ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema instance =
  match Option.bind (json_field "$ref" schema) json_string with
  | None -> None
  | Some reference -> (
      match Resolver.resolve ctx.resolver ~base_uri reference with
      | None ->
          Some
            (fail ~instance_path
               ~schema_path:(schema_append schema_path "$ref")
               ~keyword:"$ref"
               ("unresolved reference " ^ reference))
      | Some target ->
          if target.uri = draft202012_meta_schema then
            Some
              (validate_schema_document ~instance_path ~schema_path:target.uri
                 instance)
          else
            let stack_key = (target.uri, instance_path) in
            if List.mem stack_key ctx.ref_stack then Some empty
            else (
              ctx.ref_stack <- stack_key :: ctx.ref_stack;
              let result =
                validate_schema ctx ~base_uri:target.base_uri
                  ~schema_path:target.uri ~instance_path ~dynamic_scope
                  target.schema instance
              in
              ctx.ref_stack <- List.tl ctx.ref_stack;
              Some result))

and validate_dynamic_ref_keyword ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema instance =
  match Option.bind (json_field "$dynamicRef" schema) json_string with
  | None -> None
  | Some reference -> (
      match
        Resolver.resolve_dynamic ctx.resolver ~base_uri ~dynamic_scope reference
      with
      | None ->
          Some
            (fail ~instance_path
               ~schema_path:(schema_append schema_path "$dynamicRef")
               ~keyword:"$dynamicRef"
               ("unresolved dynamic reference " ^ reference))
      | Some target ->
          let stack_key = (target.uri, instance_path) in
          if List.mem stack_key ctx.ref_stack then Some empty
          else (
            ctx.ref_stack <- stack_key :: ctx.ref_stack;
            let result =
              validate_schema ctx ~base_uri:target.base_uri
                ~schema_path:target.uri ~instance_path ~dynamic_scope
                target.schema instance
            in
            ctx.ref_stack <- List.tl ctx.ref_stack;
            Some result))

and validate_type ~schema_path ~instance_path schema instance =
  match json_field "type" schema with
  | None -> None
  | Some (Json.String expected) ->
      if type_matches expected instance then Some empty
      else
        Some
          (fail ~instance_path
             ~schema_path:(schema_append schema_path "type")
             ~keyword:"type"
             ("expected " ^ expected ^ ", got " ^ kind instance))
  | Some (Json.Array values) ->
      let expected = List.filter_map json_string values in
      if List.exists (fun expected -> type_matches expected instance) expected
      then Some empty
      else
        Some
          (fail ~instance_path
             ~schema_path:(schema_append schema_path "type")
             ~keyword:"type"
             ("expected one of "
             ^ String.concat ", " expected
             ^ ", got " ^ kind instance))
  | Some _ -> None

and validate_enum ~schema_path ~instance_path schema instance =
  match Option.bind (json_field "enum" schema) json_array with
  | None -> None
  | Some values ->
      if List.exists (json_equal instance) values then Some empty
      else
        Some
          (fail ~instance_path
             ~schema_path:(schema_append schema_path "enum")
             ~keyword:"enum" "value is not one of the allowed enum members")

and validate_const ~schema_path ~instance_path schema instance =
  match json_field "const" schema with
  | None -> None
  | Some value ->
      if json_equal instance value then Some empty
      else
        Some
          (fail ~instance_path
             ~schema_path:(schema_append schema_path "const")
             ~keyword:"const" "value does not equal const")

and validate_number ~schema_path ~instance_path schema instance =
  match instance with
  | Json.Float number ->
      let checks =
        [
          ( "multipleOf",
            (fun divisor -> multiple_of number divisor),
            "number is not a multiple" );
          ( "maximum",
            (fun maximum -> number <= maximum),
            "number is greater than maximum" );
          ( "exclusiveMaximum",
            (fun maximum -> number < maximum),
            "number is not below exclusiveMaximum" );
          ( "minimum",
            (fun minimum -> number >= minimum),
            "number is less than minimum" );
          ( "exclusiveMinimum",
            (fun minimum -> number > minimum),
            "number is not above exclusiveMinimum" );
        ]
      in
      let results =
        List.filter_map
          (fun (keyword, predicate, message) ->
            match Option.bind (json_field keyword schema) json_float with
            | None -> None
            | Some bound ->
                if predicate bound then Some empty
                else
                  Some
                    (fail ~instance_path
                       ~schema_path:(schema_append schema_path keyword)
                       ~keyword message))
          checks
      in
      Some (merge results)
  | _ -> None

and validate_string ~schema_path ~instance_path options schema instance =
  match instance with
  | Json.String text ->
      let length = utf8_length text in
      let min_result =
        match Option.bind (json_field "minLength" schema) json_int with
        | Some minimum when length < minimum ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "minLength")
              ~keyword:"minLength" "string is shorter than minLength"
        | _ -> empty
      in
      let max_result =
        match Option.bind (json_field "maxLength" schema) json_int with
        | Some maximum when length > maximum ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "maxLength")
              ~keyword:"maxLength" "string is longer than maxLength"
        | _ -> empty
      in
      let pattern_result =
        match Option.bind (json_field "pattern" schema) json_string with
        | Some pattern when not (Regex.matches ~pattern text) ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "pattern")
              ~keyword:"pattern" "string does not match pattern"
        | _ -> empty
      in
      let format_result =
        if options.format_assertion then
          validate_format ~schema_path ~instance_path schema text
        else empty
      in
      Some (merge [ min_result; max_result; pattern_result; format_result ])
  | _ -> None

and validate_format ~schema_path ~instance_path schema text =
  match Option.bind (json_field "format" schema) json_string with
  | None -> empty
  | Some "email" ->
      if String.contains text '@' then empty
      else
        fail ~instance_path
          ~schema_path:(schema_append schema_path "format")
          ~keyword:"format" "invalid email format"
  | Some "uuid" ->
      if String.length text = 36 then empty
      else
        fail ~instance_path
          ~schema_path:(schema_append schema_path "format")
          ~keyword:"format" "invalid uuid format"
  | Some _ -> empty

and validate_object_keywords ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema instance =
  match instance with
  | Json.Object fields ->
      let count = List.length fields in
      let min_result =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "minProperties" schema) json_int )
        with
        | true, Some minimum when count < minimum ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "minProperties")
              ~keyword:"minProperties" "object has too few properties"
        | _ -> empty
      in
      let max_result =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "maxProperties" schema) json_int )
        with
        | true, Some maximum when count > maximum ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "maxProperties")
              ~keyword:"maxProperties" "object has too many properties"
        | _ -> empty
      in
      let required_result =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "required" schema) json_array )
        with
        | false, _ -> empty
        | true, None -> empty
        | true, Some required ->
            required
            |> List.filter_map json_string
            |> List.map (fun name ->
                if List.mem_assoc name fields then empty
                else
                  fail ~instance_path
                    ~schema_path:(schema_append schema_path "required")
                    ~keyword:"required"
                    ("missing required property " ^ name))
            |> merge
      in
      let property_result, property_names =
        validate_properties ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema fields
      in
      let pattern_result, pattern_names =
        validate_pattern_properties ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema fields
      in
      let additional_result =
        validate_additional_properties ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema fields
          ~covered:(uniq (property_names @ pattern_names))
      in
      let property_names_result =
        validate_property_names ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema fields
      in
      Some
        (merge
           [
             min_result;
             max_result;
             required_result;
             property_result;
             pattern_result;
             additional_result;
             property_names_result;
           ])
  | _ -> None

and validate_properties ctx ~base_uri ~schema_path ~instance_path ~dynamic_scope
    schema fields =
  match Option.bind (json_field "properties" schema) json_object with
  | None -> (empty, [])
  | Some properties ->
      let results, names =
        List.fold_left
          (fun (results, names) (name, subschema) ->
            match List.assoc_opt name fields with
            | None -> (results, names)
            | Some value ->
                let result =
                  validate_schema ctx ~base_uri
                    ~schema_path:
                      (schema_append
                         (schema_append schema_path "properties")
                         name)
                    ~instance_path:(pointer_append instance_path name)
                    ~dynamic_scope subschema value
                  |> as_child_property name
                in
                (result :: results, name :: names))
          ([], []) properties
      in
      (merge (List.rev results), List.rev names)

and validate_pattern_properties ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema fields =
  match Option.bind (json_field "patternProperties" schema) json_object with
  | None -> (empty, [])
  | Some patterns ->
      let results, names =
        List.fold_left
          (fun (results, names) (pattern, subschema) ->
            List.fold_left
              (fun (results, names) (name, value) ->
                if Regex.matches ~pattern name then
                  let result =
                    validate_schema ctx ~base_uri
                      ~schema_path:
                        (schema_append
                           (schema_append schema_path "patternProperties")
                           pattern)
                      ~instance_path:(pointer_append instance_path name)
                      ~dynamic_scope subschema value
                    |> as_child_property name
                  in
                  (result :: results, name :: names)
                else (results, names))
              (results, names) fields)
          ([], []) patterns
      in
      (merge (List.rev results), List.rev (uniq names))

and validate_additional_properties ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema fields ~covered =
  match json_field "additionalProperties" schema with
  | None -> empty
  | Some subschema ->
      fields
      |> List.filter (fun (name, _) -> not (List.mem name covered))
      |> List.map (fun (name, value) ->
          validate_schema ctx ~base_uri
            ~schema_path:(schema_append schema_path "additionalProperties")
            ~instance_path:(pointer_append instance_path name)
            ~dynamic_scope subschema value
          |> as_child_property name)
      |> merge

and validate_property_names ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema fields =
  match json_field "propertyNames" schema with
  | None -> empty
  | Some subschema ->
      fields
      |> List.map (fun (name, _) ->
          validate_schema ctx ~base_uri
            ~schema_path:(schema_append schema_path "propertyNames")
            ~instance_path:(pointer_append instance_path name)
            ~dynamic_scope subschema (Json.String name))
      |> merge

and validate_dependent_keywords ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema instance =
  match instance with
  | Json.Object fields ->
      let dependent_required =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "dependentRequired" schema) json_object )
        with
        | false, _ -> empty
        | true, None -> empty
        | true, Some dependencies ->
            dependencies
            |> List.concat_map (fun (name, requirements) ->
                if not (List.mem_assoc name fields) then []
                else
                  match json_array requirements with
                  | None -> []
                  | Some required ->
                      required
                      |> List.filter_map json_string
                      |> List.map (fun required_name ->
                          if List.mem_assoc required_name fields then empty
                          else
                            fail ~instance_path
                              ~schema_path:
                                (schema_append schema_path "dependentRequired")
                              ~keyword:"dependentRequired"
                              (name ^ " requires property " ^ required_name)))
            |> merge
      in
      let dependent_schemas =
        match
          Option.bind (json_field "dependentSchemas" schema) json_object
        with
        | None -> empty
        | Some dependencies ->
            dependencies
            |> List.filter_map (fun (name, subschema) ->
                if List.mem_assoc name fields then
                  Some
                    (validate_schema ctx ~base_uri
                       ~schema_path:
                         (schema_append
                            (schema_append schema_path "dependentSchemas")
                            name)
                       ~instance_path ~dynamic_scope subschema instance)
                else None)
            |> merge
      in
      Some (merge [ dependent_required; dependent_schemas ])
  | _ -> None

and validate_array_keywords ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema instance =
  match instance with
  | Json.Array values ->
      let length = List.length values in
      let min_result =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "minItems" schema) json_int )
        with
        | true, Some minimum when length < minimum ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "minItems")
              ~keyword:"minItems" "array has too few items"
        | _ -> empty
      in
      let max_result =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "maxItems" schema) json_int )
        with
        | true, Some maximum when length > maximum ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "maxItems")
              ~keyword:"maxItems" "array has too many items"
        | _ -> empty
      in
      let unique_result =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "uniqueItems" schema) json_bool )
        with
        | true, Some true when not (unique_items values) ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "uniqueItems")
              ~keyword:"uniqueItems" "array items are not unique"
        | _ -> empty
      in
      let prefix_result, prefix_count =
        validate_prefix_items ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema values
      in
      let items_result =
        validate_items ctx ~base_uri ~schema_path ~instance_path ~dynamic_scope
          schema values ~prefix_count
      in
      let contains_result =
        validate_contains ctx ~base_uri ~schema_path ~instance_path
          ~dynamic_scope schema values
      in
      Some
        (merge
           [
             min_result;
             max_result;
             unique_result;
             prefix_result;
             items_result;
             contains_result;
           ])
  | _ -> None

and unique_items values =
  let rec loop = function
    | [] -> true
    | value :: rest -> (not (List.exists (json_equal value) rest)) && loop rest
  in
  loop values

and validate_prefix_items ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema values =
  match Option.bind (json_field "prefixItems" schema) json_array with
  | None -> (empty, 0)
  | Some schemas ->
      let results =
        List.mapi
          (fun index subschema ->
            match List.nth_opt values index with
            | None -> empty
            | Some value ->
                validate_schema ctx ~base_uri
                  ~schema_path:
                    (index_path (schema_append schema_path "prefixItems") index)
                  ~instance_path:(index_path instance_path index)
                  ~dynamic_scope subschema value
                |> as_child_item index)
          schemas
      in
      (merge results, List.length schemas)

and validate_items ctx ~base_uri ~schema_path ~instance_path ~dynamic_scope
    schema values ~prefix_count =
  match json_field "items" schema with
  | None -> empty
  | Some subschema ->
      values
      |> List.mapi (fun index value -> (index, value))
      |> List.filter (fun (index, _) -> index >= prefix_count)
      |> List.map (fun (index, value) ->
          validate_schema ctx ~base_uri
            ~schema_path:(schema_append schema_path "items")
            ~instance_path:(index_path instance_path index)
            ~dynamic_scope subschema value
          |> as_child_item index)
      |> merge

and validate_contains ctx ~base_uri ~schema_path ~instance_path ~dynamic_scope
    schema values =
  match json_field "contains" schema with
  | None -> empty
  | Some subschema ->
      let matches =
        values
        |> List.mapi (fun index value ->
            let result =
              validate_schema ctx ~base_uri
                ~schema_path:(schema_append schema_path "contains")
                ~instance_path:(index_path instance_path index)
                ~dynamic_scope subschema value
            in
            (index, result))
        |> List.filter (fun (_, result) -> result.valid)
      in
      let count = List.length matches in
      let min_contains =
        if ctx.validation_enabled then
          match Option.bind (json_field "minContains" schema) json_int with
          | Some value -> value
          | None -> 1
        else 0
      in
      let min_result =
        if count < min_contains then
          fail ~instance_path
            ~schema_path:(schema_append schema_path "contains")
            ~keyword:"contains" "array does not contain enough matching items"
        else empty
      in
      let max_result =
        match
          ( ctx.validation_enabled,
            Option.bind (json_field "maxContains" schema) json_int )
        with
        | true, Some maximum when count > maximum ->
            fail ~instance_path
              ~schema_path:(schema_append schema_path "maxContains")
              ~keyword:"maxContains" "array contains too many matching items"
        | _ -> empty
      in
      let result = merge [ min_result; max_result ] in
      if result.valid then
        { result with evaluated_items = List.map fst matches }
      else result

and validate_applicators ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope schema instance =
  let all_of =
    match Option.bind (json_field "allOf" schema) json_array with
    | None -> empty
    | Some schemas ->
        schemas
        |> List.mapi (fun index subschema ->
            validate_schema ctx ~base_uri
              ~schema_path:
                (index_path (schema_append schema_path "allOf") index)
              ~instance_path ~dynamic_scope subschema instance)
        |> merge
  in
  let any_of =
    match Option.bind (json_field "anyOf" schema) json_array with
    | None -> empty
    | Some schemas ->
        let results =
          schemas
          |> List.mapi (fun index subschema ->
              validate_schema ctx ~base_uri
                ~schema_path:
                  (index_path (schema_append schema_path "anyOf") index)
                ~instance_path ~dynamic_scope subschema instance)
        in
        let passing = List.filter (fun result -> result.valid) results in
        if passing = [] then
          fail ~instance_path
            ~schema_path:(schema_append schema_path "anyOf")
            ~keyword:"anyOf" "value does not match any subschema"
        else merge passing
  in
  let one_of =
    match Option.bind (json_field "oneOf" schema) json_array with
    | None -> empty
    | Some schemas ->
        let results =
          schemas
          |> List.mapi (fun index subschema ->
              validate_schema ctx ~base_uri
                ~schema_path:
                  (index_path (schema_append schema_path "oneOf") index)
                ~instance_path ~dynamic_scope subschema instance)
        in
        let passing = List.filter (fun result -> result.valid) results in
        if List.length passing = 1 then merge passing
        else
          fail ~instance_path
            ~schema_path:(schema_append schema_path "oneOf")
            ~keyword:"oneOf" "value must match exactly one subschema"
  in
  let not_ =
    match json_field "not" schema with
    | None -> empty
    | Some subschema ->
        let result =
          validate_schema ctx ~base_uri
            ~schema_path:(schema_append schema_path "not")
            ~instance_path ~dynamic_scope subschema instance
        in
        if result.valid then
          fail ~instance_path
            ~schema_path:(schema_append schema_path "not")
            ~keyword:"not" "value matched forbidden subschema"
        else empty
  in
  let conditional =
    match json_field "if" schema with
    | None -> empty
    | Some if_schema -> (
        let if_result =
          validate_schema ctx ~base_uri
            ~schema_path:(schema_append schema_path "if")
            ~instance_path ~dynamic_scope if_schema instance
        in
        if if_result.valid then
          match json_field "then" schema with
          | None -> if_result
          | Some then_schema ->
              merge
                [
                  if_result;
                  validate_schema ctx ~base_uri
                    ~schema_path:(schema_append schema_path "then")
                    ~instance_path ~dynamic_scope then_schema instance;
                ]
        else
          match json_field "else" schema with
          | None -> empty
          | Some else_schema ->
              validate_schema ctx ~base_uri
                ~schema_path:(schema_append schema_path "else")
                ~instance_path ~dynamic_scope else_schema instance)
  in
  [ all_of; any_of; one_of; not_; conditional ]

and validate_unevaluated_properties ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope ~evaluated schema instance =
  match (json_field "unevaluatedProperties" schema, instance) with
  | None, _ -> None
  | Some subschema, Json.Object fields ->
      fields
      |> List.filter (fun (name, _) -> not (List.mem name evaluated))
      |> List.map (fun (name, value) ->
          validate_schema ctx ~base_uri
            ~schema_path:(schema_append schema_path "unevaluatedProperties")
            ~instance_path:(pointer_append instance_path name)
            ~dynamic_scope subschema value
          |> as_child_property name)
      |> merge
      |> fun result -> Some result
  | Some _, _ -> Some empty

and validate_unevaluated_items ctx ~base_uri ~schema_path ~instance_path
    ~dynamic_scope ~evaluated schema instance =
  match (json_field "unevaluatedItems" schema, instance) with
  | None, _ -> None
  | Some subschema, Json.Array values ->
      values
      |> List.mapi (fun index value -> (index, value))
      |> List.filter (fun (index, _) -> not (List.mem index evaluated))
      |> List.map (fun (index, value) ->
          validate_schema ctx ~base_uri
            ~schema_path:(schema_append schema_path "unevaluatedItems")
            ~instance_path:(index_path instance_path index)
            ~dynamic_scope subschema value
          |> as_child_item index)
      |> merge
      |> fun result -> Some result
  | Some _, _ -> Some empty

let validate ?(options = default_options) schema instance =
  let schema_json = Ast.to_json schema in
  let resolver = Resolver.create ~resources:options.resources schema_json in
  let ctx = { options; resolver; ref_stack = []; validation_enabled = true } in
  let result =
    validate_schema ctx
      ~base_uri:(Resolver.root_base_uri resolver)
      ~schema_path:"" ~instance_path:"" ~dynamic_scope:[] schema_json instance
  in
  { Output.valid = result.valid; errors = result.errors }

let validate_json ?options schema_json instance =
  match Ast.of_json schema_json with
  | Error _ as error -> error
  | Ok schema -> Ok (validate ?options schema instance)