[erlang-questions] Asserting exact maps

Danil Zagoskin z@REDACTED
Wed Oct 11 11:47:33 CEST 2017


Just to add one more option.
You may use maps:without to ensure the map does not have extra keys:

1> Check = fun(M) -> maps:without(["one", "two"], M) == #{} end.
#Fun<erl_eval.6.99386804>
2> Check(#{"one" => 1}).
true
3> Check(#{"one" => 1, "three" => 3}).
false

On Wed, Oct 11, 2017 at 11:47 AM, Technion <technion@REDACTED> wrote:

> Hi,
>
>
> I'm wondering if there is a simple process I can use to verify a map only
> contains valid variables. Consider the following example:
>
>
> 2> Checkmaps = fun(M) ->
> 2> #{"one" := One, "two" := _Two} = M,
> 2>     One end.
> #Fun<erl_eval.6.99386804>
>
> % These crash as expected
>
> 5> Checkmaps(#{"test" => one }).
> ** exception error: no match of right hand side value #{"test" => one}
> 6> Checkmaps(#{"one" => one }).
> ** exception error: no match of right hand side value #{"one" => one}
>
>
> % This works as expected
>
> 7> Checkmaps(#{"one" => one, "two" => two }).
> one
>
> % This however also runs - I would like it to crash like the first example
>
> 8> Checkmaps(#{"one" => one, "two" => two, "three" => test }).
>
> one
>
> The use case here is I'm pulling external data - anything I'm not
> expecting is not a happy path. Any assistance appreciated.
>
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>
>


-- 
Danil Zagoskin | z@REDACTED
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20171011/73e4ee07/attachment.htm>


More information about the erlang-questions mailing list