Skip to content

Commit

Permalink
refactor: rename type to schema in places where it makes sense
Browse files Browse the repository at this point in the history
  • Loading branch information
tlux committed Mar 25, 2024
1 parent 0ae5b1e commit ff0be8c
Show file tree
Hide file tree
Showing 13 changed files with 94 additions and 91 deletions.
11 changes: 7 additions & 4 deletions lib/vx.ex
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,10 @@ defmodule Vx do

alias Vx.{Error, Validatable}

@type t :: Validatable.t()
@typedoc """
A Vx schema is anything that implements the `Vx.Validatable` protocol.
"""
@type schema :: Validatable.t()

@doc """
Validates a value against a given schema.
Expand All @@ -18,7 +21,7 @@ defmodule Vx do
iex> Vx.validate(Vx.String.t(), 123)
{:error, %Vx.Error{message: "must be a string", schema: Vx.String.t(), value: 123}}
"""
@spec validate(t, any) :: :ok | {:error, Error.t()}
@spec validate(schema, any) :: :ok | {:error, Error.t()}
def validate(schema, value) do
case Validatable.validate(schema, value) do
:ok ->
Expand All @@ -40,7 +43,7 @@ defmodule Vx do
iex> Vx.validate!(Vx.String.t(), 123)
** (Vx.Error) must be a string
"""
@spec validate!(t, any) :: :ok | no_return
@spec validate!(schema, any) :: :ok | no_return
def validate!(schema, value) do
with {:error, error} <- validate(schema, value) do
raise error
Expand All @@ -59,7 +62,7 @@ defmodule Vx do
false
"""
@doc since: "0.4.0"
@spec valid?(t, any) :: boolean
@spec valid?(schema, any) :: boolean
def valid?(schema, value) do
validate(schema, value) == :ok
end
Expand Down
2 changes: 1 addition & 1 deletion lib/vx/error.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Vx.Error do
defexception [:schema, :value, :message]

@type t :: %__MODULE__{
schema: Vx.t(),
schema: Vx.schema(),
value: any,
message: String.t()
}
Expand Down
4 changes: 2 additions & 2 deletions lib/vx/float.ex
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ defmodule Vx.Float do
** (Vx.Error) must be a float
"""
@spec integer(t) :: t
def integer(%__MODULE__{} = type \\ t()) do
Vx.Number.integer(type)
def integer(%__MODULE__{} = schema \\ t()) do
Vx.Number.integer(schema)
end
end
4 changes: 2 additions & 2 deletions lib/vx/intersect.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ defmodule Vx.Intersect do
@enforce_keys [:of]
defstruct [:of]

@type t :: t(nonempty_list(Vx.t()))
@type t :: t(nonempty_list(Vx.schema()))
@opaque t(of) :: %__MODULE__{of: of}

@doc """
Expand All @@ -21,7 +21,7 @@ defmodule Vx.Intersect do
iex> Vx.Intersect.t([Vx.Integer.t(), Vx.Number.t()]) |> Vx.validate!(12.3)
** (Vx.Error) must be all of (integer & number)
"""
@spec t(of) :: t(of) when of: nonempty_list(Vx.t())
@spec t(of) :: t(of) when of: nonempty_list(Vx.schema())
def t([_ | _] = of) do
%__MODULE__{of: of}
end
Expand Down
36 changes: 18 additions & 18 deletions lib/vx/list.ex
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,17 @@ defmodule Vx.List do
- element 1: must be a string
"""
@spec t(Vx.t()) :: t
def t(type) do
new([type], &check_inner_type(type, &1))
@spec t(Vx.schema()) :: t
def t(schema) do
new([schema], &check_inner_schema(schema, &1))
end

