module Validate:sig..end
{ foo = 3;
bar = { snoo = 34.5;
blue = Snoot -6; }
}
One might end up with an error with the error path:
bar.blue.Snoot : value -6 <= bound 0
By convention, the validations for a type defined in module M appear in module M,
and have their name prefixed by validate_. E.g. Int.validate_positive.
Here's an example of how you would use validate with a record.
type t =
{ foo: int;
bar: float;
}
with fields
let validate t =
let module V = Validate in
let w check = V.field_folder t check in
V.of_list
(Fields.fold ~init:[]
~foo:(w Int.validate_positive)
~bar:(w Float.validate_non_negative)
)
And here's an example of how you would use it with a variant type:
type t =
| Foo of int
| Bar of (float * int)
| Snoo of Floogle.t
let validate = function
| Foo i -> V.name "Foo" (Int.validate_positive i)
| Bar p -> V.name "Bar" (V.pair
~fst:Float.validate_positive
~snd:Int.validate_non_negative)
| Snoo floogle -> V.name "Snoo" Floogle.validate
type t
type'acheck ='a -> t
val pass : tval fail : string -> tval fails : string -> 'a -> ('a -> Sexplib.Sexp.t) -> tval of_list : t list -> tval name : string -> t -> tval name_list : string -> t list -> tval fail_fn : string -> 'a checkfail_fn err returns a function that always returns fail, with err as the error
message. (Note that there is no pass_fn so as to discourage people from ignoring
the type of the value being passed unconditionally irrespective of type.)val pass_bool : bool checkval pass_unit : unit checkval protect : 'a check -> 'a checkprotect f x applies the validation f to x, catching any exceptions and returning
them as errors.val result : t -> unit Or_error.tval errors : t -> string listval maybe_raise : t -> unitval valid_or_error : 'a -> 'a check -> 'a Or_error.tval field : 'record -> ('record, 'a) Fieldslib.Field.t -> 'a check -> tval field_folder : 'record ->
'a check ->
t list -> ('record, 'a) Fieldslib.Field.t -> t listFields.fold.val all : 'a check list -> 'a checkval of_result : ('a -> (unit, string) Result.t) -> 'a checkval of_error : ('a -> unit Or_error.t) -> 'a checkval booltest : ('a -> bool) -> if_false:string -> 'a checkval pair : fst:'a check -> snd:'b check -> ('a * 'b) checkval list_indexed : 'a check -> 'a list checkval list : name:('a -> string) -> 'a check -> 'a list checkval first_failure : t -> t -> tval of_error_opt : string option -> tval alist : name:('a -> string) -> 'b check -> ('a * 'b) list check