View Source Shapex.Types (shapex v0.2.0)

Module that contains helper functions to define schema.

Summary

Functions

Atom type for Shapex.

Boolean type for Shapex.

Record type for Shapex. It's like a map, but fields are not pre-defined, useful for dictionaries.

Enum type for Shapex.

Float type for Shapex. Validation rules

Integer type for Shapex. Validation rules

List type for Shapex.

Map type for Shapex. Fields required by default, if you want make them optional use &optional/1

Support function to make map field optional

String type for Shapex.

Functions

Atom type for Shapex.

Validations:

- :eq - equality check
- :neq - not equal check

## Example

atom(eq: :ok)

Boolean type for Shapex.

Validations:

- :eq - equality check
- :neq - not equal check

## Example

boolean(eq: true)
Link to this function

dict(key_type, value_type)

View Source

Record type for Shapex. It's like a map, but fields are not pre-defined, useful for dictionaries.

## Example

schema = dict(string(), string())

value = %{
  "id1" => "value1",
  "id2" => "value2"
  "id3" => "value3"
}

Shapex.validate(schema, value)

# {:ok, :valid}

Enum type for Shapex.

## Example

enum([integer(), string()])
Link to this function

float(validations \\ [])

View Source

Float type for Shapex. Validation rules:

- :gt - greater than
- :gte - greater than or equal
- :lt - less than
- :lte - less than or equal
- :eq - equal
- :neq - not equal
- :in - in list
- :not_in - not in list
Link to this function

integer(validations \\ [])

View Source

Integer type for Shapex. Validation rules:

- :gt - greater than
- :gte - greater than or equal
- :lt - less than
- :lte - less than or equal
- :eq - equal
- :neq - not equal
- :in - in list
- :not_in - not in list
- :custom - custom validation function

## Example

integer(
  gt: 10,
  gte: 10,
  lt: 20,
  lte: 20,
  eq: 15,
  neq: 10,
  in: [1, 2, 3],
  not_in: [4, 5, 6],
  custom: fn value ->
    if value > 10 do
      "Value must be less than 10"
    end
  end
)

List type for Shapex.

## Example

list(integer())

Map type for Shapex. Fields required by default, if you want make them optional use &optional/1

## Example

map(%{
  name: string(),
  age: integer()
})

Support function to make map field optional

## Example

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

string(validations \\ [])

View Source

String type for Shapex.

Validation rules:

  • :min_length - minimum length
  • :max_length - maximum length
  • :length - exact length
  • :in - in list
  • :not_in - not in list
  • :custom - custom validation function