···
|> then(&Recase.Enumerable.atomize_keys/1)
|> then(&Atex.Lexicon.Schema.lexicon!/1)
-
# TODO: support returning typedefs
|> Enum.flat_map(fn {def_name, def} -> def_to_schema(lexicon.id, def_name, def) end)
-
|> Enum.map(fn {schema_key, quoted_schema} ->
defschema unquote(schema_key), unquote(quoted_schema)
···
-
# TODO: generate typedefs
@spec def_to_schema(nsid :: String.t(), def_name :: String.t(), lexicon_def :: map()) ::
-
list({key :: atom(), quoted :: term()})
defp def_to_schema(nsid, def_name, %{type: "record", record: record}) do
# TODO: record rkey format validator
def_to_schema(nsid, def_name, record)
···
|> Enum.map(fn {key, field} ->
-
field_to_schema(field, nsid)
-
&if key in nullable, do: quote(do: {:either, {{:literal, nil}, unquote(&1)}}), else: &1
-
|> then(&if key in required, do: quote(do: {:required, unquote(&1)}), else: &1)
-
|> then(&{:%{}, [], &1})
-
|> then(&[{atomise(def_name), &1}])
# TODO: validating errors?
···
defp def_to_schema(_nsid, def_name, %{type: "token"}) do
# TODO: make it a validator that expects the nsid + key.
-
[{atomise(def_name), :string}]
defp def_to_schema(nsid, def_name, %{type: type} = def)
···
-
[{atomise(def_name), field_to_schema(def, nsid)}]
-
@spec field_to_schema(field_def :: %{type: String.t()}, nsid :: String.t()) :: Peri.schema_def()
defp field_to_schema(%{type: "string"} = field, _nsid) do
fixed_schema = const_or_enum(field)
···
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
|> then(&{:custom, {Validators.String, :validate, [&1]}})
-
|> then(&Macro.escape/1)
defp field_to_schema(%{type: "boolean"} = field, _nsid) do
(const(field) || :boolean)
-
|> then(&Macro.escape/1)
defp field_to_schema(%{type: "integer"} = field, _nsid) do
···
|> then(&{:custom, {Validators.Integer, [&1]}})
-
|> then(&Macro.escape/1)
defp field_to_schema(%{type: "array", items: items} = field, nsid) do
-
inner_schema = field_to_schema(items, nsid)
|> Map.take([:maxLength, :minLength])
···
{inner_schema, _} = Code.eval_quoted(quoted_inner_schema)
{:custom, {:{}, c, [Validators.Array, :validate, [inner_schema | args]]}}
defp field_to_schema(%{type: "blob"} = field, _nsid) do
···
|> Map.take([:accept, :maxSize])
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
-
|> then(&Macro.escape/1)
defp field_to_schema(%{type: "bytes"} = field, _nsid) do
···
|> Map.take([:maxLength, :minLength])
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
-
|> then(&Macro.escape/1)
defp field_to_schema(%{type: "cid-link"}, _nsid) do
-
|> then(&Macro.escape/1)
# TODO: do i need to make sure these two deal with brands? Check objects in atp.tools
···
|> Atex.NSID.expand_possible_fragment_shorthand(ref)
|> Atex.NSID.to_atom_with_fragment()
-
unquote(nsid).get_schema(unquote(fragment))
defp field_to_schema(%{type: "union", refs: refs}, nsid) do
···
|> Atex.NSID.expand_possible_fragment_shorthand(ref)
|> Atex.NSID.to_atom_with_fragment()
-
unquote(nsid).get_schema(unquote(fragment))
# TODO: apparently should be a data object, not a primitive?
defp field_to_schema(%{type: "unknown"}, _nsid) do
-
defp field_to_schema(_field_def, _nsid), do: nil
defp maybe_default(schema, field) do
if field[:default] != nil,
···
defp atomise(x) when is_atom(x), do: x
defp atomise(x) when is_binary(x), do: String.to_atom(x)
···
|> then(&Recase.Enumerable.atomize_keys/1)
|> then(&Atex.Lexicon.Schema.lexicon!/1)
|> Enum.flat_map(fn {def_name, def} -> def_to_schema(lexicon.id, def_name, def) end)
+
|> Enum.map(fn {schema_key, quoted_schema, quoted_type} ->
+
if schema_key === :main do
+
@type t() :: unquote(quoted_type)
+
@type unquote(schema_key)() :: unquote(quoted_type)
defschema unquote(schema_key), unquote(quoted_schema)
···
@spec def_to_schema(nsid :: String.t(), def_name :: String.t(), lexicon_def :: map()) ::
+
list({key :: atom(), quoted_schema :: term(), quoted_type :: term()})
defp def_to_schema(nsid, def_name, %{type: "record", record: record}) do
# TODO: record rkey format validator
def_to_schema(nsid, def_name, record)
+
# TODO: add `$type` field. It's just a string though.
···
|> Enum.map(fn {key, field} ->
+
{quoted_schema, quoted_type} = field_to_schema(field, nsid)
+
is_nullable = key in nullable
+
is_required = key in required
+
&if is_nullable, do: quote(do: {:either, {{:literal, nil}, unquote(&1)}}), else: &1
+
|> then(&if is_required, do: quote(do: {:required, unquote(&1)}), else: &1)
+
key_type = if is_required, do: :required, else: :optional
+
|> then(&{{key_type, [], [key]}, &1})
+
{quoted_schema, quoted_type}
+
|> Enum.reduce({[], []}, fn {quoted_schema, quoted_type}, {schemas, types} ->
+
{[quoted_schema | schemas], [quoted_type | types]}
+
|> then(fn {quoted_schemas, quoted_types} ->
+
[{atomise(def_name), {:%{}, [], quoted_schemas}, {:%{}, [], quoted_types}}]
# TODO: validating errors?
···
defp def_to_schema(_nsid, def_name, %{type: "token"}) do
# TODO: make it a validator that expects the nsid + key.
defp def_to_schema(nsid, def_name, %{type: type} = def)
···
+
{quoted_schema, quoted_type} = field_to_schema(def, nsid)
+
[{atomise(def_name), quoted_schema, quoted_type}]
+
@spec field_to_schema(field_def :: %{type: String.t()}, nsid :: String.t()) ::
+
{quoted_schema :: term(), quoted_typespec :: term()}
defp field_to_schema(%{type: "string"} = field, _nsid) do
fixed_schema = const_or_enum(field)
···
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
|> then(&{:custom, {Validators.String, :validate, [&1]}})
defp field_to_schema(%{type: "boolean"} = field, _nsid) do
(const(field) || :boolean)
defp field_to_schema(%{type: "integer"} = field, _nsid) do
···
|> then(&{:custom, {Validators.Integer, [&1]}})
+
# TODO: turn into range definition based on maximum/minimum
defp field_to_schema(%{type: "array", items: items} = field, nsid) do
+
{inner_schema, inner_type} = field_to_schema(items, nsid)
|> Map.take([:maxLength, :minLength])
···
{inner_schema, _} = Code.eval_quoted(quoted_inner_schema)
{:custom, {:{}, c, [Validators.Array, :validate, [inner_schema | args]]}}
+
list(unquote(inner_type))
defp field_to_schema(%{type: "blob"} = field, _nsid) do
···
|> Map.take([:accept, :maxSize])
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
defp field_to_schema(%{type: "bytes"} = field, _nsid) do
···
|> Map.take([:maxLength, :minLength])
|> Enum.map(fn {k, v} -> {Recase.to_snake(k), v} end)
defp field_to_schema(%{type: "cid-link"}, _nsid) do
# TODO: do i need to make sure these two deal with brands? Check objects in atp.tools
···
|> Atex.NSID.expand_possible_fragment_shorthand(ref)
|> Atex.NSID.to_atom_with_fragment()
+
unquote(nsid).get_schema(unquote(fragment))
+
unquote(nsid).unquote(fragment)()
defp field_to_schema(%{type: "union", refs: refs}, nsid) do
···
|> Atex.NSID.expand_possible_fragment_shorthand(ref)
|> Atex.NSID.to_atom_with_fragment()
+
unquote(nsid).get_schema(unquote(fragment))
+
unquote(nsid).unquote(fragment)()
+
|> Enum.reduce({[], []}, fn {quoted_schema, quoted_type}, {schemas, types} ->
+
{[quoted_schema | schemas], [quoted_type | types]}
+
|> then(fn {schemaa, types} ->
+
{:oneof, unquote(schemaa)}
+
unquote(join_with_pipe(types))
# TODO: apparently should be a data object, not a primitive?
defp field_to_schema(%{type: "unknown"}, _nsid) do
+
defp field_to_schema(_field_def, _nsid), do: {nil, nil}
defp maybe_default(schema, field) do
if field[:default] != nil,
···
defp atomise(x) when is_atom(x), do: x
defp atomise(x) when is_binary(x), do: String.to_atom(x)
+
defp join_with_pipe(list) when is_list(list) do
+
[piped] = do_join_with_pipe(list)
+
defp do_join_with_pipe([head]), do: [head]
+
defp do_join_with_pipe([head | tail]), do: [{:|, [], [head | do_join_with_pipe(tail)]}]
+
defp do_join_with_pipe([]), do: []