Skip to content

Commit

Permalink
feat(schema): add option 'no_conversion' to hocon_schema:check_plain/3
Browse files Browse the repository at this point in the history
  • Loading branch information
terry-xiaoyu committed Aug 10, 2021
1 parent 6eee328 commit b86b108
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 1 deletion.
6 changes: 5 additions & 1 deletion src/hocon_schema.erl
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@
-define(IS_NON_EMPTY_STRING(X), (is_list(X) andalso X =/= [] andalso is_integer(hd(X)))).
-type loggerfunc() :: fun((atom(), map()) -> ok).
-type opts() :: #{ logger => loggerfunc()
, no_conversion => boolean()
, atom_key => boolean()
, return_plain => boolean()
%% By default allow all fields to be undefined.
Expand Down Expand Up @@ -476,7 +477,10 @@ map_field(Type, Schema, Value0, Opts) ->
ConvertedValue ->
Validators = add_default_validator(field_schema(Schema, validator), Type),
ValidationResult = validate(Opts, Schema, ConvertedValue, Validators),
{ValidationResult, boxit(Opts, ConvertedValue, Value0)}
case Opts of
#{no_conversion := true} -> {ValidationResult, Value0};
_ -> {ValidationResult, boxit(Opts, ConvertedValue, Value0)}
end
catch
C : E : St ->
{validation_errs(Opts, #{reason => converter_crashed,
Expand Down
15 changes: 15 additions & 0 deletions test/hocon_schema_tests.erl
Original file line number Diff line number Diff line change
Expand Up @@ -711,3 +711,18 @@ default_value_for_null_enclosing_struct_test() ->
hocon_schema:check_plain(Sc, PlainMap, #{nullable => true})),
?assertEqual(#{<<"l1">> => #{<<"l2">> => 22}},
hocon_schema:check(Sc, RichMap, #{nullable => true, return_plain => true})).

fill_defaults_test() ->
Sc = #{structs => ["a"],
fields => #{"a" =>
[ {b, hoconsc:t(integer(), #{default => 888})}
, {c, hoconsc:t(integer(), #{
default => "15s",
converter => fun (Dur) -> hocon_postprocess:duration(Dur) end})}
]}
},
?assertMatch(#{<<"a">> := #{<<"b">> := 888, <<"c">> := 15000}},
hocon_schema:check_plain(Sc, #{}, #{nullable => true})),
?assertMatch(#{<<"a">> := #{<<"b">> := 888, <<"c">> := "15s"}},
hocon_schema:check_plain(Sc, #{}, #{nullable => true, no_conversion => true})),
ok.

0 comments on commit b86b108

Please sign in to comment.