Hello everyone, I have written a library about validation joi, which I think is suitable for use in CQRS projects where there are many data structures defined. I’m about to release version 0.2.0, currently in the develop
branch. Do you have any suggestions for the API, or for the code, or for testing
1 Like
Hi, would it be possible to build this library on top of Ecto’s schemaless changesets?
2 Likes
Could you write an example of expected behavior? @stefanchrobot
When I looked at the example on GitHub:
iex> schema = %{a: [:integer]}
%{a: [:integer]}
iex> data1 = %{a: 1}
%{a: 1}
iex> Joi.validate(data1, schema)
{:ok, %{a: 1}}
iex> data2 = %{a: <<123>>}
iex> Joi.validate(data2, schema)
{:error,
[
%Joi.Error{
context: %{key: :a, value: "{"},
message: "a must be a integer",
path: [:a],
type: "integer.base"
}
]}
the same - or at least similar - thing can be achieved using schemaless changesets with Ecto:
iex> types = %{a: :integer}
iex> data = %{a: "hello"}
iex> changeset = {%{}, types} |> Ecto.Changeset.cast(data, Map.keys(types))
#Ecto.Changeset<
action: nil,
changes: %{},
errors: [a: {"is invalid", [type: :integer, validation: :cast]}],
data: %{},
valid?: false
>
So the question is whether the functionality you’re building can be written as an extension to Ecto, which is I think is very commonly used. In one of the previous projects, I had a microservice that didn’t have it’s own storage, but I still used Ecto to validate the schema of the incoming requests.
4 Likes