Improve normalization, add anchor support

This commit is contained in:
Ulf Wiger
2026-05-16 16:13:42 +02:00
parent 73944804c1
commit ea50e9e61a
2 changed files with 129 additions and 20 deletions
+13 -2
View File
@@ -41,6 +41,7 @@ schema_spec_examples_test_() ->
?t(t_ref_loop())
, ?t(t_recursive_def())
, ?t(t_nested_refs())
, ?t(t_anchors())
]}.
array() -> #{<<"type">> => <<"array">>}.
@@ -293,7 +294,8 @@ fails(V, S, Opts, Reason) when is_atom(Reason) ->
fails(V, S, Opts, #{e => Reason});
fails(V, S, Opts, Expect) ->
try validate(V, S, Opts) of
_ ->
Other ->
?debugFmt("Expected failure, Other = ~p", [Other]),
error({expected_exception, #{v => V,
s => S,
e => Expect}})
@@ -303,6 +305,8 @@ fails(V, S, Opts, Expect) ->
end.
%% ?assertError({Reason, [], V}, valid(V, S)).
match_expected('_', _) ->
ok;
match_expected(E, R) ->
case maps:fold(
fun(K, V, Acc) ->
@@ -350,7 +354,6 @@ all_fail(Vs, S, Reason) ->
read(F) ->
FullF = filename:join(
filename:dirname(code:which(?MODULE)), F),
?debugFmt("FullF = ~s~n", [FullF]),
{ok, Bin} = file:read_file(FullF),
dec(Bin).
@@ -402,3 +405,11 @@ t_nested_refs() ->
validate(Vs, S, Opts),
fails(Vf, S, Opts, #{e => failing_schemas}),
ok.
t_anchors() ->
S = read("data/anchors.json"),
validate(#{<<"person">> => #{ <<"name">> => <<"Ulf">>
, <<"age">> => 29 }}, S, #{}),
fails(#{<<"person">> => #{ <<"name">> => <<"Ulf">>
, <<"age">> => -17 }}, S, #{}, not_in_range),
ok.