jagi / meteor-astronomy

Model layer for Meteor
https://atmospherejs.com/jagi/astronomy
MIT License
604 stars 67 forks source link

Is there a way to use the validator without a document/model? #721

Closed dnish closed 3 years ago

dnish commented 3 years ago

Hey, I'm using some third party libraries on the client side and need to validate the forms based on the logic of the model. Is there any way to use the validators without creating a model? I've tried to check out the Validators class but it seems that it is heavily connected to the Model.

Normally I create a temporary model, add the form input to its properties and then execute a validation. But in this case it's a little bit problematic, because I want also to validate the password length - and the model doesn't have a password property (because it is saved within the services object).

lukejagodzinski commented 3 years ago

Validators were designed as integral part of the package but probably shouldn't. There were some not great design decisions but it's still possible to call validators manually:

Astro.Validator.validators.date.isValid({ value: new Date() }); // true
Astro.Validator.validators.date.isValid({ value: 5 }); // false
Astro.Validator.validators.gte.isValid({ value: 10, param: 5 }); // true

You can go into code of each validator and see how the isValid function is being executed for each validator.