Packages

A Simple JSON Schema library for Gleam

Current section

Files

Jump to
jscheam src jscheam.erl
Raw

src/jscheam.erl

-module(jscheam).
-compile([no_auto_import, nowarn_unused_vars, nowarn_unused_function, nowarn_nomatch]).
-define(FILEPATH, "src/jscheam.gleam").
-export([prop/2, optional/1, description/2, string/0, integer/0, boolean/0, float/0, array/1, object/2, to_json/1]).
-if(?OTP_RELEASE >= 27).
-define(MODULEDOC(Str), -moduledoc(Str)).
-define(DOC(Str), -doc(Str)).
-else.
-define(MODULEDOC(Str), -compile([])).
-define(DOC(Str), -compile([])).
-endif.
-file("src/jscheam.gleam", 12).
?DOC(
" Creates a property with the specified name and type\n"
" Properties are required by default\n"
).
-spec prop(binary(), jscheam@property:type()) -> jscheam@property:property().
prop(Name, Property_type) ->
{property, Name, Property_type, true, none}.
-file("src/jscheam.gleam", 22).
?DOC(" Makes a property optional (not required in the schema)\n").
-spec optional(jscheam@property:property()) -> jscheam@property:property().
optional(Property) ->
_record = Property,
{property,
erlang:element(2, _record),
erlang:element(3, _record),
false,
erlang:element(5, _record)}.
-file("src/jscheam.gleam", 27).
?DOC(" Adds a description to a property for documentation purposes\n").
-spec description(jscheam@property:property(), binary()) -> jscheam@property:property().
description(Property, Desc) ->
_record = Property,
{property,
erlang:element(2, _record),
erlang:element(3, _record),
erlang:element(4, _record),
{some, Desc}}.
-file("src/jscheam.gleam", 33).
?DOC(" Creates a string type for JSON Schema\n").
-spec string() -> jscheam@property:type().
string() ->
string.
-file("src/jscheam.gleam", 38).
?DOC(" Creates an integer/number type for JSON Schema\n").
-spec integer() -> jscheam@property:type().
integer() ->
integer.
-file("src/jscheam.gleam", 43).
?DOC(" Creates a boolean type for JSON Schema\n").
-spec boolean() -> jscheam@property:type().
boolean() ->
boolean.
-file("src/jscheam.gleam", 48).
?DOC(" Creates a float/number type for JSON Schema\n").
-spec float() -> jscheam@property:type().
float() ->
float.
-file("src/jscheam.gleam", 53).
?DOC(" Creates an array type with the specified item type\n").
-spec array(jscheam@property:type()) -> jscheam@property:type().
array(Item_type) ->
{array, Item_type}.
-file("src/jscheam.gleam", 59).
?DOC(
" Creates an object type with the specified properties\n"
" Set additional_properties to True to allow additional properties beyond those defined\n"
).
-spec object(list(jscheam@property:property()), boolean()) -> jscheam@property:type().
object(Properties, Additional_properties) ->
{object, Properties, Additional_properties}.
-file("src/jscheam.gleam", 145).
-spec fields_to_required(list(jscheam@property:property())) -> gleam@json:json().
fields_to_required(Fields) ->
Required_fields = gleam@list:filter(
Fields,
fun(Property) ->
{property, _, _, Is_required, _} = Property,
Is_required
end
),
Names = gleam@list:map(
Required_fields,
fun(Property@1) ->
{property, Name, _, _, _} = Property@1,
gleam@json:string(Name)
end
),
gleam@json:array(Names, fun(X) -> X end).
-file("src/jscheam.gleam", 87).
-spec property_to_field(jscheam@property:property()) -> {binary(),
gleam@json:json()}.
property_to_field(Property) ->
{property, Name, Property_type, _, Description} = Property,
Base_schema = type_to_json_value(Property_type),
Schema_with_description = case Description of
{some, Desc} ->
case Base_schema of
_ ->
case Property_type of
string ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"string"/utf8>>)},
{<<"description"/utf8>>,
gleam@json:string(Desc)}]
);
integer ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"number"/utf8>>)},
{<<"description"/utf8>>,
gleam@json:string(Desc)}]
);
boolean ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"boolean"/utf8>>)},
{<<"description"/utf8>>,
gleam@json:string(Desc)}]
);
float ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"number"/utf8>>)},
{<<"description"/utf8>>,
gleam@json:string(Desc)}]
);
{array, Item_type} ->
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"array"/utf8>>)},
{<<"items"/utf8>>,
type_to_json_value(Item_type)},
{<<"description"/utf8>>,
gleam@json:string(Desc)}]
);
{object, Props, Add_props} ->
Properties_json = begin
_pipe = gleam@list:map(
Props,
fun property_to_field/1
),
gleam@json:object(_pipe)
end,
Required_json = fields_to_required(Props),
gleam@json:object(
[{<<"type"/utf8>>,
gleam@json:string(<<"object"/utf8>>)},
{<<"properties"/utf8>>, Properties_json},
{<<"required"/utf8>>, Required_json},
{<<"additionalProperties"/utf8>>,
gleam@json:bool(Add_props)},
{<<"description"/utf8>>,
gleam@json:string(Desc)}]
)
end
end;
none ->
Base_schema
end,
{Name, Schema_with_description}.
-file("src/jscheam.gleam", 63).
-spec type_to_json_value(jscheam@property:type()) -> gleam@json:json().
type_to_json_value(Property_type) ->
case Property_type of
string ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"string"/utf8>>)}]
);
integer ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"number"/utf8>>)}]
);
boolean ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"boolean"/utf8>>)}]
);
float ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"number"/utf8>>)}]
);
{object, Props, Add_props} ->
Properties_json = begin
_pipe = gleam@list:map(Props, fun property_to_field/1),
gleam@json:object(_pipe)
end,
Required_json = fields_to_required(Props),
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"object"/utf8>>)},
{<<"properties"/utf8>>, Properties_json},
{<<"required"/utf8>>, Required_json},
{<<"additionalProperties"/utf8>>,
gleam@json:bool(Add_props)}]
);
{array, Item_type} ->
gleam@json:object(
[{<<"type"/utf8>>, gleam@json:string(<<"array"/utf8>>)},
{<<"items"/utf8>>, type_to_json_value(Item_type)}]
)
end.
-file("src/jscheam.gleam", 162).
?DOC(
" Converts a Type to a JSON Schema document\n"
" This is the main function to generate JSON Schema from your type definitions\n"
).
-spec to_json(jscheam@property:type()) -> gleam@json:json().
to_json(Object_type) ->
type_to_json_value(Object_type).