-module(validator). -compile(no_auto_import). -export([build1/1, build2/1, build3/1, build4/1, build5/1, build6/1, validate/3, keep/2, custom/2, 'and'/2, all/1, whole/1]). curry2(Constructor) -> fun(A) -> fun(B) -> Constructor(A, B) end end. curry3(Constructor) -> fun(A) -> fun(B) -> fun(C) -> Constructor(A, B, C) end end end. curry4(Constructor) -> fun(A) -> fun(B) -> fun(C) -> fun(D) -> Constructor(A, B, C, D) end end end end. curry5(Constructor) -> fun(A) -> fun(B) -> fun(C) -> fun(D) -> fun(E) -> Constructor(A, B, C, D, E) end end end end end. curry6(Constructor) -> fun(A) -> fun(B) -> fun(C) -> fun(D) -> fun(E) -> fun(F) -> Constructor(A, B, C, D, E, F) end end end end end end. build1(Constructor) -> {ok, Constructor}. build2(Constructor) -> {ok, curry2(Constructor)}. build3(Constructor) -> {ok, curry3(Constructor)}. build4(Constructor) -> {ok, curry4(Constructor)}. build5(Constructor) -> {ok, curry5(Constructor)}. build6(Constructor) -> {ok, curry6(Constructor)}. validate(Accumulator, Value, Validator) -> case Validator(Value) of {ok, Value@1} -> gleam@result:map(Accumulator, fun(Acc) -> Acc(Value@1) end); {error, {E, Errors}} -> case Accumulator of {ok, _} -> {error, {E, Errors}}; {error, {FirstError, PreviousErrors}} -> {error, {FirstError, gleam@list:flatten([PreviousErrors, Errors])}} end end. keep(Accumulator, Value) -> case Accumulator of {error, Errors} -> {error, Errors}; {ok, Acc} -> {ok, Acc(Value)} end. custom(Error, Check) -> validator@common:custom(Error, Check). 'and'(Validator1, Validator2) -> fun(Input) -> gleam@result:then(Validator1(Input), Validator2) end. all(Validators) -> fun(Input) -> Results = gleam@list:map( Validators, fun(Validator) -> Validator(Input) end ), Errors = gleam@list:flatten( gleam@list:map(Results, fun(Result) -> case Result of {ok, _} -> []; {error, {First, Rest}} -> Rest end end) ), case gleam@list:head(Errors) of {error, nil} -> {ok, Input}; {ok, Head} -> {error, {Head, Errors}} end end. whole(Validator) -> fun(ValidationResult) -> gleam@result:then( ValidationResult, fun(Validated) -> gleam@result:map_error( Validator(Validated), fun(Error) -> {Error, [Error]} end ) end ) end.