Vex.Validators.Uuid (Vex v0.9.2) View Source

Ensure a value is a valid UUID string.

Options

At least one of the following must be provided:

  • :format: Required. An atom that defines the UUID format of the value:
    • :default: The value must be a string with format xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx.
    • :hex: The value must be a string with the format xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx.
    • :urn: The value must be a string with the format urn:uuid:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxxx.
    • :any: The value must be a string of any of the supported formats (:default, :hex or :urn).
    • :not_any: The value must not be a valid UUID string.

Note: x is a hex number.

Optional:

  • :message: A custom error message. May be in EEx format and use the fields described in Custom Error Messages.
  • :allow_nil: A boolean whether to skip this validation for nil values.
  • :allow_blank: A boolean whether to skip this validaton for blank values.

The value for :format can be provided instead of the options keyword list. Additionally, if the options is a boolean value, then:

  • true: Is the same as the [format: :any] options.
  • false: Is the same as the [format: :not_any] options.

Examples

Examples when using the :any or true options:

iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a304", format: :any)
:ok
iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a30", format: :any)
{:error, "must be a valid UUID string"}

iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a304", true)
:ok
iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a30", true)
{:error, "must be a valid UUID string"}

Examples when using the :not_any or false options:

iex> Vex.Validators.Uuid.validate("not_a_uuid", format: :not_any)
:ok
iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a304", format: :not_any)
{:error, "must not be a valid UUID string"}

iex> Vex.Validators.Uuid.validate("not_a_uuid", false)
:ok
iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a304", false)
{:error, "must not be a valid UUID string"}

Examples when using the :default option:

iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a304", format: :default)
:ok
iex> Vex.Validators.Uuid.validate("02aa7f483ccd11e4b63e14109ff1a304", format: :default)
{:error, "must be a valid UUID string in default format"}

Examples when using the :hex option:

iex> Vex.Validators.Uuid.validate("02aa7f483ccd11e4b63e14109ff1a304", format: :hex)
:ok
iex> Vex.Validators.Uuid.validate("urn:uuid:02aa7f48-3ccd-11e4-b63e-14109ff1a304", format: :hex)
{:error, "must be a valid UUID string in hex format"}

Examples when using the :urn option:

iex> Vex.Validators.Uuid.validate("urn:uuid:02aa7f48-3ccd-11e4-b63e-14109ff1a304", format: :urn)
:ok
iex> Vex.Validators.Uuid.validate("02aa7f48-3ccd-11e4-b63e-14109ff1a304", format: :urn)
{:error, "must be a valid UUID string in urn format"}

Custom Error Messages

Custom error messages (in EEx format), provided as :message, can use the following values:

iex> Vex.Validators.Uuid.__validator__(:message_fields)
[value: "Bad value", format: "The UUID format"]

An example:

iex> Vex.Validators.Uuid.validate("not_a_uuid", format: :any,
...>                                            message: "<%= value %> should be <%= format %> UUID")
{:error, "not_a_uuid should be any UUID"}

Link to this section Summary

Functions

Callback implementation for c:Vex.Validator.Behaviour.validate/2.

Callback implementation for c:Vex.Validator.Behaviour.validate/3.

Link to this section Functions

Link to this function

validate(value, options)

View Source

Callback implementation for c:Vex.Validator.Behaviour.validate/2.

Link to this function

validate(data, context, options)

View Source

Callback implementation for c:Vex.Validator.Behaviour.validate/3.