Skip to content

Commit 80a8177

Browse files
committed
Add Yocaml.Data.Validation.where_opt
1 parent 8fe22fb commit 80a8177

File tree

3 files changed

+48
-0
lines changed

3 files changed

+48
-0
lines changed

CHANGES.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
### unreleased
22

33
#### Yocaml
4+
5+
- Add `Data.Validation.where_opt` (and `String`, `Int` and `Float` version) (by [xvw](https://xvw.lol))
46
- Improve pretty-printing of validation errors (by [Linda-Njau](https://github.com/Linda-Njau))
57
- Fix typos and improve logs display (by [clementd](https://clementd.wtf))
68

lib/core/data.ml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,16 @@ module Validation = struct
281281
fail_with ~given:(Format.asprintf "%a" pp x) (f x)
282282
else Ok x
283283

284+
let where_opt ?pp ?message predicate x =
285+
match predicate x with
286+
| Some x -> Ok x
287+
| None ->
288+
let pp = mk_pp pp in
289+
let f =
290+
Option.value ~default:(fun _ -> "unsatisfied predicate") message
291+
in
292+
fail_with ~given:(Format.asprintf "%a" pp x) (f x)
293+
284294
let sum branch x =
285295
let str_expectation () =
286296
branch
@@ -541,6 +551,9 @@ module Validation = struct
541551
let where ?message predicate actual =
542552
where ~pp:string_pp ?message predicate actual
543553

554+
let where_opt ?message predicate actual =
555+
where_opt ~pp:string_pp ?message predicate actual
556+
544557
let lowercase_ascii = Stdlib.String.lowercase_ascii
545558
let trim = Stdlib.String.trim
546559
end
@@ -561,6 +574,7 @@ module Validation = struct
561574
let le = pp_cmp le
562575
let one_of = pp_equal one_of
563576
let where = with_pp where
577+
let where_opt ?message f = (with_pp where_opt) ?message f
564578
end
565579

566580
module Float = struct
@@ -579,6 +593,7 @@ module Validation = struct
579593
let le = pp_cmp le
580594
let one_of = pp_equal one_of
581595
let where = with_pp where
596+
let where_opt ?message f = (with_pp where_opt) ?message f
582597
end
583598

584599
let negate validator x =

lib/core/data.mli

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -332,6 +332,15 @@ module Validation : sig
332332
(** [where ?pp predicate x] ensure that [x] is satisfying [predicate]. [pp] is
333333
used for error-reporting.*)
334334

335+
val where_opt :
336+
?pp:(Format.formatter -> 'a -> unit)
337+
-> ?message:('a -> string)
338+
-> ('a -> 'b option)
339+
-> 'a
340+
-> 'b validated_value
341+
(** [where_opt ?pp predicate x] ensure that [predicate x] return [Some]. [pp]
342+
is used for error-reporting.*)
343+
335344
val const : 'a -> 'b -> ('a, 'c) result
336345
(** [const k r] wrap [k] as valid and discard [r]. *)
337346

@@ -535,6 +544,14 @@ module Validation : sig
535544
(** [where ?message predicate actual] ensures that [actual] satisfies
536545
[predicate]. [message] is used for custom error messages. *)
537546

547+
val where_opt :
548+
?message:(string -> string)
549+
-> (string -> 'b option)
550+
-> string
551+
-> 'b validated_value
552+
(** [where_opt ?pp predicate x] ensure that [predicate x] return [Some].
553+
[pp] is used for error-reporting.*)
554+
538555
(** Useful function to be used with regular string validators. *)
539556

540557
val lowercase_ascii : string -> string
@@ -576,6 +593,13 @@ module Validation : sig
576593
val where :
577594
?message:(int -> string) -> (int -> bool) -> int -> int validated_value
578595
(** Integer version of {!val:Data.Validation.where} *)
596+
597+
val where_opt :
598+
?message:(int -> string)
599+
-> (int -> 'b option)
600+
-> int
601+
-> 'b validated_value
602+
(** Integer version of {!val:Data.Validation.where_opt} *)
579603
end
580604

581605
(** {2 Float validators}
@@ -616,6 +640,13 @@ module Validation : sig
616640
-> float
617641
-> float validated_value
618642
(** Float version of {!val:Data.Validation.where} *)
643+
644+
val where_opt :
645+
?message:(float -> string)
646+
-> (float -> 'b option)
647+
-> float
648+
-> 'b validated_value
649+
(** Integer version of {!val:Data.Validation.where_opt} *)
619650
end
620651

621652
(** {2 Validator combinators} *)

0 commit comments

Comments
 (0)