iex(5)> %K8S.Deployment{}
** (ArgumentError) the following keys must also be given when building struct K8S.Depl
oyment: [:spec, :metadata, :apiVersion]
(examples 0.1.0) expanding struct: K8S.Deployment.__struct__/1
iex:5: (file)
Since this is a quick hack, it has numerous edge cases that need to be addressed. Please feel free to provide feedback or suggestions. Opening an issue or pull request is very welcome.
I need to give special thanks to Zoi for making this awesome library!
You can leverage some of the helpers on the Zoi Struct module as @byu mentioned. I personally decided to not add any macros into the library (unless strictly necessary like infering type specs) but I added these helpers for anyone who wishes to extend it.
Thanks for using Zoi, awesome to see this
Great work @wingyplus, this is some inspiring stuff.
One concern I had though with this approach is that having a defstruct-like DSL that looks like Kernel.defstruct but behaves differently can be confusing.
Your work inspired me to experiment with Sigil, a proof-of-concept DSL for defining nested structs and validating external data using Zoi:
defmodule Post do
use Sigil.Schema
schema do
field :title, Zoi.string()
field :image, Image do
field :url, Zoi.string()
field :blurhash, Zoi.string()
field :metadata, Metadata do
field :size, Zoi.integer() |> Zoi.optional()
field :format, Zoi.string() |> Zoi.optional()
end
end
end
end
I’m still iterating and may try more compact forms for types like:
field :age, :integer, coerce: true
This has been a great way to learn macros and DSL design, and aims to complement(and obviously heavily inspired by) Ecto. Whereas Ecto handles data mapping and persistence at the external data/application(and database) layer, validation libraries like zoi and the likes and thus Sigil aim to provide a layer that works great for things like wrapping external api’s into client libraries and doing validation and egornomic casting into elixir constructs.