I’m the author of the peri library that aims to replicate the idea of plumatic/schema library from the clojure ecosystem.
The idea is to define data validation and schema definition with raw elixir data structures and also handle raw data as input, so it creates an extensibility and schemas combinations.
simple raw data example:
schema = {:integer, {:eq, 12}}
Peri.validate(schema, 12)
# {:ok, 12}
Peri.validate(schema, "12")
# {:error, %Peri.Error{}}
complex raw data structure example:
schema = {:list, {:tuple, [:atom, :any]}}
Peri.validate(schema, ok: 1)
# {:ok, ...}
Peri.validate(schema, foo: :bar, bar: [:hello])
# {:ok, ...}
peri also supports schema definition as maps (atom or strings keys) and keywords, so it’s kinda a merge of plumatic/schema, nimble_options and ecto
This introductions aims to reach the following discussion: peri users often asks for type casting as ecto does, and i probably will implement it, but i also would like to kinda integrate peri with ecto, something like Peri.to_changeset/1
function that receives a peri schema definition and returns a schemaless changeset.
So, given that, does make sense to ecto to handle raw data structures at least for schemaless changesets? Something like:
type = :string
param = "some string"
type
|> Ecto.Changeset.cast(param)
|> Ecto.Changeset.validate_length(min: 5)
|> Ecto.Changeset.apply_action(:parse)
# {:ok, "some string"}
I do understand that will probably be a breaking change since all Ecto.Changeset.validate_*
functions expect a field
argument and also Ecto.Changeset.cast/3
that expects only maps, but the idea of this post is to start the discussion around this topic and not discuss “how the implementation would be”, wdyt?
didn’t find a structured/centralized discussion like that but i do remember of seeing some questions or issues that mentioned this topic
also, i’m not trying to “change” ecto to fit my library needs, but my library need kinda inspired this discussion