Zot.Commons (zot v0.12.0)
View SourceCommon functions used across type implementations.
Summary
Functions
Renders a value to be used in JSON Schema.
If an example is given, wraps it in a list. Otherwise, returns nil.
Return the a JSON Schema type that might be nullable.
Validates that the given value is included in the provided list of values.
Validates the length of a string, a list or a map against a set of constraints.
Validates a number against a set of constraints.
Validates that a value is within the given range.
Validates a string against a regex pattern.
Validates the type of a value against an expected type.
Validates the given value using a custom parameterized function.
Functions
Renders a value to be used in JSON Schema.
If an example is given, wraps it in a list. Otherwise, returns nil.
@spec maybe_nullable(type_name, required?) :: String.t() | [String.t(), ...] when type_name: String.t(), required?: boolean()
Return the a JSON Schema type that might be nullable.
@spec validate_inclusion(value, Zot.Parameterized.t([term(), ...]) | nil) :: :ok | {:error, [Zot.Issue.t(), ...]} when value: term()
Validates that the given value is included in the provided list of values.
@spec validate_length(value, [constraint, ...]) :: :ok | {:error, [Zot.Issue.t(), ...]} when value: String.t() | list() | map(), constraint: {:is, Zot.Parameterized.t(integer()) | nil} | {:min, Zot.Parameterized.t(integer()) | nil} | {:max, Zot.Parameterized.t(integer()) | nil}
Validates the length of a string, a list or a map against a set of constraints.
Constraints:
:is- validates the exact length;:min- validates the minimum length; and:max- validates the maximum length.
@spec validate_number(value, [constraint, ...]) :: :ok | {:error, [Zot.Issue.t(), ...]} when value: integer() | float() | Decimal.t(), constraint: {:is, Zot.Parameterized.t(integer() | float()) | nil} | {:min, Zot.Parameterized.t(integer() | float()) | nil} | {:max, Zot.Parameterized.t(integer() | float()) | nil}
Validates a number against a set of constraints.
Constraints:
:is- validates the exact value;:min- validates the minimum value; and:max- validates the maximum value.
@spec validate_range(value, Zot.Parameterized.t(Range.t()) | nil) :: :ok | {:error, [Zot.Issue.t(), ...]} when value: term()
Validates that a value is within the given range.
@spec validate_regex(value, Zot.Parameterized.t(Regex.t()) | nil) :: :ok | {:error, [Zot.Issue.t(), ...]} when value: String.t()
Validates a string against a regex pattern.
@spec validate_type(value, [constraint, ...]) :: :ok | {:error, [Zot.Issue.t(), ...]} when value: term(), constraint: {:is, String.t()}
Validates the type of a value against an expected type.
@spec validate_with(value, validator | nil) :: :ok | {:error, [Zot.Issue.t(), ...]} when value: term(), validator: Zot.Parameterized.t((term() -> boolean()))
Validates the given value using a custom parameterized function.