Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Getting nested value in error value using Changeset#get should work #357

Conversation

kimroen
Copy link

@kimroen kimroen commented Jun 21, 2019

In v1.6.0, it was possible to get a nested value from the error object by using get on an instance of Changeset:

changeset.get('error.name')

This returned undefined if it wasn't present, and an object with the value and validation if it was.

This is not possible in the newest version - it instead returns undefined no matter what.

By using git bisect with this test, I found that this stopped working in v2.0.0-beta.0.

I don't know what the intended behavior here is now, but it seems to me like this at least was intended to work.

For instance, there's an example in the README that uses this too, which also fails since 2.0: https://github.com/poteto/ember-changeset/blob/3d777266a252d352513c6833fe3fc83345da9c90/README.md#tips-and-tricks

This always returns undefined and sets hasError to false:

    if (!changeset.get(`error.${valuePath}`)) {
      set(this, 'hasError', false);
    } else {
      // if error, restore changeset so don't show error in template immediately'
      // i.e. wait for onblur action to validate and show error in template
      changeset.restore(snapshot);
    }

The alternative to making this work again is to instead use normal getters:

// no
changeset.get('error.name')

// yes
changeset.error.name

You could even say this is preferable, I'm merely saying that it used to work and now it doesn't.

In v1.6.0, it was possible to get a nested value from the `error` object by using `get` on an instance of Changeset:

```js
changeset.get('error.name')
```

This returned `undefined` if it wasn't present, and an object with the `value` and `validation` if it was.

This is not possible in the newest version - it instead returns `undefined` no matter what.

By using `git bisect` with this test, I found that this stopped working in `v2.0.0-beta.0`.

I don't know what the intended behavior here is, but it seems to me like this at least _was_ intended to work.

There's an example in the README that uses this too, which also fails since 2.0: https://github.com/poteto/ember-changeset/blob/3d777266a252d352513c6833fe3fc83345da9c90/README.md#tips-and-tricks

Here too, this always returns `undefined` and sets `hasError` to `false`:

```js
    if (!changeset.get(`error.${valuePath}`)) {
      set(this, 'hasError', false);
    } else {
      // if error, restore changeset so don't show error in template immediately'
      // i.e. wait for onblur action to validate and show error in template
      changeset.restore(snapshot);
    }
```
@snewcomer
Copy link
Collaborator

Hm interesting. I wonder what doesn't work from the below line and if that errors hash has the key....

https://github.com/poteto/ember-changeset/blob/master/addon/index.ts#L679

@kimroen
Copy link
Author

kimroen commented Oct 21, 2019

@snewcomer Would key be "error.name" there? If so, I guess it wouldn't match anything unless they are prefixed like that in the _errors property? I'm not familiar with how those things fit together.

@kimroen kimroen deleted the getting-nested-value-in-error branch December 6, 2019 08:14
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants