Module Base__.Validate

type t

The result of a validation. This effectively contains the list of errors, qualified by their location path

type 'a check = 'a -> t

To make function signatures easier to read.

val pass : t

A result containing no errors.

val fail : string -> t

A result containing a single error.

val fails : string -> 'a -> ('a -> Base.Sexp.t) -> t
val fail_s : Base.Sexp.t -> t

This can be used with the %sexp extension.

val failf : ('a, unit, string, t) Stdlib.format4 -> 'a

Like sprintf or failwithf but produces a t instead of a string or exception.

val combine : t -> t -> t
val of_list : t list -> t

Combines multiple results, merging errors.

val name : string -> t -> t

Extends location path by one name.

val name_list : string -> t list -> t
val fail_fn : string -> _ check

fail_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 check

Checks for unconditionally passing a bool.

val pass_unit : unit check

Checks for unconditionally passing a unit.

val protect : 'a check -> 'a check

protect f x applies the validation f to x, catching any exceptions and returning them as errors.

val try_with : (unit -> unit) -> t

try_with f runs f catching any exceptions and returning them as errors.

val result : t -> unit Base.Or_error.t
val errors : t -> string list

Returns a list of formatted error strings, which include both the error message and the path to the error.

val maybe_raise : t -> unit

If the result contains any errors, then raises an exception with a formatted error message containing a message for every error.

val valid_or_error : 'a -> 'a check -> 'a Base.Or_error.t

Returns an error if validation fails.

val field : 'record -> ([> `Read ], 'record'a) Base.Field.t_with_perm -> 'a check -> t

Used for validating an individual field.

val field_folder : 'record -> 'a check -> t list -> ([> `Read ], 'record'a) Base.Field.t_with_perm -> t list

Creates a function for use in a Fields.fold.

val field_direct_folder : 'a check -> (t list -> ([> `Read ], 'record'a) Base.Field.t_with_perm -> 'record -> 'a -> t list) Base.Staged.t

Creates a function for use in a Fields.Direct.fold.

val all : 'a check list -> 'a check

Combines a list of validation functions into one that does all validations.

val of_result : ('a -> (unit, string) Base.Result.t) -> 'a check

Creates a validation function from a function that produces a Result.t.

val of_error : ('a -> unit Base.Or_error.t) -> 'a check
val booltest : ('a -> bool) -> if_false:string -> 'a check

Creates a validation function from a function that produces a bool.

val pair : fst:'a check -> snd:'b check -> ('a * 'b) check

Validation functions for particular data types.

val list_indexed : 'a check -> 'a list check

Validates a list, naming each element by its position in the list (where the first position is 1, not 0).

val list : name:('a -> string) -> 'a check -> 'a list check

Validates a list, naming each element using a user-defined function for computing the name.

val first_failure : t -> t -> t
val of_error_opt : string option -> t
val alist : name:('a -> string) -> 'b check -> ('a * 'b) list check

Validates an association list, naming each element using a user-defined function for computing the name.

val bounded : name:('a -> string) -> lower:'a Base.Maybe_bound.t -> upper:'a Base.Maybe_bound.t -> compare:('a -> 'a -> int) -> 'a check
module Infix : sig ... end