-module(valguard). -compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch, inline]). -define(FILEPATH, "src/valguard.gleam"). -export([append_error/2, list/2, collect_errors/1, prepare/1, prepare_with/2, with/3, with_optional/3]). -export_type([validation_error/0]). -if(?OTP_RELEASE >= 27). -define(MODULEDOC(Str), -moduledoc(Str)). -define(DOC(Str), -doc(Str)). -else. -define(MODULEDOC(Str), -compile([])). -define(DOC(Str), -compile([])). -endif. -type validation_error() :: {validation_error, binary(), binary()}. -file("src/valguard.gleam", 15). ?DOC( " Takes a validation result and list of validation errors\n" " If result is error, it adds it to the list\n" ). -spec append_error( {ok, nil} | {error, validation_error()}, list(validation_error()) ) -> list(validation_error()). append_error(Result, List) -> case Result of {error, Validation_error} -> lists:append(List, [Validation_error]); {ok, nil} -> List end. -file("src/valguard.gleam", 83). ?DOC(" Inner recursive loop for list\n"). -spec list_inner( list(fun(() -> {ok, nil} | {error, binary()})), {ok, nil} | {error, binary()} ) -> {ok, nil} | {error, binary()}. list_inner(List, Prev) -> gleam@result:'try'(Prev, fun(_) -> case List of [] -> Prev; [Next | Rest] -> list_inner(Rest, Next()) end end). -file("src/valguard.gleam", 71). ?DOC( " Validates a list of requirements lazily.\n" "\n" " Takes a key and list of validation functions.\n" "\n" " Returns Ok(Nil) if success or Error(String) at first issue\n" ). -spec list(binary(), list(fun(() -> {ok, nil} | {error, binary()}))) -> {ok, nil} | {error, validation_error()}. list(Key, List) -> case list_inner(List, {ok, nil}) of {ok, nil} -> {ok, nil}; {error, Value} -> {error, {validation_error, Key, Value}} end. -file("src/valguard.gleam", 29). ?DOC( " Takes a list of validation results and returns a list of the errors\n" " Returns an empty list if no errors\n" ). -spec collect_errors(list({ok, nil} | {error, validation_error()})) -> list(validation_error()). collect_errors(Inputs) -> gleam@list:filter_map(Inputs, fun(X) -> case X of {ok, nil} -> {error, nil}; {error, Validation_error} -> {ok, Validation_error} end end). -file("src/valguard.gleam", 43). ?DOC( " Takes a list of validation errors and converts it to a result\n" " Returns ok if list is empty\n" ). -spec prepare(list(validation_error())) -> {ok, nil} | {error, list(validation_error())}. prepare(Errors) -> case gleam@list:is_empty(Errors) of true -> {ok, nil}; false -> {error, Errors} end. -file("src/valguard.gleam", 56). ?DOC( " Takes a list of validation errors and converts it to a result\n" " wrapping the errors in a custom type. Returns Ok(Nil) if list is empty.\n" "\n" " This functions similar to `valguard.prepare`.\n" ). -spec prepare_with( list(validation_error()), fun((list(validation_error())) -> EMI) ) -> {ok, nil} | {error, EMI}. prepare_with(Errors, Custom_type) -> case gleam@list:is_empty(Errors) of true -> {ok, nil}; false -> {error, Custom_type(Errors)} end. -file("src/valguard.gleam", 136). ?DOC(" Inner recursive loop for with\n"). -spec with_inner( list(fun((ENL) -> {ok, nil} | {error, binary()})), ENL, {ok, nil} | {error, binary()} ) -> {ok, nil} | {error, binary()}. with_inner(List, Value, Prev) -> gleam@result:'try'(Prev, fun(_) -> case List of [] -> Prev; [Next | Rest] -> with_inner(Rest, Value, Next(Value)) end end). -file("src/valguard.gleam", 101). ?DOC( " Validates a list of requirements lazily.\n" "\n" " Takes a key, value and list of validation functions.\n" "\n" " Runs validation functions lazily and returns the result.\n" " Returns Ok(Nil) if success or Error(String) at first issue\n" ). -spec with(binary(), EMY, list(fun((EMY) -> {ok, nil} | {error, binary()}))) -> {ok, nil} | {error, validation_error()}. with(Key, Value, List) -> case with_inner(List, Value, {ok, nil}) of {ok, nil} -> {ok, nil}; {error, Value@1} -> {error, {validation_error, Key, Value@1}} end. -file("src/valguard.gleam", 120). ?DOC( " Validates a list of requirements lazily.\n" "\n" " Takes a key, an optional value and list of validation functions.\n" "\n" " Runs validation functions lazily and returns the result.\n" " Returns Ok(Nil) if optional value is None or all validations are successful.\n" " Returns Error(String) at first issue.\n" ). -spec with_optional( binary(), gleam@option:option(ENE), list(fun((ENE) -> {ok, nil} | {error, binary()})) ) -> {ok, nil} | {error, validation_error()}. with_optional(Key, Option, List) -> valguard@internal@utils:some_or( Option, {ok, nil}, fun(Value) -> case with_inner(List, Value, {ok, nil}) of {ok, nil} -> {ok, nil}; {error, Value@1} -> {error, {validation_error, Key, Value@1}} end end ).