-
-
Notifications
You must be signed in to change notification settings - Fork 364
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
Raise error if list or map not provided #443
Conversation
lib/Attribute.js
Outdated
@@ -56,10 +56,11 @@ function Attribute(schema, name, value) { | |||
else if (this.type.name === 'list'){ | |||
|
|||
if(value.type) { | |||
if (!value.list) throw new errors.SchemaError('No list schema provided for attribute:' + this.name ); |
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.
Just for formatting sake, can we change this to the following?
if (!value.list){
throw new errors.SchemaError('No list schema provided for attribute:' + this.name);
}
lib/Attribute.js
Outdated
value = value.list; | ||
} | ||
|
||
if (value === undefined && value[0] === undefined){ | ||
if (value === undefined || value[0] === undefined){ |
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.
Since you said this was failing before, can we write a test that will fail in the current version, and pass with this PR to ensure this will work correctly in the future?
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.
Will do.
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 now running into an issue where if I fetch a record from dynamo that has a field set to an empty array, this error gets raised. Is that the intended behavior?
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.
@dobrynin Honestly I'm not sure. Are you running into that on the NPM version or only after this change? I believe this code should only be run when creating or setting up the schema/model. So I'm not sure why fetching a record from Dynamo would cause this code to be run unless you have saveUnknown
set to true
.
Can you try logging some things out such as value
and see what that prints?
You can also run export DEBUG=*
which will give you more console logs and details about what Dynamoose is doing behind the scenes.
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.
Your thinking was right, we do set saveUnknown
to true
.
@dobrynin You get a chance to work on this anymore? 😃 |
Sorry, I’ve been preoccupied with other work and have neglected this. I’ll get that test going ASAP |
@dobrynin Awesome! Let me know if you have any questions or if I can help at all. |
It seems to me that the existing conditional `(value === undefined && value[0] === undefined)` errors if value is indeed undefined. This change fixes that and adds a more descriptive error if the user forgets to provide the `list` key when using `type: 'list'`.
Pull Request Test Coverage Report for Build 710
💛 - Coveralls |
1 similar comment
Pull Request Test Coverage Report for Build 710
💛 - Coveralls |
Pull Request Test Coverage Report for Build 805
💛 - Coveralls |
@dobrynin Are you ready for this to be re-reviewed for the potential of getting it merged in? |
Yes, please do! |
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.
Super close on this. Great work. Just a few more things!
lib/Attribute.js
Outdated
@@ -56,12 +59,12 @@ function Attribute(schema, name, value) { | |||
else if (this.type.name === 'list'){ | |||
|
|||
if(value.type) { | |||
if (value.list === undefined || value.list[0] === undefined){ |
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.
Above we use !value.map
, here we use value.list === undefined
and value.list[0] === undefined
. Is there a reason this line isn't just (!value.list || !value.list[0])
test/Schema.js
Outdated
}); | ||
} catch (err) { | ||
err.should.be.instanceof(Error); | ||
err.should.be.instanceof(errors.SchemaError); |
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.
Not sure I'm a huge fan of this. If there is no error thrown, this test will still pass. I think it'd be better to create a variable above (set to nothing by default). Then inside the catch, set err
to that variable we created above. Then right before done
is called, run the assertions, to ensure that variable we created exists, ensure it's an instance of, etc.
test/Schema.js
Outdated
}); | ||
} catch (err) { | ||
err.should.be.instanceof(Error); | ||
err.should.be.instanceof(errors.SchemaError); |
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.
Same thing here that I mentioned above
test/Schema.js
Outdated
}); | ||
} catch (err) { | ||
err.should.be.instanceof(Error); | ||
err.should.be.instanceof(errors.SchemaError); |
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.
Same thing here that I mentioned above
lib/Attribute.js
Outdated
@@ -56,12 +59,12 @@ function Attribute(schema, name, value) { | |||
else if (this.type.name === 'list'){ | |||
|
|||
if(value.type) { | |||
if (value.list === undefined || value.list[0] === undefined){ | |||
throw new errors.SchemaError('No list schema given for attribute:' + this.name ); |
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.
throw new errors.SchemaError('No list schema given for attribute:' + this.name ); | |
throw new errors.SchemaError(`No list schema given for attribute: ${this.name}`); |
Super picky, I know. But PR #465 is doing this, so I think from now on we should do it throughout the code.
lib/Attribute.js
Outdated
@@ -42,6 +42,9 @@ function Attribute(schema, name, value) { | |||
if (this.type.name === 'map'){ | |||
|
|||
if(value.type) { | |||
if (!value.map) { | |||
throw new errors.SchemaError('No map schema given for attribute:' + this.name ); |
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.
throw new errors.SchemaError('No map schema given for attribute:' + this.name ); | |
throw new errors.SchemaError(`No map schema given for attribute: ${this.name}`); |
Super picky, I know. But PR #465 is doing this, so I think from now on we should do it throughout the code.
@fishcharlie, sorry for the late reply, I've implemented your changes. |
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.
LGTM
It seems to me that the existing conditional
(value === undefined && value[0] === undefined)
errors if value is indeed undefined. This change fixes that and adds a more descriptive error if the user forgets to provide thelist
key when usingtype: 'list'
.Summary:
Code sample:
Schema
Model
General
GitHub linked issue:
#---
Other information:
Type (select 1):
Is this a breaking change? (select 1):
Is this ready to be merged into Dynamoose? (select 1):
Are all the tests currently passing on this PR? (select 1):
Other:
npm test
from the root of the project directory to ensure all tests continue to pass