View Source Drops.Types.Map.DSL (drops v0.1.0)

DSL functions for defining map key and value type specifications.

Functions from this module are typically used via Drops.Contract.schema/1

Summary

Functions

Returns :any type specification.

Returns a boolean type specification.

Returns a type cast specification.

Returns a float type specification.

Returns a float type specification with additional constraints.

Returns an integer type specification.

Returns an integer type specification with additional constraints.

Returns a list type specification.

Returns a list type specification with a constrained member type

Returns a map type specification.

Returns a map type specification with additional constraints.

Returns a maybe type specification.

Returns a maybe type specification with additional constraints.

Returns an optional key specification.

Returns a required key specification.

Returns a string type specification.

Returns a string type specification with additional constraints.

Returns a type specification.

Returns a type specification with additional constraints.

Types

@type type() :: {:type, {atom(), keyword()}}

Functions

@spec any() :: type()

Returns :any type specification.

Examples

any()
@spec boolean() :: type()

Returns a boolean type specification.

Examples

# a boolean with no constraints
boolean()
Link to this function

cast(type, cast_opts \\ [])

View Source (since 0.1.0)
@spec cast(type(), Keyword.t()) :: {:cast, {type(), Keyword.t()}}

Returns a type cast specification.

Examples

# cast a string to an integer
cast(:string) |> integer()

# cast a string to an integer with additional constraints
cast(string(match?: ~r/\d+/])) |> integer()
@spec float() :: type()

Returns a float type specification.

Examples

# a float with no constraints
float()
Link to this function

float(predicates)

View Source (since 0.1.0)
@spec float([]) :: type()

Returns a float type specification with additional constraints.

Examples

# a float with constraints
float(gt?: 1.0)
Link to this function

float(cast_spec, predicates \\ [])

View Source
@spec integer() :: type()

Returns an integer type specification.

Examples

# an integer with no constraints
integer()
Link to this function

integer(predicate)

View Source (since 0.1.0)
@spec integer(atom()) :: type()
@spec integer([]) :: type()

Returns an integer type specification with additional constraints.

Examples

# an integer with constraints
integer(:even?)

# an integer with multiple constraints
integer([:even?, gt?: 100])
Link to this function

integer(cast_spec, predicates \\ [])

View Source
Link to this function

list(members)

View Source (since 0.1.0)
@spec list([atom()]) :: type()
@spec list([atom()]) :: type()

Returns a list type specification.

Examples

# a list with a specified member type
list(:string)

# a list with a specified sum member type
list([:string, :integer])
Link to this function

list(type, predicates \\ [])

View Source (since 0.1.0)

Returns a list type specification with a constrained member type

Examples

# a list with a specified member type
list(:string, [:filled?])

list(:integer, [gt?: 18])
@spec map() :: type()

Returns a map type specification.

Examples

# a map with no constraints
map()
Link to this function

map(predicate)

View Source (since 0.1.0)
@spec map(atom()) :: type()
@spec map([]) :: type()

Returns a map type specification with additional constraints.

Examples

# a map with constraints
map(min_size?: 2)
Link to this function

maybe(schema)

View Source (since 0.1.0)
@spec maybe(atom()) :: type()
@spec maybe(map()) :: [type()]

Returns a maybe type specification.

Examples

# either a nil or a string
maybe(:string)
Link to this function

maybe(type, predicates \\ [])

View Source (since 0.1.0)
@spec maybe(atom(), []) :: type()

Returns a maybe type specification with additional constraints.

Examples

# either a nil or a non-empty string
maybe(:string, [:filled?])
Link to this function

optional(name)

View Source (since 0.1.0)
@spec optional(atom()) :: {:optional, atom()}

Returns an optional key specification.

Examples

%{
  optional(:age) => type(:integer)
}
Link to this function

required(name)

View Source (since 0.1.0)
@spec required(atom()) :: {:required, atom()}

Returns a required key specification.

Examples

%{
  required(:email) => type(:string)
}
@spec string() :: type()

Returns a string type specification.

Examples

# a string with no constraints
string()
Link to this function

string(predicate)

View Source (since 0.1.0)
@spec string(atom()) :: type()
@spec string([]) :: type()

Returns a string type specification with additional constraints.

Examples

# a string with constraints
string(:filled?)

# a string with multiple constraints
string([:filled?, max_length?: 255])
Link to this function

string(cast_spec, predicates \\ [])

View Source
Link to this function

type(type)

View Source (since 0.1.0)
@spec type({atom(), []}) :: type()
@spec type([{:list, atom()}]) :: type()
@spec type([{:list, []}]) :: type()
@spec type([atom()]) :: [type()]
@spec type(atom()) :: type()

Returns a type specification.

Examples

# string
type(:string)

# either a nil or a string
type([:nil, :string])
Link to this function

type(type, predicates)

View Source (since 0.1.0)
@spec type(atom(), []) :: type()
@spec type(
  {:cast, {atom(), []}},
  type()
) :: type()

Returns a type specification with additional constraints.

Examples

# string with that must be filled
type(:string, [:filled?]),

# an integer that must be greater than 18
type(:integer, [gt?: 18])