package jsonschema

  1. Overview
  2. Docs
JSON Schema validator for OCaml

Install

Dune Dependency

Authors

Maintainers

Sources

jsonschema-0.1.0.tbz
sha256=21a3ff475202fe598f639c4dc5b03355f169d9d77b4f17ecd5a1aca9b475b93e
sha512=ce90853ca15dfe5e52289f4fb5ce92275dcde931114312f77fd6e817c0727708ebb006546a6b5ce05332a9c1a49c07d7c6a8131e54a02085cc999a6d823a06e8

doc/src/jsonschema/validation_error.ml.html

Source file validation_error.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
open Error

let to_string err =
  Printf.sprintf "jsonschema validation failed with %s\n  at '%s': %s"
    err.schema_url
    (Json_pointer.of_tokens
       (List.map
          (function Prop s -> s | Item i -> string_of_int i)
          err.instance_location.tokens))
    (match err.kind with
    | Type _ -> "type mismatch"
    | Required { want } ->
        Printf.sprintf "missing properties %s" (String.concat ", " want)
    | All_of -> "allOf validation failed"
    | Schema { url } -> Printf.sprintf "schema error: %s" url
    | Unevaluated_items { got } -> Printf.sprintf "%d unevaluated items" got
    | _ -> "validation error")

let to_string_verbose err = to_string err (* TODO: implement verbose version *)
let primary_error err = err.kind

let rec all_errors err =
  (err.instance_location, err.kind) :: List.concat_map all_errors err.causes

let is_type_error err = match err.kind with Type _ -> true | _ -> false

let is_required_error err =
  match err.kind with Required _ -> true | _ -> false

let is_format_error err = match err.kind with Format _ -> true | _ -> false

let missing_properties err =
  match err.kind with Required { want } -> want | _ -> []

let type_mismatches err =
  let rec collect err acc =
    let acc' =
      match err.kind with
      | Type { got; want } ->
          (err.instance_location, Types.type_of got, want) :: acc
      | _ -> acc
    in
    List.fold_left (fun acc e -> collect e acc) acc' err.causes
  in
  collect err []
OCaml

Innovation. Community. Security.