exchema v0.2.0 Exchema.Struct
DSL to define a type struct. The idea is to be a replacement for defstruct.
It general, it is similar to defstruct but instead of defining the default values, you define the types of your fields.
Example
defmodule MyRange do
use Exchema.Struct, fields: [
start: Exchema.Types.Integer,
end: Exchema.Types.Integer
], check_schema: [
fun: fn %{start: s, end: e} ->
s <= e
end
]
end
# Now you can use it as a struct
range = %MyRange{start: 1, end: 2}
# But you can also use it as a type
true = Exchema.is?(range, MyRange)
# And this will check the constraints you specified
false = Exchema.is?(%MyRange{start: "0", end: "1"}, MyRange)
:fields
The fields option specifies the types of the fields you have and also defines them as the struct fields.
:check_schema
This option is a list of predicates to validate the whole schema against. It is useful to check constraints between fields, i.e. password confirmation.