Skip to content

Commit 640f2fe

Browse files
authored
Merge pull request #309 from thalesmg/20250624-override-typespec
fix(tconf): `type` in `hocon_schema:override/2` is optional
2 parents 4c69e26 + 27c0574 commit 640f2fe

File tree

1 file changed

+43
-38
lines changed

1 file changed

+43
-38
lines changed

src/hocon_schema.erl

Lines changed: 43 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -115,43 +115,48 @@
115115
| field_schema_fun().
116116

117117
-type field_schema_fun() :: fun((_) -> _).
118-
-type field_schema_map() ::
119-
#{
120-
type := type(),
121-
default => term(),
122-
examples => term(),
123-
mapping => undefined | string(),
124-
converter => undefined | translationfunc(),
125-
validator => undefined | validationfun(),
126-
%% set false if a field is allowed to be `undefined`
127-
%% NOTE: has no point setting it to `true` if field has a default value
128-
129-
% default = false
130-
required => boolean() | {false, recursively},
131-
%% for sensitive data obfuscation (password, token)
132-
sensitive => boolean(),
133-
desc => desc(),
134-
%% hide it from doc generation
135-
importance => importance(),
136-
%% Set to {since, Version} to mark field as deprecated.
137-
%% deprecated field can not be removed due to compatibility reasons.
138-
%% The value will be dropped,
139-
%% Deprecated fields are treated as required => {false, recursively}
140-
deprecated => {since, binary() | string()} | false,
141-
%% Other names to reference this field.
142-
%% this can be useful when we need to rename some filed names
143-
%% while keeping backward compatibility.
144-
%% For one struct, no duplication is allowed in the collection of
145-
%% all field names and aliases.
146-
%% The no-duplication assertion is made when dumping the schema to JSON.
147-
%% see `hocon_schema_json'.
148-
%% When checking values against the schema, the look up is first
149-
%% done with the current field name, if not found, try the aliases
150-
%% in the defined order until one is found (i.e. first match wins).
151-
aliases => [name()],
152-
%% transparent metadata
153-
extra => map()
154-
}.
118+
119+
-define(FIELD_SCHEMA_MAP(TYPE_KV), #{
120+
TYPE_KV,
121+
default => term(),
122+
examples => term(),
123+
mapping => undefined | string(),
124+
converter => undefined | translationfunc(),
125+
validator => undefined | validationfun(),
126+
%% set false if a field is allowed to be `undefined`
127+
%% NOTE: has no point setting it to `true` if field has a default value
128+
129+
% default = false
130+
required => boolean() | {false, recursively},
131+
%% for sensitive data obfuscation (password, token)
132+
sensitive => boolean(),
133+
desc => desc(),
134+
%% hide it from doc generation
135+
importance => importance(),
136+
%% Set to {since, Version} to mark field as deprecated.
137+
%% deprecated field can not be removed due to compatibility reasons.
138+
%% The value will be dropped,
139+
%% Deprecated fields are treated as required => {false, recursively}
140+
deprecated => {since, binary() | string()} | false,
141+
%% Other names to reference this field.
142+
%% this can be useful when we need to rename some filed names
143+
%% while keeping backward compatibility.
144+
%% For one struct, no duplication is allowed in the collection of
145+
%% all field names and aliases.
146+
%% The no-duplication assertion is made when dumping the schema to JSON.
147+
%% see `hocon_schema_json'.
148+
%% When checking values against the schema, the look up is first
149+
%% done with the current field name, if not found, try the aliases
150+
%% in the defined order until one is found (i.e. first match wins).
151+
aliases => [name()],
152+
%% transparent metadata
153+
extra => map()
154+
}).
155+
156+
-type field_schema_map() :: ?FIELD_SCHEMA_MAP(type := type()).
157+
158+
%% Same as `field_schema_map/0`, but `type` is optional when overriding...
159+
-type field_schema_map_override() :: ?FIELD_SCHEMA_MAP(type => type()).
155160

156161
-type field() :: {name(), typefunc() | field_schema()}.
157162
-type fields() ::
@@ -216,7 +221,7 @@ validations(Sc) ->
216221
maps:get(validations, Sc, []).
217222

218223
%% @doc Make a higher order schema by overriding `Base' with `OnTop'
219-
-spec override(field_schema(), field_schema_map()) -> field_schema_fun().
224+
-spec override(field_schema(), field_schema_map_override()) -> field_schema_fun().
220225
override(Base, OnTop) ->
221226
fun(SchemaKey) ->
222227
case maps:is_key(SchemaKey, OnTop) of

0 commit comments

Comments
 (0)