DockYard / valid_field

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

Embedded Schemas #29

Open zimt28 opened 6 years ago

zimt28 commented 6 years ago

Hi! First, thanks for this great library – this is the only sane way to test changesets I could find 👍

Currently there is an issue with embedded schemas (using |> cast_embed(:address, required: true)):

@valid params_for(:address)
@invalid params_for(:address, city: nil)

# This passes, but shouldn't
%Resource{}
|> with_changeset()
|> assert_field(
  :address,
  [@invalid, %{totally_invalid: :yes}],
  [nil, ""]
) # -> Passes

# This doesn't pass, but should
%Resource{}
|> with_changeset()
|> assert_field(
  :address,
  [],
  [nil, "", @invalid, %{invalid: :yes}]
)
# ** (ValidField.ValidationError) Expected the following values to be invalid for "address": 
# %{city: "", country_iso: "DE", postcode: "66960-5408", street: "Aliyah Prairie 7"},
# %{invalid: :yes}

When passing the wrong params for the embedded schema to the changeset manually, it's invalid as expected.

zimt28 commented 6 years ago

It took me some time to figure it out, but I think I've done it. See https://github.com/DockYard/valid_field/pull/32