From b00a928be064cb43bbeabe817e60eaae30740a8f Mon Sep 17 00:00:00 2001 From: Josef Erben Date: Tue, 29 Sep 2020 15:21:41 +0200 Subject: [PATCH] Release 0.0.2 --- CHANGES.md | 4 ++++ conformist.opam | 2 +- dune-project | 2 +- src/conformist.ml | 28 +++++++++++++++------------- src/conformist.mli | 14 +++++++------- 5 files changed, 28 insertions(+), 22 deletions(-) diff --git a/CHANGES.md b/CHANGES.md index dc81bb8..e64fcb0 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -1,5 +1,9 @@ # Changelog +## [0.0.2] - 2020-09-29 +### Fixed +- Move `fold_left` to top level module and accept `Conformist.t` as input + ## [0.0.1] - 2020-09-20 ### Added - Initial release supporting `int`, `float`, `string`, `bool`, `Ptime.date` and custom types diff --git a/conformist.opam b/conformist.opam index d5c4b6b..1c926a0 100644 --- a/conformist.opam +++ b/conformist.opam @@ -1,6 +1,6 @@ # This file is generated by dune, edit dune-project instead opam-version: "2.0" -version: "0.0.1" +version: "0.0.2" synopsis: "Conformist allows you to define schemas to decode, validate and sanitize input data declaratively" description: """ diff --git a/dune-project b/dune-project index 6d370ae..17d2ded 100644 --- a/dune-project +++ b/dune-project @@ -2,7 +2,7 @@ (generate_opam_files true) (name conformist) -(version 0.0.1) +(version 0.0.2) (authors "Josef Erben") (source (github oxidizing/conformist)) diff --git a/src/conformist.ml b/src/conformist.ml index c795c6d..5d249f2 100644 --- a/src/conformist.ml +++ b/src/conformist.ml @@ -88,17 +88,6 @@ module Field = struct | _ -> Error msg in make name meta decoder validator false - - let rec fold_left : - type ty args. - f:('res -> 'meta any_field -> 'res) -> - init:'res -> - ('meta, args, ty) list -> - 'res = - fun ~f ~init fields -> - match fields with - | [] -> init - | field :: fields -> fold_left ~f ~init:(f init (AnyField field)) fields end let custom = Field.make_custom @@ -124,11 +113,24 @@ let empty = { fields = Field.[]; ctor = () } let make fields ctor = { fields; ctor } +let rec fold_left' : + type ty args. + f:('res -> 'meta Field.any_field -> 'res) -> + init:'res -> + ('meta, args, ty) Field.list -> + 'res = + fun ~f ~init fields -> + match fields with + | [] -> init + | field :: fields -> fold_left' ~f ~init:(f init (AnyField field)) fields + +let fold_left ~f ~init schema = fold_left' ~f ~init schema.fields + type validation_error = (string * string) list type input = (string * string list) list -let validate { fields; _ } input = +let validate schema input = let f errors field = let name = Field.name field in match List.assoc name input with @@ -141,7 +143,7 @@ let validate { fields; _ } input = if Field.optional field then errors else List.cons (name, "No value provided") errors in - Field.fold_left ~f ~init:[] fields |> List.rev + fold_left ~f ~init:[] schema |> List.rev let rec decode : type meta ctor ty. diff --git a/src/conformist.mli b/src/conformist.mli index 07a0709..67e85c5 100644 --- a/src/conformist.mli +++ b/src/conformist.mli @@ -130,13 +130,6 @@ module Field : sig val optional : 'a any_field -> bool (** [optional field] turns a [field] into an optional field. This means that input that doesn't contain a value for the field will yield in a valid field. *) - - val fold_left : - f:('res -> 'meta any_field -> 'res) -> - init:'res -> - ('meta, 'args, 'ty) list -> - 'res - (** [fold_left ~f ~init fields] can be used to traverse a lit of fields. Use the functions [meta], [nae], [validate] and [optional] in f. *) end type 'a decoder = string -> ('a, string) result @@ -212,6 +205,13 @@ val empty : ('a, unit, unit) t val make : ('a, 'b, 'c) Field.list -> 'b -> ('a, 'b, 'c) t (** [make fields constructor] create a schema. *) +val fold_left : + f:('res -> 'meta Field.any_field -> 'res) -> + init:'res -> + ('meta, 'args, 'ty) t -> + 'res +(** [fold_left ~f ~init schema] can be used to traverse the list of fields of [schema]. Use the functions [meta], [name], [validate] and [optional] in f. *) + type validation_error = (string * string) list (** An empty [validation_error] means that the schema is valid. *)