[erlang-questions] Asserting exact maps

Attila Rajmund Nohl attila.r.nohl@REDACTED
Wed Oct 11 10:50:06 CEST 2017


2017-10-11 10:47 GMT+02:00 Technion <technion@REDACTED>:
> 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.

Check also the size of the map. If it's greater than expected, it has
extra elements.



More information about the erlang-questions mailing list