package mcp-kit
Source file compiler.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(** Schema compiler — transforms JSON Schema into validator functions. *) type config = { keywords : keyword_compiler list; format_assertion : bool; id_keyword : string; allow_boolean_schemas : bool; ref_siblings_apply : bool; recursive_ref_enabled : bool; recursive_ref_target : Uri.t option; dynamic_ref_enabled : bool; dynamic_ref_targets : (string * Uri.t) list; select_config : config -> Yojson.Safe.t -> config; } and keyword_compiler = { keyword : string; compile : context -> Yojson.Safe.t -> Registry.validator option; } and context = { config : config; registry : Registry.t; base_uri : Uri.t; schema_path : Json_pointer.t; schema_obj : Yojson.Safe.t; compile_sub : ?schema_path:Json_pointer.t -> Yojson.Safe.t -> Registry.validator; } let select_same_config config _schema_json = config let default_config = { keywords = []; format_assertion = false; id_keyword = "$id"; allow_boolean_schemas = true; ref_siblings_apply = false; recursive_ref_enabled = false; recursive_ref_target = None; dynamic_ref_enabled = false; dynamic_ref_targets = []; select_config = select_same_config; } let dynamic_targets_cache_key targets = targets |> List.map (fun (name, uri) -> name ^ "=" ^ Uri.to_string uri) |> String.concat ";" let add_dynamic_resource_targets reg resource_uri targets = Registry.dynamic_anchors_for_resource reg resource_uri |> List.fold_left (fun acc (name, uri) -> if List.mem_assoc name acc then acc else acc @ [ (name, uri) ]) targets let dynamic_ref_plain_name ref_str = let uri = Uri.of_string ref_str in match Uri_util.fragment uri with | Some name when name <> "" && name.[0] <> '/' -> Some name | _ -> None let has_string_member name pairs = match List.assoc_opt name pairs with Some (`String _) -> true | _ -> false let should_select_config config schema_path pairs = schema_path = Json_pointer.empty || has_string_member config.id_keyword pairs let rec compile config reg ?(base_uri = Uri.empty) ?(schema_path = Json_pointer.empty) schema_json = match schema_json with | `Bool true -> fun _instance _path -> [] | `Bool false -> if config.allow_boolean_schemas then fun _instance path -> [ Error.make ~instance_path:path ~schema_path ~keyword:"false" "false schema always fails"; ] else fun _instance _path -> [] | `Assoc pairs -> let config = if should_select_config config schema_path pairs then config.select_config config schema_json else config in compile_obj config reg base_uri schema_path pairs schema_json | _ -> fun _instance _path -> [] and compile_obj config reg base_uri schema_path pairs schema_json = let has_local_id = List.mem_assoc config.id_keyword pairs in let local_base_uri = match List.assoc_opt config.id_keyword pairs with | Some (`String id_str) -> let id_uri = Uri.of_string id_str in let resolved = Uri_util.resolve ~base:base_uri id_uri in if Uri_util.fragment id_uri <> None && Uri.path id_uri = "" then base_uri else Uri_util.without_fragment resolved | _ -> base_uri in let recursive_ref_target = match List.assoc_opt "$recursiveAnchor" pairs with | Some (`Bool true) -> ( match config.recursive_ref_target with | Some _ as target -> target | None -> Some local_base_uri) | _ when has_local_id -> None | _ -> config.recursive_ref_target in let config = { config with recursive_ref_target } in let dynamic_ref_targets = if config.dynamic_ref_enabled then add_dynamic_resource_targets reg local_base_uri config.dynamic_ref_targets else config.dynamic_ref_targets in let config = { config with dynamic_ref_targets } in match List.assoc_opt "$ref" pairs with | Some (`String ref_str) when not config.ref_siblings_apply -> (* Draft-07 and earlier: $ref ignores sibling keywords. *) compile_ref config reg base_uri (Json_pointer.append schema_path "$ref") ref_str | Some (`String ref_str) -> (* Draft 2019-09 and later: $ref is an applicator and siblings also apply. *) let ref_v = compile_ref config reg local_base_uri (Json_pointer.append schema_path "$ref") ref_str in let sibling_v = compile_keywords config reg local_base_uri schema_path (List.filter (fun (key, _) -> key <> "$ref") pairs) schema_json in fun instance path -> ref_v instance path @ sibling_v instance path | _ -> ( match List.assoc_opt "$recursiveRef" pairs with | Some (`String ref_str) when config.recursive_ref_enabled -> let ref_v = match (config.recursive_ref_target, ref_str) with | Some target, "#" -> compile_resolved_ref config reg (Uri.with_fragment target (Some "")) (Json_pointer.append schema_path "$recursiveRef") ref_str | _ -> compile_ref config reg local_base_uri (Json_pointer.append schema_path "$recursiveRef") ref_str in let sibling_v = compile_keywords config reg local_base_uri schema_path (List.filter (fun (key, _) -> key <> "$recursiveRef") pairs) schema_json in fun instance path -> ref_v instance path @ sibling_v instance path | _ -> ( match List.assoc_opt "$dynamicRef" pairs with | Some (`String ref_str) when config.dynamic_ref_enabled -> let resolved = let ref_uri = Uri.of_string ref_str in Uri_util.resolve ~base:local_base_uri ref_uri in let ref_v = match dynamic_ref_plain_name ref_str with | Some name when Registry.is_dynamic_anchor_uri reg resolved -> ( match List.assoc_opt name config.dynamic_ref_targets with | Some target -> compile_resolved_ref config reg target (Json_pointer.append schema_path "$dynamicRef") ref_str | None -> compile_resolved_ref config reg resolved (Json_pointer.append schema_path "$dynamicRef") ref_str) | None -> compile_ref config reg local_base_uri (Json_pointer.append schema_path "$dynamicRef") ref_str | Some _ -> compile_ref config reg local_base_uri (Json_pointer.append schema_path "$dynamicRef") ref_str in let sibling_v = compile_keywords config reg local_base_uri schema_path (List.filter (fun (key, _) -> key <> "$dynamicRef") pairs) schema_json in fun instance path -> ref_v instance path @ sibling_v instance path | _ -> compile_keywords config reg local_base_uri schema_path pairs schema_json)) and compile_ref config reg base_uri schema_path ref_str = let ref_uri = Uri.of_string ref_str in let resolved = Uri_util.resolve ~base:base_uri ref_uri in compile_resolved_ref config reg resolved schema_path ref_str and compile_resolved_ref config reg resolved schema_path ref_str = let cache_uri = let with_recursive = match config.recursive_ref_target with | None -> resolved | Some target -> Uri.add_query_param' resolved ("__jsonschema_recursive_ref_target", Uri.to_string target) in match config.dynamic_ref_targets with | [] -> with_recursive | targets -> Uri.add_query_param' with_recursive ("__jsonschema_dynamic_ref_targets", dynamic_targets_cache_key targets) in match Registry.get_compiled reg cache_uri with | Some v -> v | None -> if Registry.is_compiling reg cache_uri then begin let proxy = ref (fun (_inst : Yojson.Safe.t) (_path : Json_pointer.t) -> []) in Registry.on_compiled reg cache_uri (fun v -> proxy := v); fun instance path -> !proxy instance path end else begin match Registry.resolve_with_base_and_path reg resolved with | Some (target_json, target_base, target_path) -> Registry.mark_compiling reg cache_uri; let v = compile config reg ~base_uri:target_base ~schema_path:target_path target_json in Registry.set_compiled reg cache_uri v; v | None -> fun _instance path -> [ Error.make ~instance_path:path ~schema_path ~keyword:"$ref" (Printf.sprintf "unresolved $ref: %s" ref_str); ] end and compile_keywords config reg base_uri schema_path pairs schema_json = let compile_sub ?(schema_path = schema_path) sub_json = compile config reg ~base_uri ~schema_path sub_json in let ctx = { config; registry = reg; base_uri; schema_path; schema_obj = schema_json; compile_sub; } in let validators = List.filter_map (fun (key, value) -> List.find_opt (fun kc -> kc.keyword = key) config.keywords |> Option.map (fun kc -> let keyword_path = Json_pointer.append schema_path key in let compile_sub ?(schema_path = keyword_path) sub_json = compile config reg ~base_uri ~schema_path sub_json in let ctx = { ctx with schema_path = keyword_path; compile_sub } in kc.compile ctx value) |> Option.join) pairs in match validators with | [] -> fun _instance _path -> [] | validators -> fun instance path -> List.concat_map (fun v -> v instance path) validators