Module Changeset_lib.Make
Parameters
Signature
Types
type source
= I.source
The type of the original record type.
Exceptions
Binding
type binding
The type of bindings used by the
put_changes
function.
Errors
Changeset
val empty : t
The empty changeset.
val is_empty : t ‑> Base.bool
is_empty t
returns true if the changes is empty, false otherwise.
val is_valid : t ‑> Base.bool
is_valid t
returnstrue
if the changesett
does not contain any error orfalse
otherwise.
val from_record : source ‑> t
from_record x
returns a changeset in which all the elements inx
are loaded as changes.
val put_change : 'a label ‑> 'a ‑> t ‑> t
put_change l x t
puts a change associating the labell
with valuex
in the changesett
.
val put_changes : binding Base.list ‑> t ‑> t
put_change bs t
appliesput_change
to the list of bindingbs
in the changesett
from left to right.
val mem_label : _ label ‑> t ‑> Base.bool
mem_label l t
returnstrue
if there is a binding ofl
in the changesett
orfalse
otherwise.
val mem_error : error ‑> t ‑> Base.bool
mem_error e t
returnstrue
if the error belongs to the changesett
orfalse
otherwise.
val find : 'a label ‑> t ‑> 'a Base.option
find l t
returns the value associated with the labell
in the changesett
wrapped in an option value. It returnsNone
if the labell
is unbound int
.
val find_exn : 'a label ‑> t ‑> 'a
find l t
returns the value associated with labell
in the changesett
. It raises the exceptionNot_found
if the labell
is unbound int
.
val apply : t ‑> (source, t) Base.Result.t
apply t
returns:Ok x
wherex
is a value of typesource
if the changesett
is valid and all the labels are present in the changes oft
.Error t'
otherwise, wheret'
corresponds to the changesett
if it was not valid, ort
expanded with anerror
(l => "is missing")
if the changeset was valid but alabel
l
was not found.
val apply_exn : t ‑> source
apply t
returns a value of typesource
if the changesett
is valid and all the labels are present in the changes oft
. It raises the exceptionInvalid_changes
otherwise.
val show_errors : t ‑> Base.string
show_errors t
returns a string in the json format of the errors contained in the changesett
.
Validations
val validate_change : 'a label ‑> ('a ‑> Base.string Base.list) ‑> t ‑> t
validate_change l f t
appliesf
to the value associated with labell
in the changesett
and adds the list of error messages returned to the errors of the changesett
, all being indexed by labell
. If there is no binding for labell
in changesett
, the validation is not applied.
val require : 'a label ‑> ?message:Base.string ‑> t ‑> t
require l t
validates that there is a binding ofl
in the changesett
. If not, the errormessage
indexed by the labell
is added to the changesett
. Default error message is"is required"
.
type length_opt
=[
|
`is of Base.int
|
`min of Base.int
|
`max of Base.int
]
Options for length validations are the ones above where:
`is n
: the length must be exactlyn
.`min n
: the length must be greater than or equal ton
.`max n
: the length must be less than or equal ton
.
Error messages for
validate_length
,validate_array_length
andvalidate_list_length
are the following:`is n
:"should be %d element(s)"
`min n
:"should be at least %d element(s)"
`max n
:"should be at most %d element(s)"
Error messages for
validate_string_length
are the following:`is n
:"should be %d character(s)"
`min n
:"should be at least %d character(s)"
`max n
:"should be at most %d character(s)"
val validate_length : 'a label ‑> ('a ‑> Base.int) ‑> length_opt Base.list ‑> t ‑> t
validate_length l f opts t
validates that the length calculated by applyingf
to the value indexed by the labell
in the changesett
respects theopts
.
val validate_string_length : Base.string label ‑> length_opt Base.list ‑> t ‑> t
validate_string_length l opts t
validates that the length of the string indexed by the labell
in the changesett
respects theopts
.
val validate_array_length : 'a Base.array label ‑> length_opt Base.list ‑> t ‑> t
validate_array_length l opts t
validates that the length of the array indexed by the labell
in the changesett
respects theopts
.
val validate_list_length : 'a Base.list label ‑> length_opt Base.list ‑> t ‑> t
validate_list_length l opts t
validates that the length of the list indexed by the labell
in the changesett
respects theopts
.
type 'a ord_opt
=[
|
`equal_to of 'a
|
`greater_than of 'a
|
`greater_than_or_equal_to of 'a
|
`less_than of 'a
|
`less_than_or_equal_to of 'a
]
val validate : 'a label ‑> (module Base.Comparator.S with type t = 'a) ‑> 'a ord_opt Base.list ‑> t ‑> t
validate l cmp opts t
validates that the value indexed by the labell
in the changesett
respects theopts
using thecompare
function provided by the first-class modulecmp
.
val validate_int : Base.int label ‑> Base.int ord_opt Base.list ‑> t ‑> t
validate_int l opts t
validates that the integer indexed by labell
in the changesett
respects theopts
.
val validate_float : Base.float label ‑> Base.float ord_opt Base.list ‑> t ‑> t
validate_float l opts t
validates that float indexed by labell
in the changesett
respects theopts
.
val validate_bool : Base.bool label ‑> Base.bool ord_opt Base.list ‑> t ‑> t
validate_bool l opts t
validates that the boolean indexed by labell
in the changesett
respects theopts
.
val validate_char : Base.char label ‑> Base.char ord_opt Base.list ‑> t ‑> t
validate_char l opts t
validates that the char indexed by labell
in the changesett
respects theopts
.
val validate_string : Base.string label ‑> Base.string ord_opt Base.list ‑> t ‑> t
validate_string l opts t
validates that the string indexed by labell
in the changesett
respects theopts
.
val validate_inclusion : 'a label ‑> ('a ‑> 'a ‑> Base.bool) ‑> 'a Base.list ‑> ?message:Base.string ‑> t ‑> t
validate_inclusion equal l xs ?message t
validates that the value indexed by labell
in the changesett
is a an included inxs
. If not, an errormessage
indexed by the labell
is added to the changesett
. Default error message is"is invalid"
.
val validate_exclusion : 'a label ‑> ('a ‑> 'a ‑> Base.bool) ‑> 'a Base.list ‑> ?message:Base.string ‑> t ‑> t
validate_excluded equal l xs ?message t
validates that the value indexed by labell
in the changesett
is not included inxs
. If not, an errormessage
indexed by the labell
is added to the changesett
. Default error message is"is reserved"
.
val validate_acceptance : Base.bool label ‑> ?message:Base.string ‑> t ‑> t
validate_string l ?message t
validates that the change indexed by labell
in the changesett
istrue
. If not, an errormessage
indexed by the labell
is added to the changesett
. Default error message is"must be accepted"
.
val validate_format : Base.string label ‑> Str.regexp ‑> ?message:Base.string ‑> t ‑> t
validate_format l reg ?message t
validates that the change indexed by labell
in the changesett
is accepted by the regexr
. If not, an errormessage
indexed by the labell
is added to the changesett
. Default error message is"has invalid format"
.