kronky icon indicating copy to clipboard operation
kronky copied to clipboard

Add support for nested changeset error messages

Open simonprev opened this issue 7 years ago • 20 comments

This adds basic support for errors in nested changesets. I’m open to change the formatting for nested fields, currently it’s: association.field but it could also be associationField or something else. The dot notation fitted our use case.

Results in a GraphQL payload

Client belongs to tutor with validation for the tutor association

%{
  data: %{
    "updateClient" => %{
      "messages" => [
        %{"code" => "required", "field" => "firstname"},
        %{"code" => "required", "field" => "tutor.email"},
        %{"code" => "required", "field" => "tutor.firstname"},
        %{"code" => "required", "field" => "tutor.lastname"},
      ]
    }
  }
}

I also fixed a warning and a failing test in test/test_helper_test.exs

simonprev avatar Jan 24 '18 18:01 simonprev

This is a good start - I want to make sure that when there are many children we support that too.

I'm thinking of two different paths -

  1. Number each nested field, like tutors.0.email tutors.1.email and so forth. Will need to be sure the order in the mutation matches the order in the errors

  2. Pass in an errorId field that is then used to generate the error keys when there are nested errors. In this case, behaviour as specified in point one could be a default behaviour.

lauraannwilliams avatar Feb 02 '18 18:02 lauraannwilliams

@lauraannwilliams I just pushed changes to allow custom field constructor in simple validation message but nested one too. Kronky provides a "default" one that matches the parent.0.child format. You can see the test in test/changeset_parser_custom_field_constructor_test.exs

simonprev avatar Feb 20 '18 16:02 simonprev

When will this available? I require it. Thanks

kapilpipaliya avatar Mar 22 '18 20:03 kapilpipaliya

@simonprev: Both original and your code have a small bug.

Please replace:

String.replace(acc, "%{#{key}}", to_string(value))

to:

key_pattern = "%{#{key}}"

if String.contains?(acc, key_pattern) do
  String.replace(acc, key_pattern, to_string(value))
else
  acc
end

Example values to reproduce problem:

message = "is invalid"
opts = [validation: :embed, type: {:array, :map}]

Found in:

https://github.com/mirego/kronky/blob/0076cde1cac3dc9ab465afec743540f6b08d3e7c/lib/kronky/changeset_parser.ex#L103

Eiji7 avatar May 03 '18 08:05 Eiji7

@Eiji7 Done! I’ve made the requested changes 😄

simonprev avatar May 07 '18 12:05 simonprev

@simonprev Also I forgot to mention that you should set app env (default library configuration), because it fails without any configuration extra. I have tested it and I think that's ready to deploy, but unfortunately I do not have a permission to merge it.

Eiji7 avatar May 07 '18 18:05 Eiji7

@Eiji7 What do you mean by set app env (default library configuration)?

I added the config in config/config.exs:

config :kronky,
  ecto_repos: [],
  field_constructor: Kronky.FieldConstructor

simonprev avatar May 14 '18 12:05 simonprev

@simonprev: Please take a look at stackoverflow question.

Eiji7 avatar May 14 '18 13:05 Eiji7

@Eiji7 Is my latest commit enough as the default library config?

simonprev avatar May 18 '18 01:05 simonprev

@simonprev: Yes, now all good! Good job!

Eiji7 avatar May 18 '18 21:05 Eiji7

Is there any updates on this?

ssbb avatar Sep 13 '18 21:09 ssbb

@ssbb Looks like owner does not made any update for months. I think that @simonprev's version works without any problem, so you should use his fork version instead.

Eiji7 avatar Sep 15 '18 14:09 Eiji7

@simonprev After updating Elixir new warning come:

warning: found quoted keyword "coveralls" but the quotes are not required. Note that keywords are always atoms, even when quoted, and quotes should only be used to introduce keywords with foreign characters in them

It looks pretty simple, so can you update it?

Eiji7 avatar Sep 15 '18 14:09 Eiji7

@lauraannwilliams could you please merge it? 🙏

quolpr avatar Oct 09 '18 12:10 quolpr

Any updates on this? Will it get merged anytime soon?

d-webs avatar Feb 20 '19 21:02 d-webs

@dwebster17 It's almost 6 months after it's 100% ok for me. I don't see any arguments to not merge it especially that's not conflicting with original repository. Unfortunately 6 months should say enough … looks like this repository is dead.

Fortunately it does not mean that this project is dead. You can still use code from this PR in any Elixir project (just use github option when defining dependency in mix.exs). Maybe @simonprev would release this as hex package under his own account (so it would have same name, but different author).

The other way is to get in touch with author and change permissions or even change owner for this repository. I could take it and merge PR then, but looks like that its owner is not replying here, so we would need to find other communication way with him.

Eiji7 avatar Feb 21 '19 15:02 Eiji7

Hey @lauraannwilliams, would it be OK if I officially fork this repo and publish a new package to hex.pm (I would rename it). You can see from this PR that the fork is already used by some people 😄 The fork would live in the Mirego organization on GitHub.

If I have not heard from you by the end of the month, I will assume that this repo is unmaintained and I will proceed with the fork. If you don’t want me to publish your code on hex.pm that’s ok too 😄 Thank you for this library!

simonprev avatar Mar 20 '19 21:03 simonprev

@simonprev I don't think you need even ask about permissions. Just take a look at LICENSE file - it's MIT license. As long as you use somebody work in way which license allows then you should not have any law problems.

Also there is no need to rename it as you can use same name: https://hex.pm/docs/private

Eiji7 avatar Mar 21 '19 12:03 Eiji7

For everyone interested in switching their Kronky dependency to our fork: https://github.com/mirego/absinthe-error-payload

It supports nested changesets, uses mix format to simplify collaboration and is used in many projects already. Bonus: We run tests in travi to prevent broken releases and code coverage to maintain quality 😄 And the new name fits nicely in your mix.exs below absinthe, absinthe_plug, absinthe_phoenix, etc

Thank you again Ethelo for building Kronky!

simonprev avatar May 01 '19 17:05 simonprev

Hi Simon - I havn't had any time to devote to this, so the fork is definitly a good idea. :) Thanks for taking this on.

lauraannwilliams avatar May 08 '19 15:05 lauraannwilliams