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

Getters shouldn't be subject to validation #820

Closed
tarqd opened this issue Apr 7, 2012 · 2 comments
Closed

Getters shouldn't be subject to validation #820

tarqd opened this issue Apr 7, 2012 · 2 comments

Comments

@tarqd
Copy link

tarqd commented Apr 7, 2012

Currency = {
  type: Number,
  min: 0,
  set: function(v) {
    if (typeof v === 'string' && v.length > 1 && v[0] === '$') {
      return parseFloat(v.substr(1)) * 100;
    } else {
      return v;
    }
  },
  get: function(v) { // fails validatation when used
    return '$' + (v / 100).toFixed(2);
  }
};

var User = new Schema({
    cash: Currency
});

var user = new User({cash: "$2.50"});
console.log(user.cash); // error

If getters are meant for display-only I don't think they should be subject to validation (especially since we're generating it, obviously we're going to be okay with the output)

@tarqd
Copy link
Author

tarqd commented Apr 7, 2012

In addition this the setter doesn't work because it's never called ("$2.50" isn't a number, but I setters should be called first in case they can be converted into the proper format)

@aheckmann
Copy link
Collaborator

the setter/casting order issue is an open ticket. kind of surprised no one mentioned this use case about getters before.

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

No branches or pull requests

2 participants