package yocaml
Install
    
    dune-project
 Dependency
Authors
Maintainers
Sources
sha256=e5ab173efd4c356dfee3e9af2165762ec4f1b5d38bdf1000d13e5eb6f18edbae
    
    
  sha512=77ce2f269a6058c91ed75dee164cf9073147aa5d48c5dbdb2d6b282fc184d3a9b8155e772dd24acb8c0618ef7e47c0222bf65e13ecc3e08fab87351ea4f27192
    
    
  doc/yocaml/Yocaml/Data/Validation/index.html
Module Data.ValidationSource
Used to validate data described by type Yocaml.Data.t to build validation pipelines. The aim of this module is to produce combinators for building validation pipelines that support nesting and that can transform any value described by the AST in Data into arbitrary OCaml values.
Types
An extensible type dedicated to extending potential validation errors.
type value_error = - | Invalid_shape of {- expected : string;
- given : t;
 - }
- | Invalid_list of {- errors : (int * value_error) Nel.t;
- given : t list;
 - }
- | Invalid_record of {- errors : record_error Nel.t;
- given : (string * t) list;
 - }
- | With_message of {- }
- | Custom of custom_error
Type used to describe when a value does not have the expected form (for example when a float is given whereas a character string is expected). The With_message is only parametrized by string in order to allow to write custom error messages.
and record_error = - | Missing_field of {- }
- | Invalid_field of {- given : t;
- field : string;
- error : value_error;
 - }
Errors at the field level, for validating records.
Used to validate data described by type t to build validation pipelines.
Used to validate records described by type t to build validation pipelines.
Types helpers
Helpers for creating errors.
fail_with ~given message returns a validation error associated to a message.
fail_with_custom err returns a custonm validation error.
Validators
Validators act on classic AST values. They are used to validate the fields of a record (or the inhabitants of a list). They are often used as arguments to the optional, optional_or and required functions.
ensure that a value is null.
ensure that a value is a boolean.
ensure that a value is an int (or a float).
ensure that a value is an float.
ensure that a value is a string, if strict is false it will consider bool, int, float also as strings.
list_of v x ensure that x is a list that satisfy v (all errors are collected).
record v x ensure that x has the shape validated by v (all errors are collected).
option v x validate a value using v that can be null wrapped into an option.
val pair : 
  (t -> 'a validated_value) ->
  (t -> 'b validated_value) ->
  t ->
  ('a * 'b) validated_valuepair fst snd x validated a pair (described as Yocaml.Data.pair, {"fst": a, "snd": b}).
val triple : 
  (t -> 'a validated_value) ->
  (t -> 'b validated_value) ->
  (t -> 'c validated_value) ->
  t ->
  ('a * 'b * 'c) validated_valuetriple f g h v define a triple validator built on top of pair. (Under the hood, it treat value like that (x, (y, z)). )
val quad : 
  (t -> 'a validated_value) ->
  (t -> 'b validated_value) ->
  (t -> 'c validated_value) ->
  (t -> 'd validated_value) ->
  t ->
  ('a * 'b * 'c * 'd) validated_valuequad f g h i v define a quad validator built on top of triple. (Under the hood, it treat value like that (w, (x, (y, z))). )
sum [(k, v)] value validated a sum value using the representation described in Yocaml.Data.sum: {"constr": const_value, "value": e}.
val either : 
  (t -> 'a validated_value) ->
  (t -> 'b validated_value) ->
  t ->
  ('a, 'b) Either.t validated_valueeither left right v validated a either value.
Validators on parsed data
Validators to use when data is already parsed.
positive x ensure that x is positive.
positive x ensure that x is positive.
bounded ~min ~max x ensure that x is included in the range [min;max] (both included).
bounded ~min ~max x ensure that x is included in the range [min;max] (both included).
non_empty l ensure that l is non-empty.
val equal : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?equal:('a -> 'a -> bool) ->
  'a ->
  'a ->
  'a validated_valueequal ?pp ?equal x y ensure that y is equal to x. pp is used for error-reporting.
val not_equal : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?equal:('a -> 'a -> bool) ->
  'a ->
  'a ->
  'a validated_valuenot_equal ?pp ?equal x y ensure that y is different of x. pp is used for error-reporting.
val gt : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?compare:('a -> 'a -> int) ->
  'a ->
  'a ->
  'a validated_valuegt ?pp ?equal x y ensure that x is greater than y. pp is used for error-reporting.
val ge : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?compare:('a -> 'a -> int) ->
  'a ->
  'a ->
  'a validated_valuege ?pp ?equal x y ensure that x is greater or equal to y. pp is used for error-reporting.
val lt : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?compare:('a -> 'a -> int) ->
  'a ->
  'a ->
  'a validated_valuelt ?pp ?equal x y ensure that x is lesser than y. pp is used for error-reporting.
val le : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?compare:('a -> 'a -> int) ->
  'a ->
  'a ->
  'a validated_valuele ?pp ?equal x y ensure that x is lesser or equal to y. pp is used for error-reporting.
val one_of : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?equal:('a -> 'a -> bool) ->
  'a list ->
  'a ->
  'a validated_valueone_of ?pp ?equal li x ensure that x is include in li. pp is used for error-reporting.
val where : 
  ?pp:(Format.formatter -> 'a -> unit) ->
  ?message:('a -> string) ->
  ('a -> bool) ->
  'a ->
  'a validated_valuewhere ?pp predicate x ensure that x is satisfying predicate. pp is used for error-reporting.
Infix operators
Infix operators are essentially used to compose data validators (unlike binding operators, which are used to compose record validation fragments).
val (&) : 
  ('a -> 'b validated_value) ->
  ('b -> 'c validated_value) ->
  'a ->
  'c validated_value(v1 & v2) x sequentially compose v2 (v1 x), so v1 following by v2. For example : int &> positive &> c.
val (/) : 
  ('a -> 'b validated_value) ->
  ('a -> 'b validated_value) ->
  'a ->
  'b validated_value(v1 / v2) x perform v1 x and if it fail, performs v2 x.
(v1 $ f) x perform f on the result of v1 x.
Fields validators
Field validators are used to describe parallel validation strategies for each field in a record and collect errors by field. Usually, a field validator takes as arguments the associative list of keys/values in a record, the name of the field to be observed and a regular validator.
val required : 
  (string * t) list ->
  string ->
  (t -> 'a validated_value) ->
  'a validated_recordrequired assoc field validator required field of assoc, validated by validator.
val optional : 
  (string * t) list ->
  string ->
  (t -> 'a validated_value) ->
  'a option validated_recordoptional assoc field validator optional field of assoc, validated by validator.
val optional_or : 
  (string * t) list ->
  string ->
  default:'a ->
  (t -> 'a validated_value) ->
  'a validated_recordoptional_or ~default assoc field validator optional field of assoc, validated by validator. If the field does not exists, it return default. (default is not validated)
Bindings operators
Binding operators are used to link fields together to build a complete validation.
let+ x = v in  k x is map (fun x -> k x) v.
let+ x = v and+ y = w in k x y is map2 (fun x y -> k x y) v w.
let* r = f x in return r tries to produce a result Ok from the expression f x, if the expression returns Error _, the computation chain is interrupted.
Warning: the semantics of let* are significantly different from a succession of let+ ... and+ ... which allow errors to be collected in parallel (independently), whereas let* captures them sequentially. The composition of let* and let+ is tricky and let* should only be used to validate preconditions.