Zoi.Struct (Zoi v0.6.4)

View Source

A helper module to define and validate structs using Zoi schemas. This module provides functions to extract @enforce_keys and struct fields from a Zoi struct schema. It is particularly useful when you want to create Elixir structs that align with Zoi schemas.

Examples

defmodule MyApp.SomeModule do
  @schema Zoi.struct(__MODULE__, %{
    name: Zoi.string()
    age: Zoi.integer() |> Zoi.default(0) |> Zoi.optional(),
    email: Zoi.string()
  })

  @enforce_keys Zoi.Struct.enforce_keys(schema) # [:name]
  defstruct Zoi.Struct.struct_fields(schema) # [:name, :email, {:age, 0}]
end

Summary

Functions

Returns a list of keys that are required for the struct based on the schema. This is useful for defining @enforce_keys in Elixir structs.

Returns a list of fields for the struct, where fields with default values are represented as tuples of the form {key, default_value}. This is useful for defining the fields of an Elixir struct.

Functions

enforce_keys(struct)

Returns a list of keys that are required for the struct based on the schema. This is useful for defining @enforce_keys in Elixir structs.

struct_fields(struct)

Returns a list of fields for the struct, where fields with default values are represented as tuples of the form {key, default_value}. This is useful for defining the fields of an Elixir struct.