defp check_inner_type(type, values) when is_list(values) do
defp check_inner_schema(schema, values) when is_list(values) do
errors =
values
|> Enum.with_index()
|> Enum.flat_map(fn {value, index} ->
case Vx.Validatable.validate(type, value) do
case Vx.Validatable.validate(schema, value) do
:ok -> []
{:error, message} -> ["- element #{index}: #{message}"]
end
Expand All @@ -60,12 +60,12 @@ defmodule Vx.List do
:ok
else
{:error,
"must be a #{Vx.Inspectable.inspect(t(type))}\n" <>
"must be a #{Vx.Inspectable.inspect(t(schema))}\n" <>
Enum.join(errors, "\n")}
end
end

defp check_inner_type(_type, _values), do: {:error, "must be a list"}
defp check_inner_schema(_type, _values), do: {:error, "must be a list"}

@doc """
Requires the list to be non-empty.
Expand All @@ -79,17 +79,17 @@ defmodule Vx.List do
** (Vx.Error) must not be empty
"""
@spec non_empty(t) :: t
def non_empty(%__MODULE__{} = type \\ t()) do
constrain(type, :non_empty, fn
def non_empty(%__MODULE__{} = schema \\ t()) do
constrain(schema, :non_empty, fn
[] -> {:error, "must not be empty"}
_ -> :ok
end)
end

@spec size(t, non_neg_integer) :: t
def size(%__MODULE__{} = type \\ t(), size)
def size(%__MODULE__{} = schema \\ t(), size)
when is_integer(size) and size >= 0 do
constrain(type, :size, size, fn value ->
constrain(schema, :size, size, fn value ->
if length(value) == size do
:ok
else
Expand All @@ -102,9 +102,9 @@ defmodule Vx.List do
Requires the list to have a minimum size.
"""
@spec min_size(t, non_neg_integer) :: t
def min_size(%__MODULE__{} = type \\ t(), size)
def min_size(%__MODULE__{} = schema \\ t(), size)
when is_integer(size) and size >= 0 do
constrain(type, :min_size, size, fn value ->
constrain(schema, :min_size, size, fn value ->
if length(value) >= size do
:ok
else
Expand All @@ -117,9 +117,9 @@ defmodule Vx.List do
Requires the list to have a maximum size.
"""
@spec max_size(t, non_neg_integer) :: t
def max_size(%__MODULE__{} = type \\ t(), size)
def max_size(%__MODULE__{} = schema \\ t(), size)
when is_integer(size) and size >= 0 do
constrain(type, :max_size, size, fn value ->
constrain(schema, :max_size, size, fn value ->
if length(value) <= size do
:ok
else
Expand All @@ -144,9 +144,9 @@ defmodule Vx.List do
** (Vx.Error) must match [number, string]
- element 1: must be a string
"""
@spec shape(t, [Vx.t()]) :: t
def shape(%__MODULE__{} = type \\ t(), shape) when is_list(shape) do
constrain(type, :shape, shape, fn value ->
@spec shape(t, [Vx.schema()]) :: t
def shape(%__MODULE__{} = schema \\ t(), shape) when is_list(shape) do
constrain(schema, :shape, shape, fn value ->
value_size = length(value)
shape_size = length(shape)
max_size = max(value_size, shape_size)
Expand Down
10 changes: 5 additions & 5 deletions lib/vx/map.ex
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ defmodule Vx.Map do
** (Vx.Error) must be a map<atom, number>
- value of element :foo: must be a number
"""
@spec t(Vx.t(), Vx.t()) :: t
@spec t(Vx.schema(), Vx.schema()) :: t
def t(key_t, value_t) do
new([key_t, value_t], &check_map_of(&1, key_t, value_t))
end
Expand Down Expand Up @@ -98,9 +98,9 @@ defmodule Vx.Map do
** (Vx.Error) must have a size of 1
"""
@spec size(t, non_neg_integer) :: t
def size(%__MODULE__{} = type \\ t(), size)
def size(%__MODULE__{} = schema \\ t(), size)
when is_integer(size) and size >= 0 do
constrain(type, :size, size, fn value ->
constrain(schema, :size, size, fn value ->
if map_size(value) == size do
:ok
else
Expand Down Expand Up @@ -152,8 +152,8 @@ defmodule Vx.Map do
- key :b: must be a number
"""
@spec shape(t, map) :: t
def shape(%__MODULE__{} = type \\ t(), shape) when is_map(shape) do
constrain(type, :shape, shape, &check_shape(&1, shape))
def shape(%__MODULE__{} = schema \\ t(), shape) when is_map(shape) do
constrain(schema, :shape, shape, &check_shape(&1, shape))
end

defp check_shape(map, shape) do
Expand Down
4 changes: 2 additions & 2 deletions lib/vx/not.ex
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ defmodule Vx.Not do
@enforce_keys [:of]
defstruct [:of]

@type t :: t(Vx.t())
@type t :: t(Vx.schema())
@opaque t(of) :: %__MODULE__{of: of}

@doc """
Expand All @@ -20,7 +20,7 @@ defmodule Vx.Not do
iex> Vx.Not.t(Vx.Integer.t()) |> Vx.validate!(123)
** (Vx.Error) must not be integer
"""
@spec t(of) :: t(of) when of: Vx.t()
@spec t(of) :: t(of) when of: Vx.schema()
def t(of) do
%__MODULE__{of: of}
end
Expand Down
68 changes: 34 additions & 34 deletions lib/vx/number.ex
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,13 @@ defmodule Vx.Number do
iex> Vx.Number.integer() |> Vx.validate!("foo")
** (Vx.Error) must be a number
"""
@spec integer(type) :: type when type: numeric
def integer(type \\ t())
@spec integer(schema) :: schema when schema: numeric
def integer(schema \\ t())

def integer(%Vx.Integer{} = type), do: type
def integer(%Vx.Integer{} = schema), do: schema

def integer(type) do
constrain_num(type, :integer, fn
def integer(schema) do
constrain_num(schema, :integer, fn
value when is_integer(value) ->
:ok

Expand All @@ -69,9 +69,9 @@ defmodule Vx.Number do
Requires the number to be positive.
"""
@doc since: "0.3.0"
@spec positive(type) :: type when type: numeric
def positive(type \\ t()) do
constrain_num(type, :positive, fn value ->
@spec positive(schema) :: schema when schema: numeric
def positive(schema \\ t()) do
constrain_num(schema, :positive, fn value ->
if value > 0 do
:ok
else
Expand All @@ -84,9 +84,9 @@ defmodule Vx.Number do
Requires the number to be negative.
"""
@doc since: "0.3.0"
@spec negative(type) :: type when type: numeric
def negative(type \\ t()) do
constrain_num(type, :negative, fn value ->
@spec negative(schema) :: schema when schema: numeric
def negative(schema \\ t()) do
constrain_num(schema, :negative, fn value ->
if value < 0 do
:ok
else
Expand All @@ -98,9 +98,9 @@ defmodule Vx.Number do
@doc """
Requires the number to be greater than the given value.
"""
@spec gt(type, number) :: type when type: numeric
def gt(type \\ t(), value) when is_number(value) do
constrain_num(type, :gt, value, fn actual_value ->
@spec gt(schema, number) :: schema when schema: numeric
def gt(schema \\ t(), value) when is_number(value) do
constrain_num(schema, :gt, value, fn actual_value ->
if actual_value > value do
:ok
else
Expand All @@ -112,9 +112,9 @@ defmodule Vx.Number do
@doc """
Requires the number to be greater than or equal to the given value.
"""
@spec gteq(type, number) :: type when type: numeric
def gteq(type \\ t(), value) when is_number(value) do
constrain_num(type, :gteq, value, fn actual_value ->
@spec gteq(schema, number) :: schema when schema: numeric
def gteq(schema \\ t(), value) when is_number(value) do
constrain_num(schema, :gteq, value, fn actual_value ->
if actual_value >= value do
:ok
else
Expand All @@ -126,9 +126,9 @@ defmodule Vx.Number do
@doc """
Requires the number to be less than the given value.
"""
@spec lt(type, number) :: type when type: numeric
def lt(type \\ t(), value) when is_number(value) do
constrain_num(type, :lt, value, fn actual_value ->
@spec lt(schema, number) :: schema when schema: numeric
def lt(schema \\ t(), value) when is_number(value) do
constrain_num(schema, :lt, value, fn actual_value ->
if actual_value < value do
:ok
else
Expand All @@ -140,9 +140,9 @@ defmodule Vx.Number do
@doc """
Requires the number to be less than or equal to the given value.
"""
@spec lteq(type, number) :: type when type: numeric
def lteq(type \\ t(), value) when is_number(value) do
constrain_num(type, :lteq, value, fn actual_value ->
@spec lteq(schema, number) :: schema when schema: numeric
def lteq(schema \\ t(), value) when is_number(value) do
constrain_num(schema, :lteq, value, fn actual_value ->
if actual_value <= value do
:ok
else
Expand All @@ -154,16 +154,16 @@ defmodule Vx.Number do
@doc """
Requires the number to be within the given range.
"""
@spec between(type, number, number) :: type when type: numeric
def between(type \\ t(), first, last)
@spec between(schema, number, number) :: schema when schema: numeric
def between(schema \\ t(), first, last)

def between(type, last, first)
def between(schema, last, first)
when is_number(first) and is_number(last) and last > first do
range(type, first..last)
range(schema, first..last)
end

def between(type, first, last) when is_number(first) and is_number(last) do
range(type, first..last)
def between(schema, first, last) when is_number(first) and is_number(last) do
range(schema, first..last)
end

@doc """
Expand All @@ -177,9 +177,9 @@ defmodule Vx.Number do
iex> Vx.Number.range(1..10) |> Vx.validate!(11)
** (Vx.Error) must be in 1..10
"""
@spec range(type, Range.t()) :: type when type: numeric
def range(type \\ t(), _.._ = range) do
constrain_num(type, :range, range, fn actual_value ->
@spec range(schema, Range.t()) :: schema when schema: numeric
def range(schema \\ t(), _.._ = range) do
constrain_num(schema, :range, range, fn actual_value ->
if actual_value in range do
:ok
else
Expand All @@ -188,8 +188,8 @@ defmodule Vx.Number do
end)
end

defp constrain_num(%struct{} = type, name, value \\ nil, fun)
defp constrain_num(%struct{} = schema, name, value \\ nil, fun)
when struct in [__MODULE__, Vx.Float, Vx.Integer] do
Vx.Type.constrain(type, name, value, fun)
Vx.Type.constrain(schema, name, value, fun)
end
end
Loading

0 comments on commit ff0be8c

Please # to comment.