zz - composable schema validation for Erlang, inspired by Zod

Hi!

I just released the first public version of my little library zz. It’s a composable schema validator library written in pure Erlang, very much inspired by Zod (for TypeScript).

I was surprised that I couldn’t find any similar existing library in Erlang. Here’s a simple example of how it can be used:

create_user_schema() ->
    zz:map(
        #{
            <<"username">> => zz:binary(#{min => 3, max => 32, regex => "^[a-z0-9_]+$"}),
            <<"email">>    => zz:binary(#{regex => "@"}),
            <<"age">>      => zz:optional(zz:integer(#{min => 13, max => 120})),
            <<"role">>     => zz:enum([<<"admin">>, <<"member">>, <<"guest">>]),
            <<"tags">>     => zz:list(zz:binary(), #{max => 10})
        },
        #{unknown_keys => strict}
    ).

handle_create_user(Body) ->
    case zz:parse(create_user_schema(), Body) of
        {ok, Validated} ->
            store_user(Validated);
        {error, Errors} ->
            {bad_request, zz:issues(Errors)}
    end.

%% Example:
%% handle_create_user(#{<<"username">> => <<"x">>,
%%                      <<"email">>    => 42,
%%                      <<"role">>     => <<"superadmin">>}).
%% {bad_request, [
%%     #{path => [<<"username">>], code => binary_too_short},
%%     #{path => [<<"email">>],    code => not_binary},
%%     #{path => [<<"role">>],     code => not_in_enum},
%%     #{path => [<<"tags">>],     code => missing_key}
%% ]}

Available on Hex: zz | Hex

Here is the GitHub repo:

Try it on real schemas — bug reports welcome on GitHub! :slight_smile:

3 Likes