View Source Xema.Format (xema v0.17.5)

This module contains semantic validators for strings.

Summary

Types

The list of supported validators.

Functions

Checks if the string is a valid date representation.

Checks if the string is a valid date time representation.

Checks if the string is a valid email representation.

Checks if the string is a valid host representation.

Checks if the string is a valid hostname representation.

Checks if the string is a valid IPv4 address representation.

Checks if the string is a valid IPv6 address representation.

Checks if the value matches the given type. The function expected a available format and a string to check. Returns true for a valid string, false otherwise.

Checks if the string is a valid JSON pointer representation.

Return true if string contains a regular expression.

Checks if the string is a valid JSON pointer representation.

Returns true for a valid format type, false otherwise. This macro can be used in guards.

Checks if the string is a valid time representation.

Checks if the string is a valid URI representation.

Checks if the string is a valid URI fragment representation.

Checks if the string is a valid URI path representation.

Checks if the string is a valid URI query representation.

Checks if the string is a valid URI reference representation.

Checks if the string is a valid URI template representation.

Checks if the string is a valid URI template path representation.

Checks if the string is a valid URI user info.

Types

format()

@type format() ::
  :date
  | :date_time
  | :email
  | :hostname
  | :ipv4
  | :ipv6
  | :json_pointer
  | :regex
  | :relative_json_pointer
  | :time
  | :uri
  | :uri_fragment
  | :uri_path
  | :uri_query
  | :uri_reference
  | :uri_template
  | :uri_userinfo

The list of supported validators.

Functions

date?(string)

@spec date?(String.t()) :: boolean()

Checks if the string is a valid date representation.

This function returns true if the value is a string and is formatted as defined by RFC 3339, false otherwise.

date_time?(string)

@spec date_time?(String.t()) :: boolean()

Checks if the string is a valid date time representation.

This function returns true if the value is a string and is formatted as defined by RFC 3339, false otherwise.

email?(string)

@spec email?(String.t()) :: boolean()

Checks if the string is a valid email representation.

This function returns true if the value is a string and is formatted as defined by RFC 5322, false otherwise.

The regular expression was taken from https://emailregex.com/.

Examples

iex> import Xema.Format
...> email?("marin.musterman@germany.net")
true
...> email?("Otto.Normalverbraucher")
false
...> email?("Otto.Normal@Verbraucher.NET")
true
...> email?("müller.aßa@foo.de")
true
...> email?("réñe@foo.de")
true

host?(string)

@spec host?(String.t()) :: boolean()

Checks if the string is a valid host representation.

This function returns true if the value is a valid IPv4 address, IPv6 address, or a valid hostname, false otherwise.

Examples

iex> import Xema.Format
iex>
iex> host?("127.0.0.1")
true
iex> host?("localhost")
true
iex> host?("elixirforum.com")
true
iex> host?("go go go")
false

hostname?(string)

@spec hostname?(String.t()) :: boolean()

Checks if the string is a valid hostname representation.

This function returns true if the value is a string and is formatted as defined by RFC 1034, false otherwise.

ipv4?(string)

@spec ipv4?(String.t()) :: boolean()

Checks if the string is a valid IPv4 address representation.

This function returns true if the value is a string and is formatted as defined by RFC 2673, false otherwise.

ipv6?(string)

@spec ipv6?(String.t()) :: boolean()

Checks if the string is a valid IPv6 address representation.

This function returns true if the value is a string and is formatted as defined by RFC 2373, false otherwise.

is?(atom, string)

@spec is?(format(), String.t()) :: boolean()

Checks if the value matches the given type. The function expected a available format and a string to check. Returns true for a valid string, false otherwise.

Examples

iex> Xema.Format.is?(:email, "foo@bar.net")
true
iex> Xema.Format.is?(:email, "foo.bar.net")
false

json_pointer?(string)

@spec json_pointer?(String.t()) :: boolean()

Checks if the string is a valid JSON pointer representation.

regex?(string)

@spec regex?(String.t()) :: boolean()

Return true if string contains a regular expression.

relative_json_pointer?(string)

@spec relative_json_pointer?(String.t()) :: boolean()

Checks if the string is a valid JSON pointer representation.

supports(format)

(macro)

Returns true for a valid format type, false otherwise. This macro can be used in guards.

time?(string)

@spec time?(String.t()) :: boolean()

Checks if the string is a valid time representation.

This function returns true if the value is a string and is formatted as defined by RFC 3339, false otherwise.

uri?(string)

@spec uri?(String.t()) :: boolean()

Checks if the string is a valid URI representation.

This function returns true if the value is a string and is formatted as defined by RFC 3986, false otherwise.

The following are two example URIs and their component parts:

                      hierarchical part
                            |
        |-----------------------------------------|
                    authority               path
                        |                    |
        |-------------------------------||--------|
  abc://username:password@example.com:123/path/data?key=value#fragid1
  |-|   |---------------| |---------| |-|           |-------| |-----|
   |            |              |       |                |        |
scheme  user information     host     port            query   fragment

  urn:example:mammal:monotreme:echidna
  |-| |------------------------------|
   |                 |
scheme              path

Wikipedia: Uniform Resource Identifier

uri_fragment?(string)

@spec uri_fragment?(String.t()) :: boolean()

Checks if the string is a valid URI fragment representation.

uri_path?(string)

@spec uri_path?(String.t()) :: boolean()

Checks if the string is a valid URI path representation.

See also Xema.Format.uri?/1.

uri_query?(string)

@spec uri_query?(String.t()) :: boolean()

Checks if the string is a valid URI query representation.

uri_reference?(string)

@spec uri_reference?(String.t()) :: boolean()

Checks if the string is a valid URI reference representation.

uri_template?(string)

Checks if the string is a valid URI template representation.

uri_template_path?(string)

@spec uri_template_path?(String.t()) :: boolean()

Checks if the string is a valid URI template path representation.

uri_userinfo?(string)

@spec uri_userinfo?(String.t()) :: boolean()

Checks if the string is a valid URI user info.

See also Xema.Format.uri?/1.