package mcp-kit

  1. Overview
  2. Docs

Source file kw_string.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
(** Keywords: maxLength, minLength, pattern (spec §6.3). *)

open Jsonschema_core

let compile_max_length : Compiler.keyword_compiler =
  {
    keyword = "maxLength";
    compile =
      (fun ctx value ->
        match Util.to_nonneg_int_bound value with
        | Some bound ->
            Some
              (fun instance path ->
                match instance with
                | `String s
                  when Util.actual_greater_than_bound (Instance.utf8_length s)
                         bound ->
                    [
                      Error.make ~instance_path:path
                        ~schema_path:ctx.schema_path ~keyword:"maxLength"
                        (Printf.sprintf "string too long (max %s)"
                           (Util.bound_to_string bound));
                    ]
                | _ -> [])
        | None -> None);
  }

let compile_min_length : Compiler.keyword_compiler =
  {
    keyword = "minLength";
    compile =
      (fun ctx value ->
        match Util.to_nonneg_int_bound value with
        | Some bound ->
            Some
              (fun instance path ->
                match instance with
                | `String s
                  when Util.actual_less_than_bound (Instance.utf8_length s)
                         bound ->
                    [
                      Error.make ~instance_path:path
                        ~schema_path:ctx.schema_path ~keyword:"minLength"
                        (Printf.sprintf "string too short (min %s)"
                           (Util.bound_to_string bound));
                    ]
                | _ -> [])
        | None -> None);
  }

let compile_pattern : Compiler.keyword_compiler =
  {
    keyword = "pattern";
    compile =
      (fun ctx value ->
        match value with
        | `String pat -> (
            match Regex.compile pat with
            | Ok re ->
                Some
                  (fun instance path ->
                    match instance with
                    | `String s ->
                        if Regex.matches re s then []
                        else
                          [
                            Error.make ~instance_path:path
                              ~schema_path:ctx.schema_path ~keyword:"pattern"
                              (Printf.sprintf "does not match pattern %s" pat);
                          ]
                    | _ -> [])
            | Error _ -> None)
        | _ -> None);
  }

let compilers = [ compile_max_length; compile_min_length; compile_pattern ]