nomasystems / ndto

:white_check_mark: An Erlang library for DTOs validation.
https://nomasystems.github.io/ndto/
Apache License 2.0
24 stars 1 forks source link

Non-required parameters are still required #95

Closed LoisSotoLopez closed 11 months ago

LoisSotoLopez commented 11 months ago

I'm not sure this is right.

❯ rebar3 shell
===> Verifying dependencies...
===> Analyzing applications...
===> Compiling ndto
Erlang/OTP 25 [erts-13.1.5] [source] [64-bit] [smp:10:10] [ds:10:10:10] [async-threads:1] [jit]
Eshell V13.1.5  (abort with ^G)
1> Schema = #{
1>     type => object,
1>     properties => #{
1>         <<"foo">> => #{
1>             type => string
1>         },
1>         <<"bar">> => #{
1>             type => integer
1>         }
1>     },
1>     required => [<<"foo">>]
1> },
1> DTO = ndto:generate(test_required, Schema),
1> ok = ndto:load(DTO),
1> test_required:is_valid(#{<<"foo">> => <<"foobar">>}).
false
2> merl:print(DTO).
%%% Copyright 2023 Nomasystems, S.L. http://www.nomasystems.com
%%
%% Licensed under the Apache License, Version 2.0 (the "License");
%% you may not use this file except in compliance with the License.
%% You may obtain a copy of the License at
%%
%%     http://www.apache.org/licenses/LICENSE-2.0
%%
%%  Unless required by applicable law or agreed to in writing, software
%%  distributed under the License is distributed on an "AS IS" BASIS,
%%  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
%% See the License for the specific language governing permissions and
%% limitations under the License
%%% NOTE: Module generated by ndto, do not modify this file.
-module(test_required).

%%% EXTERNAL EXPORTS
-export([is_valid/1]).

%%%-----------------------------------------------------------------------------
%%% EXTERNAL EXPORTS
%%%-----------------------------------------------------------------------------
is_valid(Val) -> is_valid_object(Val).

%%%-----------------------------------------------------------------------------
%%% INTERNAL FUNCTIONS
%%%-----------------------------------------------------------------------------
is_valid_object(Val) when is_map(Val) ->
    (true andalso is_valid_object_required(Val)) andalso
        is_valid_object_properties(Val);
is_valid_object(_Val) -> false.

is_valid_object_required(Val) ->
    lists:all(fun (Property) -> maps:is_key(Property, Val)
              end,
              [<<"foo">>]).

is_valid_object_properties(Val) ->
    (true andalso
         is_valid_object_properties_foo_string(maps:get(<<"foo">>,
                                                        Val,
                                                        undefined)))
        andalso
        is_valid_object_properties_bar_integer(maps:get(<<"bar">>,
                                                        Val,
                                                        undefined)).

is_valid_object_properties_foo_string(Val)
    when is_binary(Val) ->
    true;
is_valid_object_properties_foo_string(_Val) -> false.

is_valid_object_properties_bar_integer(Val)
    when is_integer(Val) ->
    true;
is_valid_object_properties_bar_integer(_Val) -> false.
ok
3>