Module Kwvalid.Json

module Json: sig .. end

Json Validators

See the Tutorial below.
See also RFC 7158


type [< `A of 'a list
| `Bool of bool
| `Float of float
| `Null
| `O of (string * 'a) list
| `String of string ]
as 'a
parsed_json
= 'a
The type of parsed Json representations validated by this module.

This representation is compatible with Ezjson.from_string.

type 'a o = [ `Opt of string * 'a descriptor
| `Req of string * 'a descriptor ] list
The type of object descriptor (`O) field values.
type 'a descriptor = [ `A of ('a list -> bool) * 'a descriptor
| `Bool of Kwvalid.t
| `Float of Kwvalid.t
| `Null
| `O of 'a o
| `String of Kwvalid.t ]
The type of Json descriptors.

The array (`A) descriptor carries a predicate that determines whether or not the array is of the correct size. See Kwvalid.Json.Size for some predefined predicates.

val string_of_o : ([< `Opt of
string *
([< `A of 'c * 'b
| `Bool of Kwvalid.t
| `Float of Kwvalid.t
| `Null
| `O of 'a
| `String of Kwvalid.t ]
as 'b)
| `Req of string * 'b ]
list as 'a) ->
string
val string_of_descriptor : ([< `A of 'b * 'a
| `Bool of Kwvalid.t
| `Float of Kwvalid.t
| `Null
| `O of [< `Opt of string * 'a | `Req of string * 'a ] list
| `String of Kwvalid.t ]
as 'a) ->
string

string_of_descriptor d: render a Json descriptor as a readable string
module Size: sig .. end
Predefined Array Size Predicates
module Num: Kwvalid.Number(sig
include Real
val name : string
end)
Json Number Validators
val make : (string ->
([< `A of 'a list
| `Bool of bool
| `Float of float
| `Null
| `O of ('b * 'a) list
| `String of string ]
as 'a)) ->
([ `A of ('a list -> bool) * 'c
| `Bool of Kwvalid.t
| `Float of Kwvalid.t
| `Null
| `O of
[< `Opt of string * 'c & 'b * 'c | `Req of string * 'c & 'b * 'c ] list
| `String of Kwvalid.t ] as 'c) ->
Kwvalid.t
make from_string v: make a validator from a Json descriptor.

from_string is a function to parse a string of Json; the representation used is compatible with Ezjson.from_string.


Tutorial

Kwvalid.Json.make takes a data structure (a Kwvalid.Json.descriptor) that describes valid chunks of Json and returns a Kwvalid.t that can be used to validate it. Here's an example descriptor:

 let d = `O [
            `Req ("patron", `O [`Req ("barcode", `String always)]);
            `Req ("items",  `A (Size.any, `O [`Req ("barcode", `String always)]));
          ] 

This one says that the only valid Json is an object (`O) that has a required (`Req) "patron" field whose value is an object with a required "barcode" field whose value is a string; likewise for the "items" field, except it's an array (of any length) of the same kind of objects.

The value v carried by (`String v) needs to be a Kwvalid.t -- a validator itself: it's called to validate the string value in the Json.

So, this array of numbers is valid json:

 # Ezjsonm.from_string "[1,2,3]" ;;
          - : [> Ezjsonm.t ] = `A [`Float 1.; `Float 2.; `Float 3.] 

but is not a valid d. Neither is:

 {"patron": {}, "items": []} 

because d says thet the "patron" field needs to be an object with a "barcode" field.

This one:

 {"patron": {"barcode": "1234"}, "items": []} 

is valid, because the array is allowed, by the array size predicate Kwvalid.Json.Size.any, to be of any length.

This is invalid:

 { "patron": { "barcode": "1234"}, "items": [1,2] } 

because the items need to be objects, not numbers.