package jsonschema

  1. Overview
  2. Docs

Module JsonschemaSource

JSON Schema validator supporting drafts 4, 6, 7, 2019-09, and 2020-12

Core Types

Sourcetype draft =
  1. | Draft4
  2. | Draft6
  3. | Draft7
  4. | Draft2019_09
  5. | Draft2020_12

Supported draft versions

Sourcetype schema

A compiled JSON schema

Sourcetype instance_token =
  1. | Prop of string
  2. | Item of int

Instance location token

Sourcetype instance_location = {
  1. tokens : instance_token list;
}

Instance location in the validated document

Sourcetype type_set

Set of JSON types

Sourcetype error_kind =
  1. | Group
  2. | Schema of {
    1. url : string;
    }
  3. | Content_schema
  4. | Property_name of {
    1. prop : string;
    }
  5. | Reference of {
    1. kw : string;
    2. url : string;
    }
  6. | Ref_cycle of {
    1. url : string;
    2. kw_loc1 : string;
    3. kw_loc2 : string;
    }
  7. | False_schema
  8. | Type of {
    1. got : Yojson.Basic.t;
    2. want : type_set;
    }
  9. | Enum of {
    1. want : Yojson.Basic.t list;
    }
  10. | Const of {
    1. want : Yojson.Basic.t;
    }
  11. | Format of {
    1. got : Yojson.Basic.t;
    2. want : string;
    3. err : exn;
    }
  12. | Min_properties of {
    1. got : int;
    2. want : int;
    }
  13. | Max_properties of {
    1. got : int;
    2. want : int;
    }
  14. | Additional_properties of {
    1. got : string list;
    }
  15. | Unevaluated_properties of {
    1. got : string list;
    }
  16. | Unevaluated_items of {
    1. got : int;
    }
  17. | Required of {
    1. want : string list;
    }
  18. | Dependency of {
    1. prop : string;
    2. missing : string list;
    }
  19. | Dependent_required of {
    1. prop : string;
    2. missing : string list;
    }
  20. | Min_items of {
    1. got : int;
    2. want : int;
    }
  21. | Max_items of {
    1. got : int;
    2. want : int;
    }
  22. | Contains
  23. | Min_contains of {
    1. got : int list;
    2. want : int;
    }
  24. | Max_contains of {
    1. got : int list;
    2. want : int;
    }
  25. | Unique_items of {
    1. got : int * int;
    }
  26. | Additional_items of {
    1. got : int;
    }
  27. | Min_length of {
    1. got : int;
    2. want : int;
    }
  28. | Max_length of {
    1. got : int;
    2. want : int;
    }
  29. | Pattern of {
    1. got : string;
    2. want : string;
    }
  30. | Content_encoding of {
    1. want : string;
    2. err : exn;
    }
  31. | Content_media_type of {
    1. got : bytes;
    2. want : string;
    3. err : exn;
    }
  32. | Minimum of {
    1. got : float;
    2. want : float;
    }
  33. | Maximum of {
    1. got : float;
    2. want : float;
    }
  34. | Exclusive_minimum of {
    1. got : float;
    2. want : float;
    }
  35. | Exclusive_maximum of {
    1. got : float;
    2. want : float;
    }
  36. | Multiple_of of {
    1. got : float;
    2. want : float;
    }
  37. | Not
  38. | All_of
  39. | Any_of
  40. | One_of of (int * int) option

Validation error kinds

Sourcetype validation_error = {
  1. schema_url : string;
  2. instance_location : instance_location;
  3. kind : error_kind;
  4. causes : validation_error list;
}

Validation error

Sourcetype compile_error =
  1. | Parse_url_error of {
    1. url : string;
    2. src : exn;
    }
  2. | Load_url_error of {
    1. url : string;
    2. src : exn;
    }
  3. | Unsupported_url_scheme of {
    1. url : string;
    }
  4. | Invalid_meta_schema_url of {
    1. url : string;
    2. src : exn;
    }
  5. | Unsupported_draft of {
    1. url : string;
    }
  6. | MetaSchema_cycle of {
    1. url : string;
    }
  7. | Validation_error of {
    1. url : string;
    2. src : validation_error;
    }
  8. | Parse_id_error of {
    1. loc : string;
    }
  9. | Parse_anchor_error of {
    1. loc : string;
    }
  10. | Duplicate_id of {
    1. url : string;
    2. id : string;
    3. ptr1 : string;
    4. ptr2 : string;
    }
  11. | Duplicate_anchor of {
    1. anchor : string;
    2. url : string;
    3. ptr1 : string;
    4. ptr2 : string;
    }
  12. | Invalid_json_pointer of string
  13. | Json_pointer_not_found of string
  14. | Anchor_not_found of {
    1. url : string;
    2. reference : string;
    }
  15. | Unsupported_vocabulary of {
    1. url : string;
    2. vocabulary : string;
    }
  16. | Invalid_regex of {
    1. url : string;
    2. regex : string;
    3. src : exn;
    }
  17. | Bug of exn

Compilation errors

Sourcetype json_pointer

JSON pointer for navigating JSON documents

High-Level API

Sourcetype validator

A reusable validator

Sourceval validate_file : ?draft:draft -> schema:string -> Yojson.Basic.t -> (unit, validation_error) result

Validate JSON against a schema from a file/URL

Sourceval validate_strings : ?draft:draft -> schema:string -> json:string -> unit -> (unit, validation_error) result

Validate JSON string against schema string

Sourceval create_validator : ?draft:draft -> ?enable_format_assertions:bool -> ?enable_content_assertions:bool -> string -> (validator, compile_error) result

Create a reusable validator for efficiency

Validate using a pre-compiled validator

Sourceval create_validator_with_loader : ?draft:draft -> ?enable_format_assertions:bool -> ?enable_content_assertions:bool -> url_loader:Loader.url_loader -> schema:Yojson.Basic.t -> unit -> (validator, compile_error) result

Create a validator with a custom URL loader for remote refs

Sourceval create_validator_from_json : ?draft:draft -> ?enable_format_assertions:bool -> ?enable_content_assertions:bool -> schema:Yojson.Basic.t -> unit -> (validator, compile_error) result

Create a validator from JSON schema (for testing)

Sourceval draft4_validator : validator

Pre-compiled validators for meta-schemas

Sourceval draft6_validator : validator
Sourceval draft7_validator : validator
Sourceval draft2019_09_validator : validator
Sourceval draft2020_12_validator : validator

Low-Level API

Compilation

Sourcemodule Compiler : sig ... end

Schema Operations

Sourcemodule Schema : sig ... end

Types

Sourcemodule Types : sig ... end

Output Formats

Sourcemodule Output : sig ... end

Format Validation

Sourcemodule Format : sig ... end

Content Validation

Sourcemodule Content : sig ... end

URL Loading

Sourcemodule Loader : sig ... end

Utilities

Sourcemodule Draft : sig ... end
Sourcemodule Json_pointer : sig ... end
Sourcemodule Validation_error : sig ... end

Pretty Printing

Sourceval pp_compile_error : Format.formatter -> compile_error -> unit
Sourceval pp_validation_error : Format.formatter -> validation_error -> unit
Sourceval pp_validation_error_verbose : Format.formatter -> validation_error -> unit
OCaml

Innovation. Community. Security.