Is anybody interested in an Ecto like validation library?

#1

I’ve implemented a validation library that’s basically a copy of Ecto’s Changeset validations. Rather than working on schemas, these validations work on plain maps and structs. It also doesn’t (currently) implement any sort of coercion or casting.

I’m curious if anyone else would find this useful. If so, I’d be happy to open source what I’ve got.

Before you answer my question I’m sure some of you these two questions:

  1. Why not just use Ecto?

For the purpose of my project, I didn’t need Ecto. Importing a database/schema library when I don’t need databases or schemas seemed unnecessary to me. Even when I am using Ecto sometimes it doesn’t make sense to convert my structs to schemas for no other purpose than validation. I just want to pass in a map or struct and get some validations back.

Yes, I know you can use Ecto on plain maps if you pass in types as a tuple instead of a schema but even that seems unnecessary in most non-Ecto circumstances.

  1. Why not use Vex?

This was originally the route I went. After getting knee deep into implementation I was finding that it’s ActiveRecord inspiration created unneeded complexity. One of the things I love about Ecto’s validations is that they’re just functions. No need to add a module with callbacks to implement new validations.

This is my rationale. If the community would find this useful I’d be happy to contribute it. Otherwise, I’ll just keep it to myself and not spend the time on documentation/support.

10 Likes
Validate in elixir Without Ecto and ...
#2

This here is a great example as to why Ecto’s Changeset functionality needs to be pulled into a standalone library. :slight_smile:

However, you can just link in Ecto and use it’s code without actually using Ecto, that is what I do currently… ^.^;

There are a couple edge cases in Ecto (one of which just got mentioned in an issue on the tracker recently so it may get fixed, whooo!) that I’d like to see fixed, but it seems decent there on it’s own. :slight_smile:

I’m curious what full set of changes you’ve made compared to Ecto? It’d be nice to see a github of it if possible? :slight_smile:

6 Likes
#3

Is this this your lib? Saw it on reddit earlier.
This one is pretty cool.

#4

Here’s what I have.

It has all validations Ecto.Changeset provides except validate_number and validate_subset. Feel free to open a PR :wink:

I’m sure there are bugs and dragons. Feel free to open issues if you find any.

Would love any feedback!

#5

P.S. haven’t released this yet but plan to soon

#6

P.P.S. Documentation is a WIP :smile:

#7

i just wanted to reply that while i don’t currently have a use case for this, it would have been extremely useful to me over my last couple projects. i look forward to using it soon

1 Like
#8

There has also been the idea (and iirc some work) into pulling all the database related functionality out of ecto, so you can use ecto with and without a database in the back. Personally I find that approach much more compelling, because validation is one part, but the whole idea of schema/schemaless workflows seems quite complex to replicate.

3 Likes
#9

@anthonator: Something like:

config :my_lib, validation_module: MyApp.ValidationModule

defmodule MyApp.ValidationModule do
  use MyLib.DefaultValidations

  def validate(term, :my_custom_type) do
    # …
  end

  def validate(struct = %MyApp.MyStruct{}, :my_struct) do
    validate_all(struct, age: :integer, name: :bitstring, something: :my_custom_type)
  end

  def validate(_term, _type), do: false
end

with changesets could be really awesome.

Anyway I think any Open Source library is better than nothing. :smiley:

1 Like
#10

Until Ecto is broken up you can:

  1. Add ecto dependency without adding adapter (e.g postgrex) dependency. You’ll still get some unnecessary stuff though. You may need to set config :myapp, ecto_repos: [].
  2. Use schemaless changesets - https://hexdocs.pm/ecto/Ecto.Changeset.html#module-schemaless-changesets

Edit: oops, you’ve already mentioned schemaless changesets in your initial post, sorry for noise then!

4 Likes
#11

It was probably a mistake for me to compare this to Ecto. My goals are aligned more with Vex than they are with Ecto.

I’ve already described my issue with Vex. Ecto’s approach to validations feels much more natural in Elixir. I’m trying to accomplish Vex’s goals with Ecto.Changeset’s approach/aesthetic. If that makes sense.

#12

Just released the initial version.

The documentation is still pretty poor but I’m working on it.

3 Likes
#13

@anthonator How can I contrib with module? Do you have a ToDo list?