package json-data-encoding
Type-safe encoding to and decoding from JSON
Install
Dune Dependency
Authors
Maintainers
Sources
data-encoding-v1.0.1.tar.gz
md5=82d6e7783274595c82cff4562e2b06a2
sha512=df5d00dfef8afeada8a6aee2a97d491a2ce20cfe90aed203848f6098ba05ba60e2ee9d1afc0c6c07cf32dad3f8e34c0b55cf900ef1f2e7a72d704f07fd32e651
doc/src/json-data-encoding/json_schema.ml.html
Source file json_schema.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 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320 1321 1322 1323 1324 1325 1326 1327 1328 1329 1330 1331 1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350 1351 1352 1353 1354 1355 1356 1357 1358 1359 1360 1361 1362 1363 1364 1365 1366 1367 1368 1369 1370 1371 1372 1373 1374 1375 1376 1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393 1394 1395 1396 1397 1398 1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412 1413 1414 1415 1416 1417 1418 1419 1420 1421 1422 1423 1424 1425 1426 1427 1428 1429 1430 1431 1432 1433 1434 1435 1436 1437 1438 1439 1440 1441 1442 1443 1444 1445 1446 1447 1448 1449 1450 1451 1452 1453 1454 1455 1456 1457 1458 1459 1460 1461 1462 1463 1464 1465 1466 1467 1468 1469 1470 1471 1472 1473 1474 1475 1476 1477 1478 1479 1480 1481 1482 1483 1484 1485 1486 1487 1488 1489 1490 1491 1492 1493 1494 1495 1496 1497 1498 1499 1500 1501 1502 1503 1504 1505 1506 1507 1508 1509 1510 1511 1512 1513 1514 1515
(*****************************************************************************) (* *) (* Open Source License *) (* Copyright 2014 OCamlPro *) (* Copyright (c) 2020 Nomadic Labs <contact@nomadic-labs.com> *) (* *) (* Permission is hereby granted, free of charge, to any person obtaining a *) (* copy of this software and associated documentation files (the "Software"),*) (* to deal in the Software without restriction, including without limitation *) (* the rights to use, copy, modify, merge, publish, distribute, sublicense, *) (* and/or sell copies of the Software, and to permit persons to whom the *) (* Software is furnished to do so, subject to the following conditions: *) (* *) (* The above copyright notice and this permission notice shall be included *) (* in all copies or substantial portions of the Software. *) (* *) (* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR*) (* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, *) (* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL *) (* THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER*) (* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING *) (* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER *) (* DEALINGS IN THE SOFTWARE. *) (* *) (*****************************************************************************) (* TODO: validator *) (* The currently handled version *) let version = "http://json-schema.org/draft-04/schema#" (*-- types -----------------------------------------------------------------*) module Def_map = Map.Make (struct type t = Json_query.path let compare = compare end) module Id_map = Map.Make (String) module Id_set = Set.Make (String) (* The root of a schema with the named definitions, a precomputed ID-element map and a cache for external documents. *) type schema = { root : element; source : Uri.t; (* whose fragment should be empty *) definitions : element Def_map.t; ids : element Id_map.t; world : schema list; } and element = { title : string option; description : string option; default : Json_repr.any option; enum : Json_repr.any list option; kind : element_kind; format : string option; id : string option; } and element_kind = | Object of object_specs | Array of element list * array_specs | Monomorphic_array of element * array_specs | Combine of combinator * element list | Def_ref of Json_query.path | Id_ref of string | Ext_ref of Uri.t | String of string_specs | Integer of numeric_specs | Number of numeric_specs | Boolean | Null | Any | Dummy and combinator = Any_of | One_of | All_of | Not and array_specs = { min_items : int; max_items : int option; unique_items : bool; additional_items : element option; } and numeric_specs = { multiple_of : float option; minimum : (float * [`Inclusive | `Exclusive]) option; maximum : (float * [`Inclusive | `Exclusive]) option; } and object_specs = { properties : (string * element * bool * Json_repr.any option) list; pattern_properties : (string * element) list; additional_properties : element option; min_properties : int; max_properties : int option; schema_dependencies : (string * element) list; property_dependencies : (string * string list) list; } and string_specs = { pattern : string option; min_length : int; max_length : int option; str_format : string option; } let sort_assoc_list_by_string_key kvs = List.sort (fun (a, _) (b, _) -> String.compare a b) kvs (* box an element kind without any optional field *) let element kind = { title = None; description = None; default = None; kind; format = None; enum = None; id = None; } (*-- equality --------------------------------------------------------------*) let rec eq_element a b = a == b || a.title = b.title && a.description = b.description && Option.map Json_repr.from_any a.default = Option.map Json_repr.from_any b.default && Option.map (List.map Json_repr.from_any) a.enum = Option.map (List.map Json_repr.from_any) b.enum && eq_kind a.kind b.kind && a.format = b.format && a.id = b.id and eq_kind a b = match (a, b) with | Object aa, Object ab -> eq_object_specs aa ab | Array (esa, sa), Array (esb, sb) -> List.compare_lengths esa esb = 0 && List.for_all2 eq_element esa esb && eq_array_specs sa sb | Monomorphic_array (ea, sa), Monomorphic_array (eb, sb) -> eq_element ea eb && eq_array_specs sa sb | Combine (ca, esa), Combine (cb, esb) -> ca = cb && List.compare_lengths esa esb = 0 && List.for_all2 eq_element esa esb | Def_ref pa, Def_ref pb -> pa = pb | Id_ref ra, Id_ref rb -> ra = rb | Ext_ref ra, Ext_ref rb -> ra = rb | String sa, String sb -> sa = sb | Integer na, Integer nb -> na = nb | Number na, Number nb -> na = nb | Boolean, Boolean -> true | Null, Null -> true | Any, Any -> true | Dummy, Dummy -> true | _ -> false and eq_object_specs a b = Int.equal a.min_properties b.min_properties && Option.equal Int.equal a.max_properties b.max_properties && List.compare_lengths a.property_dependencies b.property_dependencies = 0 && List.sort compare a.property_dependencies = List.sort compare b.property_dependencies && Option.equal eq_element a.additional_properties b.additional_properties && List.compare_lengths a.pattern_properties b.pattern_properties = 0 && List.for_all2 (fun (na, ea) (nb, eb) -> String.equal na nb && eq_element ea eb) (sort_assoc_list_by_string_key a.pattern_properties) (sort_assoc_list_by_string_key b.pattern_properties) && List.compare_lengths a.schema_dependencies b.schema_dependencies = 0 && List.for_all2 (fun (na, ea) (nb, eb) -> String.equal na nb && eq_element ea eb) (sort_assoc_list_by_string_key a.schema_dependencies) (sort_assoc_list_by_string_key b.schema_dependencies) && List.compare_lengths a.properties b.properties = 0 && List.for_all2 (fun (na, ea, ra, da) (nb, eb, rb, db) -> String.equal na nb && eq_element ea eb && ra = rb && Option.map Json_repr.from_any da = Option.map Json_repr.from_any db) (List.sort (fun (x, _, _, _) (y, _, _, _) -> String.compare x y) a.properties) (List.sort (fun (x, _, _, _) (y, _, _, _) -> String.compare x y) b.properties) and eq_array_specs a b = a.min_items = b.min_items && a.max_items = b.max_items && a.unique_items = b.unique_items && match (a.additional_items, b.additional_items) with | Some a, Some b -> eq_element a b | None, None -> true | _, _ -> false (*-- human readable output -------------------------------------------------*) let pp_string ppf s = Json_repr.(pp (module Ezjsonm)) ppf (`String s) let pp_num ppf num = if abs_float num < 1000. then Format.fprintf ppf "%g" num else let is_positive, abs_num = if num < 0. then (false, ~-.num) else (true, num) in let already_printed = List.fold_left (fun already_printed delta -> already_printed || let candidate = log (abs_num +. delta) /. log 2. in if abs_float (ceil candidate -. candidate) < 0.00001 then ( Format.fprintf ppf "%s2^%g" (if is_positive then "" else "-") candidate ; if (is_positive && delta < 0.) || ((not is_positive) && delta > 0.) then Format.fprintf ppf "+%g" (abs_float delta) ; if (is_positive && delta > 0.) || ((not is_positive) && delta < 0.) then Format.fprintf ppf "-%g" (abs_float delta) ; true) else false) false [0.; 1.; -1.; 2.; -2.] in if not already_printed then Format.fprintf ppf "%f" abs_num let pp_numeric_specs ppf {multiple_of; minimum; maximum} = Format.fprintf ppf "%a%a%a" (fun ppf -> function | None -> () | Some v -> Format.fprintf ppf "multiple of %g" v) multiple_of (fun ppf -> function | None, _, _ | _, None, None -> () | _ -> Format.fprintf ppf ", ") (multiple_of, minimum, maximum) (fun ppf -> function | None, None -> () | minimum, maximum -> Format.fprintf ppf "∈ %a, %a" (fun ppf -> function | None -> Format.fprintf ppf "]∞" | Some (m, `Exclusive) -> Format.fprintf ppf "]%a" pp_num m | Some (m, `Inclusive) -> Format.fprintf ppf "[%a" pp_num m) minimum (fun ppf -> function | None -> Format.fprintf ppf "∞[" | Some (m, `Exclusive) -> Format.fprintf ppf "%a[" pp_num m | Some (m, `Inclusive) -> Format.fprintf ppf "%a]" pp_num m) maximum) (minimum, maximum) let pp_path ppf = function | [`Field "definitions"; `Field name] -> Format.fprintf ppf "%s" name | path -> Json_query.(print_path_as_json_path ~wildcards:true) ppf path let pp_desc element = match element with | {title = None; description = None} -> None | {title = Some text; description = None} | {title = None; description = Some text} -> Some (fun ppf () -> Format.fprintf ppf "/* @[<hov 0>%a@] */" Format.pp_print_text text) | {title = Some title; description = Some description} -> Some (fun ppf () -> Format.fprintf ppf "/* @[<v 0>@[<hov 0>%a@]@,@[<hov 0>%a@]@] */" Format.pp_print_text title Format.pp_print_text description) let rec pp_element ppf element = match element.id with | Some id -> Format.fprintf ppf "#%s" id | None -> ( match element.format with | Some format -> Format.fprintf ppf "%s" format | None -> ( match element.enum with | Some cases -> let pp_sep ppf () = Format.fprintf ppf "@ | " in Format.fprintf ppf "@[<hv 0>%a@]" (Format.pp_print_list ~pp_sep (Json_repr.pp_any ~compact:false ())) cases | None -> ( match pp_desc element with | Some pp_desc -> ( let stripped = {element with title = None; description = None} in match element.kind with | Combine _ -> Format.fprintf ppf "%a@,%a" pp_desc () pp_element stripped | Object specs -> Format.fprintf ppf "@[<v 2>{ %a@,%a }@]" pp_desc () pp_object_contents specs | _ -> Format.fprintf ppf "%a@ %a" pp_element stripped pp_desc () ) | None -> ( match element.kind with | String {pattern; min_length; max_length; str_format} -> ( let length_pp ppf = function | n, None when n > 0 -> Format.fprintf ppf " (%d <= length)" n | _, None -> Format.fprintf ppf "" | n, Some m when n > 0 -> Format.fprintf ppf " (%d <= length <= %d)" n m | _, Some n -> Format.fprintf ppf " (length <= %d)" n in match (pattern, str_format) with | None, None -> Format.fprintf ppf "string%a" length_pp (min_length, max_length) | Some pat, None -> Format.fprintf ppf "/%s/%a" pat length_pp (min_length, max_length) | None, Some fmt -> Format.fprintf ppf "%s%a" fmt length_pp (min_length, max_length) | Some pat, Some fmt -> Format.fprintf ppf "%s (/%s/)%a" fmt pat length_pp (min_length, max_length)) | Integer {multiple_of = None; minimum = None; maximum = None} -> Format.fprintf ppf "integer" | Integer specs -> Format.fprintf ppf "integer %a" pp_numeric_specs specs | Number {multiple_of = None; minimum = None; maximum = None} -> Format.fprintf ppf "number" | Number specs -> Format.fprintf ppf "number %a" pp_numeric_specs specs | Id_ref id -> Format.fprintf ppf "#%s" id | Def_ref path -> Format.fprintf ppf "$%a" pp_path path | Ext_ref uri -> Format.fprintf ppf "$%a" Uri.pp_hum uri | Boolean -> Format.fprintf ppf "boolean" | Null -> Format.fprintf ppf "null" | Any -> Format.fprintf ppf "any" | Dummy -> assert false | Combine (Not, [elt]) -> Format.fprintf ppf "! %a" pp_element elt | Combine (c, elts) -> let pp_sep ppf () = match c with | Any_of -> Format.fprintf ppf "@ | " | One_of -> Format.fprintf ppf "@ || " | All_of -> Format.fprintf ppf "@ && " | _ -> assert false in Format.fprintf ppf "@[<hv 0>%a@]" (Format.pp_print_list ~pp_sep pp_element) elts | Object { properties = []; pattern_properties = []; additional_properties = None; min_properties = 0; max_properties = Some 0; schema_dependencies = []; property_dependencies = []; } -> Format.fprintf ppf "{}" | Object specs -> Format.fprintf ppf "@[<v 2>{ %a }@]" pp_object_contents specs | Array (_, {max_items = Some 0}) | Monomorphic_array (_, {max_items = Some 0}) -> Format.fprintf ppf "[]" | Array (elements, {additional_items}) -> let pp_sep = let first = ref true in fun ppf () -> if !first then first := false else Format.fprintf ppf ",@ " in Format.fprintf ppf "@[<hv 2>[ " ; List.iter (fun elt -> Format.fprintf ppf "%a%a" pp_sep () pp_element elt) elements ; (match additional_items with | None -> () | Some {kind = Any} -> Format.fprintf ppf "%a,@ ..." pp_sep () | Some elt -> Format.fprintf ppf "%a,@ %a ..." pp_sep () pp_element elt) ; Format.fprintf ppf " ]@]" | Monomorphic_array (elt, {additional_items = None}) -> Format.fprintf ppf "[ %a ... ]" pp_element elt | Monomorphic_array (elt, {additional_items = Some {kind = Any}}) -> Format.fprintf ppf "@[<hv 2>[ %a ...,@ ... ]@]" pp_element elt | Monomorphic_array (elt, {additional_items = Some add_elt}) -> (* TODO: find a good way to print length *) Format.fprintf ppf "@[<hv 2>[ %a ...,@ %a ... ]@]" pp_element elt pp_element add_elt)))) and pp_object_contents ppf {properties; pattern_properties; additional_properties} = (* TODO: find a good way to print length / dependencies *) let pp_sep = let first = ref true in fun ppf () -> if !first then first := false else Format.fprintf ppf ",@ " in List.iter (fun (name, elt, req, _) -> Format.fprintf ppf "%a@[<hv 2>%a%s:@ %a@]" pp_sep () pp_string name (if req then "" else "?") pp_element elt) properties ; List.iter (fun (name, elt) -> Format.fprintf ppf "%a@[<hv 2>/%s/:@ %a@]" pp_sep () name pp_element elt) pattern_properties ; match additional_properties with | None -> () | Some {kind = Any} -> Format.fprintf ppf "%a..." pp_sep () | Some elt -> Format.fprintf ppf "%a@[<hv 2>*:@ %a@]" pp_sep () pp_element elt let pp ppf schema = Format.fprintf ppf "@[<v 0>" ; pp_element ppf schema.root ; Def_map.iter (fun path elt -> match pp_desc elt with | None -> Format.fprintf ppf "@,@[<hv 2>$%a:@ %a@]" pp_path path pp_element elt | Some pp_desc -> let stripped = {elt with title = None; description = None} in Format.fprintf ppf "@,@[<v 2>$%a:@,%a@,%a@]" pp_path path pp_desc () pp_element stripped) schema.definitions ; Id_map.iter (fun id elt -> match pp_desc elt with | None -> Format.fprintf ppf "@,@[<hv 2>#%s:@ %a@]" id pp_element {elt with id = None} | Some pp_desc -> let stripped = {elt with title = None; description = None; id = None} in Format.fprintf ppf "@,@[<v 2>#%s:@,%a@,%a@]" id pp_desc () pp_element stripped) schema.ids ; Format.fprintf ppf "@]" (*-- errors ----------------------------------------------------------------*) exception Cannot_parse of Json_query.path * exn exception Dangling_reference of Uri.t exception Bad_reference of string exception Unexpected of string * string exception Duplicate_definition of Json_query.path * element * element let rec print_error ?print_unknown ppf = function | Cannot_parse (path, exn) -> Format.fprintf ppf "@[<v 2>Schema parse error:@,At %a@,%a@]" (Json_query.print_path_as_json_path ~wildcards:true) path (print_error ?print_unknown) exn | Dangling_reference uri -> Format.fprintf ppf "Dangling reference %s" (Uri.to_string uri) | Bad_reference str -> Format.fprintf ppf "Illegal reference notation %s" str | Unexpected (unex, ex) -> Format.fprintf ppf "Unexpected %s instead of %s" unex ex | Duplicate_definition (name, elt, defelt) -> Format.fprintf ppf "@[<v 2>Duplicate definition %a@,\ To be inserted:@,\ \ @[<v 0>%a@]@,\ Already present:@,\ \ @[<v 0>%a@]@]" (Json_query.print_path_as_json_pointer ~wildcards:false) name pp_element elt pp_element defelt | exn -> Json_query.print_error ?print_unknown ppf exn (*-- internal definition table handling ------------------------------------*) let find_definition name defs = Def_map.find name defs let definition_exists name defs = Def_map.mem name defs let insert_definition name elt (defs : element Def_map.t) : element Def_map.t = Def_map.update name (function | None -> Some elt | Some {kind = Dummy} -> Some elt | Some defelt -> if not (eq_element elt defelt) then raise (Duplicate_definition (name, elt, defelt)) ; Some elt) defs module Make (Repr : Json_repr.Repr) = struct module Query = Json_query.Make (Repr) open Query (*-- printer ---------------------------------------------------------------*) let to_json schema = (* functional JSON building combinators *) let obj l = Repr.repr (`O l) in let set_always f v rest = (f, Repr.repr v) :: rest in let set_if_some f v cb rest = match v with None -> rest | Some v -> (f, Repr.repr (cb v)) :: rest in let set_if_cons f v cb rest = match v with [] -> rest | v -> (f, Repr.repr (cb v)) :: rest in let set_if_neq f v v' cb rest = if v <> v' then (f, Repr.repr (cb v)) :: rest else rest in let set_multiple xs rest = List.rev_append xs rest in (* recursive encoder *) let rec format_element {title; description; default; enum; kind; format} = set_if_some "title" title (fun s -> `String s) @@ set_if_some "description" description (fun s -> `String s) @@ (fun rest -> match kind with | Object specs -> let required = List.fold_left (fun r (n, _, p, _) -> if p then Repr.repr (`String n) :: r else r) [] specs.properties in let properties = List.map (fun (n, elt, _, _) -> (n, obj (format_element elt))) specs.properties in set_always "type" (`String "object") @@ set_if_cons "properties" properties (fun l -> `O l) @@ set_if_cons "required" required (fun l -> `A l) @@ set_if_cons "patternProperties" specs.pattern_properties (fun fs -> `O (List.map (fun (n, elt) -> (n, obj (format_element elt))) fs)) @@ set_if_neq "additionalProperties" specs.additional_properties (Some (element Any)) (function | None -> `Bool false | Some elt -> `O (format_element elt)) @@ set_if_neq "minProperties" specs.min_properties 0 (fun i -> `Float (float i)) @@ set_if_some "maxProperties" specs.max_properties (fun i -> `Float (float i)) @@ set_if_cons "schemaDependencies" specs.schema_dependencies (fun fs -> `O (List.map (fun (n, elt) -> (n, obj (format_element elt))) fs)) @@ set_if_cons "propertyDependencies" specs.property_dependencies (fun fs -> let property_dependencies = let strings ls = List.map (fun s -> Repr.repr (`String s)) ls in List.map (fun (n, ls) -> (n, Repr.repr (`A (strings ls)))) fs in `O property_dependencies) @@ rest | Array (elts, specs) -> set_always "type" (`String "array") @@ set_always "items" (`A (List.map (fun elt -> obj (format_element elt)) elts)) @@ set_if_neq "minItems" specs.min_items 0 (fun i -> `Float (float i)) @@ set_if_some "maxItems" specs.max_items (fun i -> `Float (float i)) @@ set_if_neq "uniqueItems" specs.unique_items false (fun b -> `Bool b) @@ set_if_neq "additionalItems" specs.additional_items (Some (element Any)) (function | None -> `Bool false | Some elt -> `O (format_element elt)) @@ rest | Monomorphic_array (elt, {min_items; max_items; unique_items}) -> set_always "type" (`String "array") @@ set_always "items" (`O (format_element elt)) @@ set_if_neq "minItems" min_items 0 (fun i -> `Float (float i)) @@ set_if_some "maxItems" max_items (fun i -> `Float (float i)) @@ set_if_neq "uniqueItems" unique_items false (fun b -> `Bool b) @@ rest | Combine (c, elts) -> let combinator = function | Any_of -> "anyOf" | One_of -> "oneOf" | All_of -> "allOf" | Not -> "not" in set_always (combinator c) (`A (List.map (fun elt -> obj (format_element elt)) elts)) @@ rest | Def_ref path -> set_always "$ref" (`String ("#" ^ Json_query.json_pointer_of_path path)) @@ rest | Id_ref name -> set_always "$ref" (`String ("#" ^ name)) @@ rest | Ext_ref uri -> set_always "$ref" (`String (Uri.to_string uri)) @@ rest | Integer specs -> set_always "type" (`String "integer") @@ set_if_some "multipleOf" specs.multiple_of (fun v -> `Float v) @@ set_multiple (match specs.minimum with | None -> [] | Some (v, `Inclusive) -> [("minimum", Repr.repr (`Float v))] | Some (v, `Exclusive) -> [ ("exclusiveMinimum", Repr.repr (`Bool true)); ("minimum", Repr.repr (`Float v)); ]) @@ set_multiple (match specs.maximum with | None -> [] | Some (v, `Inclusive) -> [("maximum", Repr.repr (`Float v))] | Some (v, `Exclusive) -> [ ("exclusiveMaximum", Repr.repr (`Bool true)); ("maximum", Repr.repr (`Float v)); ]) @@ rest | Number specs -> set_always "type" (`String "number") @@ set_if_some "multipleOf" specs.multiple_of (fun v -> `Float v) @@ set_multiple (match specs.minimum with | None -> [] | Some (v, `Inclusive) -> [("minimum", Repr.repr (`Float v))] | Some (v, `Exclusive) -> [ ("exclusiveMinimum", Repr.repr (`Bool true)); ("minimum", Repr.repr (`Float v)); ]) @@ set_multiple (match specs.maximum with | None -> [] | Some (v, `Inclusive) -> [("maximum", Repr.repr (`Float v))] | Some (v, `Exclusive) -> [ ("exclusiveMaximum", Repr.repr (`Bool true)); ("maximum", Repr.repr (`Float v)); ]) @@ rest | String {pattern; min_length; max_length; str_format} -> set_always "type" (`String "string") @@ set_if_neq "minLength" min_length 0 (fun i -> `Float (float i)) @@ set_if_some "maxLength" max_length (fun i -> `Float (float i)) @@ set_if_some "pattern" pattern (fun s -> `String s) @@ set_if_some "format" str_format (fun s -> `String s) @@ rest | Boolean -> set_always "type" (`String "boolean") @@ rest | Null -> set_always "type" (`String "null") @@ rest | Dummy -> invalid_arg "Json_schema.to_json: remaining dummy element" | Any -> set_multiple [] @@ rest) @@ set_if_some "default" default (fun j -> Repr.view (Json_repr.any_to_repr (module Repr) j)) @@ set_if_some "enum" enum (fun js -> `A (List.map (Json_repr.any_to_repr (module Repr)) js)) @@ set_if_some "format" format (fun s -> `String s) @@ [] in Def_map.fold (fun n elt acc -> insert n (obj (format_element elt)) acc) schema.definitions (obj (set_always "$schema" (`String version) @@ format_element schema.root)) let unexpected kind expected = let kind = match kind with | `O [] -> "empty object" | `A [] -> "empty array" | `O _ -> "object" | `A _ -> "array" | `Null -> "null" | `String "" -> "empty string" | `String _ -> "string" | `Float _ -> "number" | `Bool _ -> "boolean" in Cannot_parse ([], Unexpected (kind, expected)) (*-- parser ----------------------------------------------------------------*) let at_path p = function | Cannot_parse (l, err) -> Cannot_parse (List.append p l, err) | exn -> exn let at_field n = at_path [`Field n] let at_index i = at_path [`Index i] let of_json ?(definitions_path = "/definitions/") json = (* parser combinators *) let opt_field obj n = match Repr.view obj with | `O ls -> ( try Some (List.assoc n ls) with Not_found -> None) | _ -> None in let opt_field_view obj n = match Repr.view obj with | `O ls -> ( try Some (Repr.view (List.assoc n ls)) with Not_found -> None) | _ -> None in let opt_string_field obj n = match opt_field_view obj n with | Some (`String s) -> Some s | Some k -> raise (at_field n @@ unexpected k "string") | None -> None in let opt_bool_field def obj n = match opt_field_view obj n with | Some (`Bool b) -> b | Some k -> raise (at_field n @@ unexpected k "bool") | None -> def in let opt_int_field obj n = match opt_field_view obj n with | Some (`Float f) when fst (modf f) = 0. && f <= 2. ** 53. && f >= -2. ** 53. -> Some f | Some k -> raise (at_field n @@ unexpected k "integer") | None -> None in let opt_length_field obj n = match opt_field_view obj n with | Some (`Float f) when fst (modf f) = 0. && f <= 2. ** 30. && f >= 0. -> Some (int_of_float f) | Some k -> raise (at_field n @@ unexpected k "length") | None -> None in let opt_float_field obj n = match opt_field_view obj n with | Some (`Float f) -> Some f | Some k -> raise (at_field n @@ unexpected k "number") | None -> None in let opt_array_field obj n = match opt_field_view obj n with | Some (`A s) -> Some s | Some k -> raise (at_field n @@ unexpected k "array") | None -> None in let opt_uri_field obj n = match opt_string_field obj n with | None -> None | Some uri -> ( match Uri.canonicalize (Uri.of_string uri) with | exception _ -> raise (Cannot_parse ([], Bad_reference (uri ^ " is not a valid URI"))) | uri -> Some uri) in (* local resolution of definitions *) let schema_source = match opt_uri_field json "id" with | Some uri -> Uri.with_fragment uri None | None -> Uri.empty in let collected_definitions : element Def_map.t ref = ref Def_map.empty in let collected_id_defs = ref Id_map.empty in let collected_id_refs = ref Id_set.empty in let rec collect_definition : Uri.t -> element_kind = fun uri -> match (Uri.host uri, Uri.fragment uri) with | Some _ (* Actually means: any of host, user or port is defined. *), _ -> Ext_ref uri | None, None -> raise (Cannot_parse ([], Bad_reference (Uri.to_string uri ^ " has no fragment"))) | None, Some fragment when not (String.contains fragment '/') -> collected_id_refs := Id_set.add fragment !collected_id_refs ; Id_ref fragment | None, Some fragment -> ( let path = try Json_query.path_of_json_pointer ~wildcards:false fragment with err -> raise (Cannot_parse ([], err)) in try let raw = query path json in if not (definition_exists path !collected_definitions) then ( (* dummy insertion so we don't recurse and we support cycles *) collected_definitions := insert_definition path (element Dummy) !collected_definitions ; let elt = try parse_element schema_source raw with err -> raise (at_path path err) in (* actual insertion *) collected_definitions := insert_definition path elt !collected_definitions) ; Def_ref path with Not_found -> raise (Cannot_parse ([], Dangling_reference uri))) (* recursive parser *) and parse_element : Uri.t -> Repr.value -> element = fun source json -> let id = opt_uri_field json "id" in let id, source = match id with | None -> (None, source) | Some uri -> let uri = Uri.canonicalize (Uri.resolve "http" source uri) in (Uri.fragment uri, Uri.with_fragment uri None) in (* We don't support inlined schemas, so we just drop elements with external sources and replace them with external references. *) if source <> schema_source then element (Ext_ref (Uri.with_fragment source id)) else let id = match id with | None -> None | Some id when String.contains id '/' -> raise (at_field "id" @@ Cannot_parse ([], Bad_reference (id ^ " is not a valid ID")) ) | Some id -> Some id in (* We parse the various element syntaxes and combine them afterwards. *) (* 1. An element with a known type field and associated fields. *) let as_kind = match opt_field_view json "type" with | Some (`String name) -> Some (element (parse_element_kind source json name)) | Some (`A [] as k) -> raise (at_field "type" @@ unexpected k "type, type array or operator") | Some (`A l) -> let rec items i acc = function | [] -> let kind = Combine (Any_of, List.rev acc) in Some (element kind) | `String name :: tl -> let kind = parse_element_kind source json name in let case = element kind in items (succ i) (case :: acc) tl | k :: _ -> raise (at_field "type" @@ at_index i @@ unexpected k "type") in items 0 [] (List.map Repr.view l) | Some k -> raise (at_field "type" @@ unexpected k "type, type array or operator") | None -> None in (* 2. A reference *) let as_ref = match opt_uri_field json "$ref" with | Some uri -> let path = collect_definition uri in Some (element path) | None -> None in (* 3. Combined schemas *) let as_nary name combinator others = let build = function | [] -> None (* not found and no auxiliary case *) | [case] -> Some case (* one case -> simplify *) | cases -> (* several cases build the combination node with empty options *) let kind = Combine (combinator, cases) in Some (element kind) in let items = match opt_field_view json name with | Some (`A (_ :: _ as cases)) (* list of schemas *) -> let rec items i acc = function | elt :: tl -> let elt = try parse_element source elt with err -> raise (at_field name @@ at_index i @@ err) in items (succ i) (elt :: acc) tl | [] -> List.rev acc in items 0 [] cases | None -> [] | Some k -> raise (at_field name @@ unexpected k "a list of elements") in build (List.rev_append others items) in (* 4. Negated schema *) let as_not = match opt_field_view json "not" with | None -> None | Some elt -> let elt = try parse_element source (Repr.repr elt) with err -> raise (at_field "not" err) in let kind = Combine (Not, [elt]) in Some (element kind) in (* parse optional fields *) let title = opt_string_field json "title" in let description = opt_string_field json "description" in let default = match opt_field json "default" with | Some v -> Some (Json_repr.repr_to_any (module Repr) v) | None -> None in let enum = Option.map (fun v -> List.map (Json_repr.repr_to_any (module Repr)) v) (opt_array_field json "enum") in let format = opt_string_field json "format" in (* TODO: check format ? *) (* combine all specifications under a big conjunction *) let as_one_of = as_nary "oneOf" One_of [] in let as_any_of = as_nary "anyOf" Any_of [] in let cases = let ( @? ) o xs = match o with None -> xs | Some x -> x :: xs in (* Note: building this reversed so we can use [rev_append] *) as_any_of @? as_one_of @? as_not @? as_ref @? as_kind @? [] in let kind = match as_nary "allOf" All_of cases with | None -> Any (* no type, ref or logical combination found *) | Some {kind} -> kind in (* add optional fields *) {title; description; default; format; kind; enum; id} and parse_element_kind source json name = let integer_specs json = let multiple_of = opt_int_field json "multipleOf" in let minimum = if opt_bool_field false json "exclusiveMinimum" then match opt_int_field json "minimum" with | None -> let err = "minimum field required when exclusiveMinimum is true" in raise (Failure err) | Some v -> Some (v, `Inclusive) else match opt_int_field json "minimum" with | None -> None | Some v -> Some (v, `Exclusive) in let maximum = if opt_bool_field false json "exclusiveMaximum" then match opt_int_field json "maximum" with | None -> let err = "maximum field required when exclusiveMaximum is true" in raise (Failure err) | Some v -> Some (v, `Inclusive) else match opt_int_field json "maximum" with | None -> None | Some v -> Some (v, `Exclusive) in {multiple_of; minimum; maximum} in let numeric_specs json = let multiple_of = opt_float_field json "multipleOf" in let minimum = if opt_bool_field false json "exclusiveMinimum" then match opt_float_field json "minimum" with | None -> let err = "minimum field required when exclusiveMinimum is true" in raise (Failure err) | Some v -> Some (v, `Inclusive) else match opt_float_field json "minimum" with | None -> None | Some v -> Some (v, `Exclusive) in let maximum = if opt_bool_field false json "exclusiveMaximum" then match opt_float_field json "maximum" with | None -> let err = "maximum field required when exclusiveMaximum is true" in raise (Failure err) | Some v -> Some (v, `Inclusive) else match opt_float_field json "maximum" with | None -> None | Some v -> Some (v, `Exclusive) in {multiple_of; minimum; maximum} in match name with | "integer" -> Integer (integer_specs json) | "number" -> Number (numeric_specs json) | "boolean" -> Boolean | "null" -> Null | "string" -> let specs = let pattern = opt_string_field json "pattern" in let str_format = opt_string_field json "format" in let min_length = opt_length_field json "minLength" in let max_length = opt_length_field json "maxLength" in let min_length = match min_length with None -> 0 | Some l -> l in {pattern; min_length; max_length; str_format} in String specs | "array" -> ( let specs = let unique_items = opt_bool_field false json "uniqueItems" in let min_items = opt_length_field json "minItems" in let max_items = opt_length_field json "maxItems" in let min_items = match min_items with None -> 0 | Some l -> l in match opt_field_view json "additionalItems" with | Some (`Bool true) -> { min_items; max_items; unique_items; additional_items = Some (element Any); } | None | Some (`Bool false) -> {min_items; max_items; unique_items; additional_items = None} | Some elt -> let elt = try parse_element source (Repr.repr elt) with err -> raise (at_field "additionalItems" err) in { min_items; max_items; unique_items; additional_items = Some elt; } in match opt_field_view json "items" with | Some (`A elts) -> let rec elements i acc = function | [] -> Array (List.rev acc, specs) | elt :: tl -> let elt = try parse_element source elt with err -> raise (at_field "items" @@ at_index i err) in elements (succ i) (elt :: acc) tl in elements 0 [] elts | Some elt -> let elt = try parse_element source (Repr.repr elt) with err -> raise (at_field "items" err) in Monomorphic_array (elt, specs) | None -> Monomorphic_array (element Any, specs)) | "object" -> let required = match opt_array_field json "required" with | None -> [] | Some l -> let rec items i acc = function | `String s :: tl -> items (succ i) (s :: acc) tl | [] -> List.rev acc | k :: _ -> raise (at_field "required" @@ at_index i @@ unexpected k "string") in items 0 [] (List.map Repr.view l) in let properties = match opt_field_view json "properties" with | Some (`O props) -> let rec items acc = function | [] -> List.rev acc | (n, elt) :: tl -> let elt = try parse_element source elt with err -> raise (at_field "properties" @@ at_field n @@ err) in let req = List.mem n required in items ((n, elt, req, None) :: acc) tl (* XXX: fixme *) in items [] props | None -> [] | Some k -> raise (at_field "properties" @@ unexpected k "object") in let additional_properties = match opt_field_view json "additionalProperties" with | Some (`Bool false) -> None | None | Some (`Bool true) -> Some (element Any) | Some elt -> let elt = try parse_element source (Repr.repr elt) with err -> raise (at_field "additionalProperties" err) in Some elt in let property_dependencies = match opt_field_view json "propertyDependencies" with | None -> [] | Some (`O l) -> let rec sets sacc = function | (n, `A l) :: tl -> let rec strings j acc = function | [] -> sets ((n, List.rev acc) :: sacc) tl | `String s :: tl -> strings (succ j) (s :: acc) tl | k :: _ -> raise (at_field "propertyDependencies" @@ at_field n @@ at_index j @@ unexpected k "string") in strings 0 [] (List.map Repr.view l) | (n, k) :: _ -> raise (at_field "propertyDependencies" @@ at_field n @@ unexpected k "string array") | [] -> List.rev sacc in sets [] (List.map (fun (n, v) -> (n, Repr.view v)) l) | Some k -> raise (at_field "propertyDependencies" @@ unexpected k "object") in let parse_element_assoc field = match opt_field_view json field with | None -> [] | Some (`O props) -> let rec items acc = function | [] -> List.rev acc | (n, elt) :: tl -> let elt = try parse_element source elt with err -> raise (at_field field @@ at_field n err) in items ((n, elt) :: acc) tl in items [] props | Some k -> raise (at_field field @@ unexpected k "object") in let pattern_properties = parse_element_assoc "patternProperties" in let schema_dependencies = parse_element_assoc "schemaDependencies" in let min_properties = match opt_length_field json "minProperties" with | None -> 0 | Some l -> l in let max_properties = opt_length_field json "maxProperties" in Object { properties; pattern_properties; additional_properties; min_properties; max_properties; schema_dependencies; property_dependencies; } | n -> raise (Cannot_parse ([], Unexpected (n, "a known type"))) in (* parse recursively from the root *) let root = parse_element Uri.empty json in (* force the addition of everything inside /definitions *) (match Repr.view (query [`Field "definitions"] json) with | `O all -> List.iter (fun (n, _) -> let uri = Uri.of_string ("#" ^ definitions_path ^ n) in ignore (collect_definition uri)) all | _ -> () | exception Not_found -> ()) ; (* check the domain of IDs *) Id_set.iter (fun id -> if not (Id_map.mem id !collected_id_defs) then raise (Cannot_parse ([], Dangling_reference Uri.(with_fragment empty (Some id))))) !collected_id_refs ; let ids = !collected_id_defs in let source = schema_source in let world = [] in let definitions = !collected_definitions in {root; definitions; source; ids; world} (*-- creation and update ---------------------------------------------------*) (* Checks that all local refs and ids are defined *) let check_definitions root definitions = let collected_id_defs = ref Id_map.empty in let collected_id_refs = ref Id_set.empty in let rec check ({kind; id} as elt) = (match id with | None -> () | Some id -> collected_id_defs := Id_map.add id elt !collected_id_defs) ; match kind with | Object { properties; pattern_properties; additional_properties; schema_dependencies; } -> ( List.iter (fun (_, e, _, _) -> check e) properties ; List.iter (fun (_, e) -> check e) pattern_properties ; List.iter (fun (_, e) -> check e) schema_dependencies ; match additional_properties with Some e -> check e | None -> ()) | Array (es, {additional_items}) -> ( List.iter check es ; match additional_items with Some e -> check e | None -> ()) | Monomorphic_array (e, {additional_items}) -> ( check e ; match additional_items with Some e -> check e | None -> ()) | Combine (_, es) -> List.iter check es | Def_ref path -> if not (definition_exists path definitions) then let path = Json_query.json_pointer_of_path path in raise (Dangling_reference (Uri.(with_fragment empty) (Some path))) | Id_ref id -> collected_id_refs := Id_set.add id !collected_id_refs | Ext_ref _ | String _ | Integer _ | Number _ | Boolean | Null | Any | Dummy -> () in (* check the root and definitions *) check root ; Def_map.iter (fun _ root -> check root) definitions ; (* check the domain of IDs *) Id_set.iter (fun id -> if not (Id_map.mem id !collected_id_defs) then raise (Dangling_reference Uri.(with_fragment empty (Some id)))) !collected_id_refs ; !collected_id_defs let create root = let ids = check_definitions root Def_map.empty in {root; definitions = Def_map.empty; world = []; ids; source = Uri.empty} let root {root} = root let update root sch = let ids = check_definitions root sch.definitions in {sch with root; ids} let any = create (element Any) let self = { root = element (Ext_ref (Uri.of_string version)); definitions = Def_map.empty; ids = Id_map.empty; world = []; source = Uri.empty; } (* remove unused definitions from the schema *) let simplify schema = let res = ref Def_map.empty (* collected definitions *) in let rec collect {kind} = match kind with | Object { properties; pattern_properties; additional_properties; schema_dependencies; } -> ( List.iter (fun (_, e, _, _) -> collect e) properties ; List.iter (fun (_, e) -> collect e) pattern_properties ; List.iter (fun (_, e) -> collect e) schema_dependencies ; match additional_properties with Some e -> collect e | None -> ()) | Array (es, {additional_items}) -> ( List.iter collect es ; match additional_items with Some e -> collect e | None -> ()) | Monomorphic_array (e, {additional_items}) -> ( collect e ; match additional_items with Some e -> collect e | None -> ()) | Combine (_, es) -> List.iter collect es | Def_ref path -> let def = find_definition path schema.definitions in res := insert_definition path def !res | Ext_ref _ | Id_ref _ | String _ | Integer _ | Number _ | Boolean | Null | Any | Dummy -> () in collect schema.root ; {schema with definitions = !res} let definition_path_of_name ?(definitions_path = "/definitions/") name = Json_query.path_of_json_pointer ~wildcards:false @@ match name.[0] with | exception _ -> raise (Bad_reference name) | '/' -> name | _ -> definitions_path ^ name let find_definition ?definitions_path name schema = let path = definition_path_of_name ?definitions_path name in find_definition path schema.definitions let definition_ref ?definitions_path name = let path = definition_path_of_name ?definitions_path name in element (Def_ref path) let definition_exists ?definitions_path name schema = let path = definition_path_of_name ?definitions_path name in definition_exists path schema.definitions let add_definition ?definitions_path name elt schema = let path = definition_path_of_name ?definitions_path name in (* check inside def *) let definitions = insert_definition path elt schema.definitions in ({schema with definitions}, element (Def_ref path)) let merge_definitions (sa, sb) = let definitions = Def_map.merge (fun name x y -> match (x, y) with | None, None -> None | Some x, None | None, Some x -> Some x | Some da, Some db -> if da.kind = Dummy || db.kind = Dummy || eq_element da db then Some da else raise (Duplicate_definition (name, da, db))) sa.definitions sb.definitions in ({sa with definitions}, {sb with definitions}) let combine op schemas = let rec combine sacc eacc = function | [] -> update (element (Combine (op, eacc))) sacc | s :: ss -> let sacc, s = merge_definitions (sacc, s) in combine sacc (s.root :: eacc) ss in combine any [] schemas let is_nullable {ids; definitions; root} = let rec nullable {kind} = match kind with | Null | Any -> true | Object _ | Array _ | Monomorphic_array _ | Ext_ref _ | String _ | Integer _ | Number _ | Boolean -> false | Combine (Not, [elt]) -> not (nullable elt) | Combine (All_of, elts) -> List.for_all nullable elts | Combine ((Any_of | One_of), elts) -> List.exists nullable elts | Def_ref path -> nullable (Def_map.find path definitions) | Id_ref id -> nullable (Id_map.find id ids) | Combine (Not, _) | Dummy -> assert false in nullable root (*-- default specs ---------------------------------------------------------*) let array_specs = { min_items = 0; max_items = None; unique_items = false; additional_items = None; } let object_specs = { properties = []; pattern_properties = []; additional_properties = Some (element Any); min_properties = 0; max_properties = None; schema_dependencies = []; property_dependencies = []; } let string_specs = {pattern = None; min_length = 0; max_length = None; str_format = None} let numeric_specs = {multiple_of = None; minimum = None; maximum = None} end include Make (Json_repr.Ezjsonm)
sectionYPositions = computeSectionYPositions($el), 10)"
x-init="setTimeout(() => sectionYPositions = computeSectionYPositions($el), 10)"
>