Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source
OCaml JSON Schema
A JSON Schema validator for OCaml supporting drafts 4, 6, 7, 2019-09, and 2020-12.
Installation
opam install jsonschema
Usage
High-level API
open Jsonschema
(* Validate JSON string against schema string *)
let schema = {|{"type": "object", "required": ["name"]}|}
let json = {|{"name": "John", "age": 30}|}
match validate_strings ~schema ~json with
| Ok () -> print_endline "Valid!"
| Error err -> print_endline (Validation_error.to_string err)
(* Validate from files *)
let json = Yojson.Basic.from_file "data.json" in
match validate_file ~schema:"schema.json" json with
| Ok () -> print_endline "Valid!"
| Error err -> print_endline (Validation_error.to_string err)
(* Create a reusable validator *)
match create_validator "schema.json" with
| Ok validator ->
let json = Yojson.Basic.from_file "data.json" in
(match validate validator json with
| Ok () -> print_endline "Valid!"
| Error err -> print_endline (Validation_error.to_string err))
| Error err -> pp_compile_error Format.std_formatter err
JSON Pointer
let json = `Assoc [("foo", `Assoc [("bar", `Int 42)])] in
match Json_pointer.of_string "/foo/bar" with
| Ok ptr ->
(match Json_pointer.lookup ptr json with
| Some (`Int n) -> Printf.printf "Found: %d\n" n
| _ -> print_endline "Not found")
| Error e -> Printf.printf "Invalid pointer: %s\n" e
Format Validators
The library includes validators for common formats:
email, idn_email
date, time, date_time
ipv4, ipv6
hostname, idn_hostname
uri, iri, uri_reference, iri_reference
uuid
json_pointer, relative_json_pointer
regex
Implementation Status
Test Coverage by Draft Version
Draft 4: 153/159 test groups (96%)
Draft 6: 209/231 test groups (90%)
Draft 7: 228/254 test groups (89%)
Draft 2019-09: 309/361 test groups (85%)
Draft 2020-12: 309/368 test groups (83%)
Core Features (Fully Implemented)
✅ Type validation (null, boolean, number, integer, string, array, object)