Skip to content

Commit

Permalink
feat: add Vx.Type.of/1
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 25, 2024
1 parent 41023d9 commit 97a928b
Showing 1 changed file with 15 additions and 2 deletions.
17 changes: 15 additions & 2 deletions lib/vx/type.ex
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ defmodule Vx.Type do

@type fun :: (any -> :ok | {:error, String.t()})

@type inner :: any

@type t(name) :: %__MODULE__{
name: name,
of: [any],
of: [inner],
fun: fun,
constraints: [Constraint.t()]
}
Expand All @@ -29,11 +31,22 @@ defmodule Vx.Type do
@doc """
Creates a new type.
"""
@spec new(name, [any], fun) :: t(name) when name: atom
@spec new(name, [inner], fun) :: t(name) when name: atom
def new(name, of \\ [], fun) when is_list(of) and is_function(fun, 1) do
%__MODULE__{name: name, of: of, fun: fun}
end

@doc """
Returns a list of inner types or schemata.
"""
@doc since: "0.5.0"
@spec of(t(atom) | custom(module, atom)) :: [inner]
def of(type) do
type
|> resolve()
|> Map.fetch!(:of)
end

@doc """
Adds a constraint to the type.
"""
Expand Down

0 comments on commit 97a928b

Please sign in to comment.