-
Notifications
You must be signed in to change notification settings - Fork 88
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
Removes undefined values when populating #127
base: master
Are you sure you want to change the base?
Conversation
P.S. I would love to document this in the readme, but I don’t see an obvious place to add the info. I’m absolutely open to suggestions! |
@aexmachina: Hi there, I don't mean to pester but I was wondering if you have any thoughts about this. Thank you! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for this, sorry for the late reply!
@@ -14,6 +14,8 @@ export default function asyncPopulate(target, source) { | |||
promise = asyncPopulate(target[attr], source[attr]); | |||
} else if (source[attr] === null) { | |||
target[attr] = null; | |||
} else if (typeof source[attr] === 'undefined') { | |||
delete target[attr]; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Why do we delete this? I would've thought it'd be better to just pass through the undefined to the target.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The primary motivation here is to provide developers with a way of telling factory-girl to leave a particular attribute of the built object out. Without this all attributes are always required.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm against this in principle, unless there's a compelling reason why. Factories define a particular "type" of object, which should be stable at runtime. If you do need to omit a property at runtime then you can use afterBuild
or some other hook to delete
the property.
I'm still happy for this PR, because we want to avoid getPrototypeOf(undefined)
throwing an exception, but I don't think we should delete
any of the properties 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hm, yeah, I see what you mean. That will probably work. I’ll give it a shot and report back.
Supposing passing through the value as undefined
and then doing extra processing on it in afterBuild
(in my case, deleting the key/value pair), would a diff like this be good to go?
+ } else if (typeof source[attr] === 'undefined') {
+ target[attr] = undefined;
Factory-girl does not support being passed an object spec where a member has the value
undefined
. This happens:If
source[attr]
is defined asundefined
, thenasyncPopulate
callsisPlainObject
which passes undefined toObject.getPrototypeOf
which throws aTypeError
.This PR alleviates this by having factory-girl remove members where the value is
undefined
. A use case where this comes up is when using factory-girl with structure. Structure validates objects against a spec, and allows you to define default values for individual members.So if I have an object spec and a factory for it like this:
This is great for tests, except when you want to test that the default works because there is no way to tell factory-girl "completely leave this one value out". And so, this PR alleviates this need by allowing someone to build a factory and tell it to drop a member from the built object.