sylvainpolletvillard / ObjectModel

Strong Dynamically Typed Object Modeling for JavaScript
http://objectmodel.js.org
MIT License
467 stars 28 forks source link

Is it possible to validate without throwing an error? #31

Closed talyssonoc closed 8 years ago

talyssonoc commented 8 years ago

I'd like to have something link an isValid() method instead of throwning an error right from the constructor, for example:

const NumberModel = Model(Number);
const number = new NumberModel('42'); // do not throw TypeError
number.isValid(); // returns false

Is is achievable with this lib?

Btw, nice lib!

sylvainpolletvillard commented 8 years ago

Hi,

I think you are looking for let isValid = NumberModel.test('42')

There is also a validate method that can receive a custom error collector as an argument: NumberModel.validate('42', errors => console.log(errors))

talyssonoc commented 8 years ago

Oh, you're right! I was looking for a way to do that directly from the instance but it will work too. Thanks! :smile:

sylvainpolletvillard commented 8 years ago

Models instances are necessarily valid, this is the whole point of strong type checking. If an operation on a model instance makes it invalid, the operation will not be applied.