-
-
Notifications
You must be signed in to change notification settings - Fork 100
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
How to validate nested keys? #110
Comments
👍 |
This is currently a bug that I'm going to fix - when it lands, it should be: {
title: validatePresence(true),
'contactNumber.number': validatePresence(true)
} Tracking the work here - adopted-ember-addons/ember-changeset#140 |
@poteto We ran into this exact same problem this morning! :-) Excited for your update! |
Is there any update on when a fix for this might land? I tried working around it by getting my top-level validator to return an object with keys containing the nested errors, but that then changes the error structure to be non-standard, and so I'd rather avoid doing that if possible. |
Hey @BillyRayPreachersSon, I'll be reviewing this sometime this week – see adopted-ember-addons/ember-changeset#140 (comment). It's high on my todo list. |
Will have time for this on Thursday and Friday. |
Hola, I just ran into this exact issue and coincidence you guys are talking about it...
This doesn't work so far...
|
Was this issue fixed by the 3.0 release? I'm trying to validate a nested key as follows, and it does not appear to be working at this time (the other non-nested validations on this model work as expected): // Data structure
{
amount: {
value: 0,
currency: 'USD'
}
} // Validations
import {
validateNumber,
validatePresence,
} from 'ember-changeset-validations/validators';
export default {
'amount.value': [
validatePresence(true),
validateNumber({ integer: true }),
]
}; |
@kpfefferle Prior to the 3.0 release, there were some hard to deal with bugs relating to nested keys. This was mainly due to dot separated keys. With 3.0 (and specifically 3.4.0), we have finally reached a solid solution for allowing users to retrieve CHANGES/CONTENT and set at any level in the object tree. One consequence of this is that we deal with JS objects as they are (in "expanded" form). So this should work!
|
@snewcomer Great, I actually like that syntax better. I'll give that a try, thanks! |
The validation seems to be working as described, but I'm seeing the top-level object get replaced with only the nested properties from the changeset rather than preserving any properties that have not been edited. To build on my example above, I have the following data structure: {
amount: {
value: 0,
currency: 'USD'
}
} I then have a changeset validating // Validations
export default {
amount: {
value': [
validatePresence(true),
validateNumber({ integer: true }),
]
}
}; When I apply the changeset to the underlying model, I lose the {
amount: {
value: '1000'
// currency: 'USD' has been wiped out by the changeset
}
} I would expect the changeset to only update If this should be filed as a new issue or on a different repo, please let me know. |
Just as a quick check, what version are you on? |
@snewcomer I just updated this morning to [email protected] (which did fix a previous issue from 3.4.0!) |
You used the never ever never |
@snewcomer With 3.5.1, I'm still getting my {
amount: {
value: {
value: '1000' // nested value.value
}
// currency: 'USD' is still wiped out
}
} where I'd hope to see {
amount: {
value: '1000',
currency: 'USD'
}
} |
@kpfefferle With the latest e-c-v (3.5.2), is it still failing? How different is your case from this test case? |
Might still not work with Ember Data Models. I defined a model with an attribute which contains an object: import Model, { attr } from '@ember-data/model';
export default class TermModel extends Model {
@attr('string') title;
@attr({
defaultValue() {
return {};
}
}) content;
} And created a corresponding validator with a rule for the nested object: import {
validatePresence
} from 'ember-changeset-validations/validators';
export default {
title: validatePresence(true),
content: {
address: validatePresence(true)
}
}; Finally in my template I followed the suggested implementation: <div>
Title:<br>
<Input @value={{@changeset.title}} />
</div>
<div>
Address:<br>
<Input @value={{@changeset.content.address}} />
</div>
<button {{on 'click' (fn @submit @changeset)}}>
Save
</button> If I call the validation method, only the first level of attributes is recognized. I also tried it with a pure Javascript object and unfortunately I got the same result. |
@snewcomer Sorry for the delay on this, but I recently refactored away from the pattern that was requiring me to validate nested keys. Thanks for your responsiveness, and hopefully someone else running into the same issue can help out moving forward. |
Given the following Ember.Object:
How do I define validations to validate the contactNumber.number field?
The text was updated successfully, but these errors were encountered: