package mcp-kit

  1. Overview
  2. Docs

Source file kw_type.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
(** Keywords: type, enum, const (spec §6.1 — any instance type). *)

open Jsonschema_core

(* ── type ── *)

let is_type_draft04 typ j =
  match (typ, j) with
  | "integer", (`Int _ | `Intlit _) -> true
  | "integer", _ -> false
  | _ -> Instance.is_type typ j

let is_type_draft03 typ j = typ = "any" || is_type_draft04 typ j

let compile_type_with is_type : Compiler.keyword_compiler =
  {
    keyword = "type";
    compile =
      (fun ctx value ->
        match value with
        | `String typ ->
            Some
              (fun instance path ->
                if is_type typ instance then []
                else
                  [
                    Error.make ~instance_path:path ~schema_path:ctx.schema_path
                      ~keyword:"type"
                      (Printf.sprintf "expected %s" typ);
                  ])
        | `List types ->
            let type_strings =
              List.filter_map (function `String s -> Some s | _ -> None) types
            in
            Some
              (fun instance path ->
                if List.exists (fun typ -> is_type typ instance) type_strings
                then []
                else
                  [
                    Error.make ~instance_path:path ~schema_path:ctx.schema_path
                      ~keyword:"type" "type mismatch";
                  ])
        | _ -> None);
  }

let compile_type = compile_type_with Instance.is_type
let compile_draft04_type = compile_type_with is_type_draft04

let compile_draft03_type : Compiler.keyword_compiler =
  {
    keyword = "type";
    compile =
      (fun ctx value ->
        let type_error path =
          Error.make ~instance_path:path ~schema_path:ctx.schema_path
            ~keyword:"type" "type mismatch"
        in
        let schema_choice schema =
          let sub_v = ctx.compile_sub schema in
          fun instance path -> sub_v instance path = []
        in
        match value with
        | `String typ ->
            Some
              (fun instance path ->
                if is_type_draft03 typ instance then [] else [ type_error path ])
        | `Assoc _ ->
            let matches = schema_choice value in
            Some
              (fun instance path ->
                if matches instance path then [] else [ type_error path ])
        | `List types ->
            let choices =
              List.filter_map
                (function
                  | `String typ ->
                      Some (fun instance _path -> is_type_draft03 typ instance)
                  | `Assoc _ as schema -> Some (schema_choice schema)
                  | _ -> None)
                types
            in
            Some
              (fun instance path ->
                if List.exists (fun matches -> matches instance path) choices
                then []
                else [ type_error path ])
        | _ -> None);
  }

let compile_disallow : Compiler.keyword_compiler =
  {
    keyword = "disallow";
    compile =
      (fun ctx value ->
        let type_choice typ =
         fun instance _path -> is_type_draft03 typ instance
        in
        let schema_choice schema =
          let sub_v = ctx.compile_sub schema in
          fun instance path -> sub_v instance path = []
        in
        let parse_choice = function
          | `String typ -> Some (type_choice typ)
          | `Assoc _ as schema -> Some (schema_choice schema)
          | _ -> None
        in
        let choices =
          match value with
          | `List values -> List.filter_map parse_choice values
          | _ -> List.filter_map parse_choice [ value ]
        in
        if choices = [] then None
        else
          Some
            (fun instance path ->
              if List.exists (fun matches -> matches instance path) choices then
                [
                  Error.make ~instance_path:path ~schema_path:ctx.schema_path
                    ~keyword:"disallow"
                    "instance matches disallowed type or schema";
                ]
              else []));
  }

(* ── enum ── *)

let compile_enum : Compiler.keyword_compiler =
  {
    keyword = "enum";
    compile =
      (fun ctx value ->
        match value with
        | `List values ->
            Some
              (fun instance path ->
                if List.exists (fun v -> Instance.json_equal v instance) values
                then []
                else
                  [
                    Error.make ~instance_path:path ~schema_path:ctx.schema_path
                      ~keyword:"enum" "value not in enum";
                  ])
        | _ -> None);
  }

(* ── const ── *)

let compile_const : Compiler.keyword_compiler =
  {
    keyword = "const";
    compile =
      (fun ctx value ->
        Some
          (fun instance path ->
            if Instance.json_equal value instance then []
            else
              [
                Error.make ~instance_path:path ~schema_path:ctx.schema_path
                  ~keyword:"const" "value does not match const";
              ]));
  }

let compilers = [ compile_type; compile_enum; compile_const ]
let draft04_compilers = [ compile_draft04_type; compile_enum ]
let draft03_compilers = [ compile_draft03_type; compile_enum; compile_disallow ]