Validate
SourceA module for organizing validations of data structures.
Allows standardized ways of checking for conditions, and keeps track of the location of errors by keeping a path to each error found. Thus, if you were validating the following datastructure:
{ 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;
}
[@@deriving fields ~iterators:to_list]
let validate t =
let module V = Validate in
let w check = V.field check t in
Fields.to_list
~foo:(w Int.validate_positive)
~bar:(w Float.validate_non_negative)
|> V.of_list
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 p)
| Snoo floogle -> V.name "Snoo" (Floogle.validate floogle)
The result of a validation. This effectively contains the list of errors, qualified by their location path
A result containing a single error.
This can be used with the %sexp
extension.
Like sprintf
or failwithf
but produces a t
instead of a string or exception.
Extends location path by one name.
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.)
protect f x
applies the validation f
to x
, catching any exceptions and returning them as errors.
try_with f
runs f
catching any exceptions and returning them as errors.
Returns a list of formatted error strings, which include both the error message and the path to the error.
If the result contains any errors, then raises an exception with a formatted error message containing a message for every error.
Returns an error if validation fails.
Used for validating an individual field. Should be used with Fields.to_list
.
val field_direct :
'a check ->
([> `Read ], 'record, 'a) Base.Field.t_with_perm ->
'record ->
'a ->
t
Used for validating an individual field. Should be used with Fields.Direct.to_list
.
val field_folder :
'a check ->
'record ->
t Base.list ->
([> `Read ], 'record, 'a) Base.Field.t_with_perm ->
t Base.list
Creates a function for use in a Fields.fold
.
val field_direct_folder :
'a check ->
(t Base.list ->
([> `Read ], 'record, 'a) Base.Field.t_with_perm ->
'record ->
'a ->
t Base.list)
Base.Staged.t
Creates a function for use in a Fields.Direct.fold
.
Combines a list of validation functions into one that does all validations.
Creates a validation function from a function that produces a Result.t
.
Creates a validation function from a function that produces a bool.
Validation functions for particular data types.
Validates a list, naming each element by its position in the list (where the first position is 1, not 0).
Validates a list, naming each element using a user-defined function for computing the name.
Validates an association list, naming each element using a user-defined function for computing the name.
val bounded :
name:('a -> Base.string) ->
lower:'a Base.Maybe_bound.t ->
upper:'a Base.Maybe_bound.t ->
compare:('a -> 'a -> Base.int) ->
'a check