I have no clue why I exactly get this error or more like how I exactly use the typedex_type macro in my example.
here is my example code:
defmodule UserSchema do
use Tipx
@typedex_type [
name: :string,
age: :integer,
email: :string
]
end
defmodule UserProcessor do
def process_user_data(data) do
validated_data = validate_user(data)
# Process the validated data
IO.inspect(validated_data, label: "Validated User Data")
end
defp validate_user(data) do
validate_type(data, UserSchema)
end
end
user_data = %{
name: "John Doe",
age: 30,
email: "john@example.com"
}
UserProcessor.process_user_data(user_data)
error stack:
== Compilation error in file lib/user_schema.ex ==
** (UndefinedFunctionError) function Tipx.__before_compile__/1 is undefined or private
Tipx.__before_compile__(#Macro.Env<aliases: [], context: nil, context_modules: [UserSchema], file: "c:/tipx/lib/user_schema.ex", function: nil, functions: [{Kernel, [!=: 2, !==: 2, *: 2, **: 2, +: 1, +: 2, ++: 2, -: 1, -: 2, --: 2, /: 2, <: 2, <=: 2, ==: 2, ===: 2, =~: 2, >: 2, >=: 2, abs: 1, apply: 2, apply: 3, binary_part: 3, binary_slice: 2, binary_slice: 3, bit_size: 1, byte_size: 1, ceil: 1, div: 2, elem: 2, exit: 1, floor: 1, function_exported?: 3, get_and_update_in: 3, get_in: 2, hd: 1, inspect: 1, inspect: 2, is_atom: 1, is_binary: 1, is_bitstring: 1, is_boolean: 1, ...]}], lexical_tracker: #PID<0.117.0>, line: 1, macro_aliases: [], macros: [{Kernel, [!: 1, &&: 2, ..: 0, ..: 2, ..//: 3, <>: 2, @: 1, alias!: 1, and: 2, binding: 0, binding: 1, dbg: 0, dbg: 1, dbg: 2, def: 1, def: 2, defdelegate: 2, defexception: 1, defguard: 1, defguardp: 1, defimpl: 2, defimpl: 3, defmacro: 1, defmacro: 2, defmacrop: 1, defmacrop: 2, defmodule: 2, defoverridable: 1, defp: 1, defp: 2, defprotocol: 2, defstruct: 1, destructure: 2, get_and_update_in: 2, if: 2, in: 2, is_exception: 1, ...]}], module: UserSchema, requires: [Application, Kernel, Kernel.Typespec, Tipx], ...>)
(stdlib 5.0.2) lists.erl:1594: :lists.foldl/3
lib/user_schema.ex:1: (file)
whereas here is my main module code:
defmodule Tipx do
defmacro typedex_type(schema) when is_list(schema) do
quote do
@typedex_annotation unquote(schema)
end
end
defmacro __using__(_) do
quote do
@before_compile unquote(__MODULE__)
end
end
defmacro validate_type(data, schema) do
quote do
case Tipx.validate(unquote(data), unquote(schema)) do
{:ok, validated_data} -> validated_data
{:error, errors} -> raise RuntimeError, "Type validation error: #{inspect(errors)}"
end
end
end
defmacro generate(schema) do
quote do
Tipx.generate(unquote(schema))
end
end
def validate(data, schema) when is_map(data) and is_map(schema),
do: validate_map(data, schema, [])
defp validate_map(data, schema, path) when is_list(schema) do
do_validate_map(data, schema, path)
end
defp validate_map(_, _, _), do: {:error, [{"root", "invalid schema"}]}
defp do_validate_map(data, schema, path) do
Enum.reduce(schema, {:ok, %{}}, fn {key, type}, {:ok, acc} ->
case Map.get(data, key) do
nil ->
{:error, [{path ++ [key], "missing"}]}
value ->
case Tipx.validate(value, type) do
{:ok, validated_value} -> {:ok, Map.put(acc, key, validated_value)}
{:error, errors} -> {:error, merge_errors(errors, path ++ [key])}
end
end
end)
end
defp merge_errors(errors, path) do
Enum.map(errors, fn {sub_path, error} -> {path ++ sub_path, error} end)
end
end