DockYard / valid_field

https://hex.pm/packages/valid_field
MIT License
48 stars 6 forks source link

Asserting constraints #6

Closed bcardarella closed 8 years ago

bcardarella commented 8 years ago

Adds:

These functions should be used to assert constraints. Constraints need to hit the database in order to fire.

Changesets can now be passed directly into assertions if they are already created.

Repo.insert(build_changeset(email: "test@example.com")
Repo.insert(build_changeset(email: "test@example.com")
|> assert_invalid_field(:email)
bcardarella commented 8 years ago

Still a work in progress, don't merge yet

danmcclain commented 8 years ago

You'll want to do this:

Repo.insert(build_changeset(email: "test@example.com")
|> with_changeset
|> assert_invalid_field(:email)
bcardarella commented 8 years ago

@danmcclain what is the reason for requiring with_changeset ? Why can we just pass the changeset directly into assert_invalid_field ?

danmcclain commented 8 years ago

We do that to be able to pass a lambda so that you don't have to define the changeset function in your Model module

danmcclain commented 8 years ago

Could you document the assert_(in)valid_fields?

One minor issue: by calling assert_valid_field via Enum.each, it will stop at the first error. If possible, it would be nice to collect the fields that are (in)valid so that we could show a single message with all the failed constraints

bcardarella commented 8 years ago

@danmcclain I am aware of that issue. For now I need to get this function in so I can move forward with SOG. If you want to open an issue about error collection it can be addressed in the future

bcardarella commented 8 years ago

@danmcclain updated