-module(ormlette_validate@valid). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]). -export([validate/2, validate_dict/2, is_not_empty/0, has_min_length/1, is_number/0, min/1, max/1, is_boolean/0, matches_regex/1, is_in_list/1, is_positive/0, is_negative/0]). -export_type([validator/1, field_validator/0]). -type validator(LON) :: {validator, fun((LON) -> {ok, LON} | {error, list(binary())})}. -type field_validator() :: {field_validator, binary(), validator(gleam@dynamic:dynamic_())}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 17). -spec validate(LOO, list(validator(LOO))) -> {ok, LOO} | {error, list(binary())}. validate(Input, Validators) -> gleam@list:fold(Validators, {ok, Input}, fun(Acc, V) -> case V of {validator, F} -> case Acc of {ok, Val} -> F(Val); {error, Errs} -> case F(Input) of {ok, _} -> Acc; {error, New_errs} -> {error, lists:append(Errs, New_errs)} end end end end). -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 37). -spec validate_dict( gleam@dict:dict(binary(), gleam@dynamic:dynamic_()), list(field_validator()) ) -> {ok, gleam@dict:dict(binary(), gleam@dynamic:dynamic_())} | {error, list(binary())}. validate_dict(Input, Field_validators) -> gleam@list:fold( Field_validators, {ok, Input}, fun(Acc, Field_validator) -> case Field_validator of {field_validator, Field, {validator, F}} -> case gleam@dict:get(Input, Field) of {ok, Value} -> case F(Value) of {ok, _} -> Acc; {error, New_errs} -> case Acc of {ok, _} -> {error, New_errs}; {error, Errs} -> {error, lists:append(Errs, New_errs)} end end; {error, _} -> Acc end end end ). -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 63). -spec is_not_empty() -> validator(gleam@dynamic:dynamic_()). is_not_empty() -> {validator, fun(Input) -> case gleam@dynamic:string(Input) of {ok, Value} -> case Value =:= <<""/utf8>> of true -> {error, [<<"Value cannot be empty"/utf8>>]}; false -> {ok, Input} end; {error, _} -> {error, [<<"Value is not a string"/utf8>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 77). -spec has_min_length(integer()) -> validator(gleam@dynamic:dynamic_()). has_min_length(Min_length) -> {validator, fun(Input) -> case gleam@dynamic:string(Input) of {ok, Value} -> case gleam@string:length(Value) < Min_length of true -> {error, [<<<<"Value must have at least "/utf8, (gleam@int:to_string(Min_length))/binary>>/binary, " characters"/utf8>>]}; false -> {ok, Input} end; {error, _} -> {error, [<<"Value is not a string"/utf8>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 96). -spec is_number() -> validator(gleam@dynamic:dynamic_()). is_number() -> {validator, fun(Input) -> case gleam@dynamic:string(Input) of {ok, Value} -> case gleam@int:parse(Value) of {ok, _} -> {ok, Input}; {error, _} -> {error, [<<"Value must be a number"/utf8>>]} end; {error, _} -> case gleam@dynamic:int(Input) of {ok, _} -> {ok, Input}; {error, _} -> {error, [<<"Value is not a number!"/utf8>>]} end end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 115). -spec min(integer()) -> validator(gleam@dynamic:dynamic_()). min(Num) -> {validator, fun(Input) -> Value_ok = gleam@dynamic:int(Input), case Value_ok of {ok, Value} -> case Value >= Num of true -> {ok, Input}; false -> {error, [<<"Value must be greater than or equal to "/utf8, (gleam@int:to_string(Num))/binary>>]} end; {error, _} -> {error, [<<"Not an int!"/utf8>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 133). -spec max(integer()) -> validator(gleam@dynamic:dynamic_()). max(Num) -> {validator, fun(Input) -> Value_ok = gleam@dynamic:int(Input), case Value_ok of {ok, Value} -> case Value =< Num of true -> {ok, Input}; false -> {error, [<<"Value must be less than or equal to "/utf8, (gleam@int:to_string(Num))/binary>>]} end; {error, _} -> {error, [<<"Not an int!"/utf8>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 149). -spec is_boolean() -> validator(gleam@dynamic:dynamic_()). is_boolean() -> {validator, fun(Input) -> case gleam@dynamic:bool(Input) of {ok, _} -> {ok, Input}; {error, _} -> {error, [<<"Value must be a boolean (true or false)"/utf8>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 158). -spec matches_regex(binary()) -> validator(gleam@dynamic:dynamic_()). matches_regex(Pattern) -> {validator, fun(Input) -> case gleam@dynamic:string(Input) of {ok, Value} -> _assert_subject = gleam@regex:from_string(Pattern), {ok, Regex_pattern} = case _assert_subject of {ok, _} -> _assert_subject; _assert_fail -> erlang:error(#{gleam_error => let_assert, message => <<"Pattern match failed, no pattern matched the value."/utf8>>, value => _assert_fail, module => <<"ormlette_validate/valid"/utf8>>, function => <<"matches_regex"/utf8>>, line => 162}) end, case gleam@regex:check(Regex_pattern, Value) of true -> {ok, Input}; false -> {error, [<<"Value does not match the required pattern: "/utf8, Pattern/binary>>]} end; {error, _} -> {error, [<<"Value is not a string"/utf8>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 174). -spec is_in_list(list(gleam@dynamic:dynamic_())) -> validator(gleam@dynamic:dynamic_()). is_in_list(Allowed_values) -> {validator, fun(Input) -> case gleam@list:contains(Allowed_values, Input) of true -> {ok, Input}; false -> {error, [<<"Value must be one of: "/utf8, (gleam@string:join( gleam@list:map( Allowed_values, fun(A) -> case gleam@dynamic:string(A) of {ok, V} -> V; {error, _} -> <<""/utf8>> end end ), <<","/utf8>> ))/binary>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 197). -spec is_positive() -> validator(gleam@dynamic:dynamic_()). is_positive() -> {validator, fun(Input) -> case gleam@dynamic:int(Input) of {ok, Value} -> case Value > 0 of true -> {ok, Input}; false -> {error, [<<"Value must be positive"/utf8>>]} end; {error, _} -> {error, [<<"Value is not a number"/utf8>>]} end end}. -file("/Users/ashercohen/Desktop/gleam/ormlette/src/ormlette_validate/valid.gleam", 211). -spec is_negative() -> validator(gleam@dynamic:dynamic_()). is_negative() -> {validator, fun(Input) -> case gleam@dynamic:int(Input) of {ok, Value} -> case Value < 0 of true -> {ok, Input}; false -> {error, [<<"Value must be positive"/utf8>>]} end; {error, _} -> {error, [<<"Value is not a number"/utf8>>]} end end}.