Skip to content

Commit 9ebe970

Browse files
committed
fix: correct error messages for wrong_type and missing_required_property
jesse returns wrong_type and missing_required_property without a {ErrorType, Details} wrapper. For wrong_type, extract the expected type from the schema map. For missing_required_property, the property name is in the Value position, not Details.
1 parent 12bb70e commit 9ebe970

File tree

2 files changed

+34
-0
lines changed

2 files changed

+34
-0
lines changed

src/nova_json_schemas.erl

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,21 @@ render_one_error({data_invalid, _Schema, {ErrorType, Details}, Value, Path}) ->
180180
actual_value => safe_value(Value),
181181
expected => safe_value(Details)
182182
};
183+
render_one_error({data_invalid, Schema, wrong_type, Value, Path}) ->
184+
Expected = maps:get(<<"type">>, Schema, undefined),
185+
#{
186+
path => to_json_pointer(Path),
187+
type => wrong_type,
188+
message => format_error_message(wrong_type, Expected, Value),
189+
actual_value => safe_value(Value),
190+
expected => safe_value(Expected)
191+
};
192+
render_one_error({data_invalid, _Schema, missing_required_property, Property, Path}) ->
193+
#{
194+
path => to_json_pointer(Path),
195+
type => missing_required_property,
196+
message => format_error_message(missing_required_property, Property, undefined)
197+
};
183198
render_one_error({data_invalid, _Schema, ErrorType, Value, Path}) ->
184199
#{
185200
path => to_json_pointer(Path),

test/nova_json_schemas_tests.erl

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,25 @@ render_one_error_data_invalid_simple_test() ->
169169
?assertEqual(<<"foo">>, maps:get(actual_value, Result)),
170170
?assertNot(maps:is_key(expected, Result)).
171171

172+
render_one_error_wrong_type_from_schema_test() ->
173+
Schema = #{<<"type">> => <<"integer">>},
174+
Error = {data_invalid, Schema, wrong_type, <<"old">>, [<<"age">>]},
175+
Result = nova_json_schemas:render_one_error(Error),
176+
?assertEqual(<<"/age">>, maps:get(path, Result)),
177+
?assertEqual(wrong_type, maps:get(type, Result)),
178+
?assertEqual(<<"Must be of type integer">>, maps:get(message, Result)),
179+
?assertEqual(<<"old">>, maps:get(actual_value, Result)),
180+
?assertEqual(<<"integer">>, maps:get(expected, Result)).
181+
182+
render_one_error_missing_required_property_test() ->
183+
Schema = #{<<"required">> => [<<"name">>, <<"age">>]},
184+
Error = {data_invalid, Schema, missing_required_property, <<"age">>, []},
185+
Result = nova_json_schemas:render_one_error(Error),
186+
?assertEqual(<<"/">>, maps:get(path, Result)),
187+
?assertEqual(missing_required_property, maps:get(type, Result)),
188+
?assertEqual(<<"Missing required property: age">>, maps:get(message, Result)),
189+
?assertNot(maps:is_key(actual_value, Result)).
190+
172191
render_one_error_schema_invalid_with_details_test() ->
173192
Error = {schema_invalid, #{}, {missing_required_property, <<"id">>}},
174193
Result = nova_json_schemas:render_one_error(Error),

0 commit comments

Comments
 (0)