Zot.Commons (zot v0.12.0)

View Source

Common 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

__using__(_)

(macro)

dump(value)

@spec dump(term()) :: nil | String.t() | number() | boolean() | list()

Renders a value to be used in JSON Schema.

maybe_examples(example)

If an example is given, wraps it in a list. Otherwise, returns nil.

maybe_nullable(type_name, bool)

@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.

validate_inclusion(value, list)

@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.

validate_length(value, constraints)

@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.

validate_number(value, constraints)

@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.

validate_range(value, range)

@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.

validate_regex(value, regex)

@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.

validate_type(value, list)

@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.

validate_with(value, validator)

@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.