Don't allow non-functions to be set as getters or setters #704

Closed
ljharb opened this Issue Feb 1, 2012 · 4 comments

Projects

None yet

2 participants

Contributor
ljharb commented Feb 1, 2012

I misread the documentation and passed "true" for "get" when creating a schema. This led to an error in the save callback: [TypeError: Object true has no method 'call']

Obviously I'm the one who messed up, but it would be nice if it failed much faster. Attempting to create a schema with a getter or setter that's not a function should either cause an error, be ignored, and/or console.log a warning - it would have saved me hours of troubleshooting.

I'd do the pull request myself but I'm not sure where that check should be added. If somebody can point me to the file/function/line, I'll be happy to make the change.

fredrick commented Feb 1, 2012

The relevant lines are in the SchemaType prototype, https://github.com/LearnBoost/mongoose/blob/master/lib/schematype.js#L111-133

Something along the lines of typeof(fn) === 'function' would probably suffice, however, not sure if this appropriate, and what the thoughts of the principle maintainers on this kind of defensive argument/type checking. Comments?

Contributor
ljharb commented Feb 1, 2012

Since "required" forces any !== false value to be true, I'm going to go with "force a sensible value". Pull request in #706

Contributor
ljharb commented Feb 1, 2012

Fixing typos and throwing an Error instead in #707

Contributor
ljharb commented Feb 2, 2012

Pull request's merged, thanks!

@ljharb ljharb closed this Feb 2, 2012
@gitfy gitfy pushed a commit to gitfy/mongoose that referenced this issue Feb 5, 2012
@aheckmann aheckmann ocd + test
closes #707
closes #704
075cc56
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment