Skip to content

Commit

Permalink
Release 0.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
joseferben committed Sep 29, 2020
1 parent 38ad2df commit b00a928
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 22 deletions.
4 changes: 4 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion conformist.opam
Original file line number Diff line number Diff line change
@@ -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: """
Expand Down
2 changes: 1 addition & 1 deletion dune-project
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
28 changes: 15 additions & 13 deletions src/conformist.ml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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.
Expand Down
14 changes: 7 additions & 7 deletions src/conformist.mli
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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. *)

Expand Down

0 comments on commit b00a928

Please sign in to comment.