Add schema helpers

This commit is contained in:
Ulf Wiger 2025-03-07 16:46:10 +01:00
parent e553a2c338
commit afad9544bb
2 changed files with 89 additions and 1 deletions

View File

@ -0,0 +1,88 @@
-module(gmconfig_schema_helpers).
-export(
[
schema_init/0
]).
-export(
[
pos_int/1
, pos_int/2
, non_neg_int/0
, non_neg_int/1
, non_neg_int/2
, int/1
, int/2
, num/2
, bool/1
, bool/2
, str/1
, str/2
, obj/1
, obj/2
, array/1
, array/2
, key_value_pattern/1
, key_value_pattern/2
]).
-define(KEY_VALUE_PATTERN, <<"^[a-zA-Z0-9\\-_\\.]+\\h*:\\h*[0-9]+(\\h*,\\h*[a-zA-Z_]+\\h*:\\h*[0-9]+)*">>).
schema_init() ->
#{'$schema' => <<"http://json-schema.org/draft-04/schema#">> }.
pos_int(Def) when is_integer(Def) ->
#{type => integer, minimum => 1, default => Def};
pos_int(Opts) when is_map(Opts) ->
Opts#{type => integer, minimum => 1}.
pos_int(Def, Descr) when is_integer(Def) -> int(#{minimum => 1, default => Def}, Descr);
pos_int(Opts, Descr) when is_map(Opts) ->
int(Opts#{minimum => 1}, Descr).
non_neg_int() -> #{type => integer, minimum => 0}.
non_neg_int(Def) when is_integer(Def) -> #{type => integer, default => Def,
minimum => 0};
non_neg_int(Opts) when is_map(Opts) ->
int(Opts#{minimum => 0}).
non_neg_int(Def, Descr) when is_integer(Def) -> int(#{minimum => 0, default => Def}, Descr);
non_neg_int(Opts, Descr) when is_map(Opts) ->
int(Opts#{minimum => 0}, Descr).
int(Opts) when is_map(Opts) ->
Opts#{type => integer}.
int(Def, Descr) when is_integer(Def) -> int(#{default => Def}, Descr);
int(Opts, Descr) -> Opts#{type => integer, description => Descr}.
%% num(Opts) when is_map(Opts) ->
%% Opts#{type => number}.
num(Opts, Descr) -> Opts#{type => number, description => Descr}.
bool(B) when is_boolean(B) ->
#{type => boolean, default => B}.
bool(B, Descr) when is_boolean(B) ->
#{type => boolean, default => B, description => Descr};
bool(Opts, Descr) -> Opts#{type => boolean , description => Descr}.
str(Opts) -> Opts#{type => string}.
str(Opts, Descr) -> Opts#{type => string, description => Descr}.
obj(Props) -> #{type => object, additionalProperties => false, properties => Props}.
obj(Descr, Props) when is_binary(Descr) ->
obj(#{description => Descr}, Props);
obj(Opts, Props) ->
Def = #{additionalProperties => false},
maps:merge(Def, Opts#{type => object, properties => Props}).
array(Items) -> #{type => array, items => Items}.
array(Opts, Items) -> Opts#{type => array, items => Items}.
key_value_pattern(Def) when is_binary(Def) ->
str(#{default => Def, pattern => ?KEY_VALUE_PATTERN}).
key_value_pattern(Def, Descr) when is_binary(Def) ->
str(#{default => Def, pattern => ?KEY_VALUE_PATTERN, description => Descr}).

View File

@ -791,7 +791,7 @@ expand_schema(S, S0) when is_map(S) ->
%% https://json-schema.org/understanding-json-schema/structuring#dollarref
%% When $id is used in a subschema, it indicates an embedded schema.
%% The identifier for the embedded schema is the value of $id
%%resolved against the Base URI of the schema it appears in.
%% resolved against the Base URI of the schema it appears in.
%% A schema document that includes embedded schemas is called a
%% Compound Schema Document. Each schema with an $id in a
%% Compound Schema Document is called a Schema Resource.