lukejagodzinski / meteor-astronomy-validators

https://atmospherejs.com/jagi/astronomy-validators
MIT License
11 stars 13 forks source link

Make fields required only when a condition is meet #21

Closed ghost closed 9 years ago

ghost commented 9 years ago

Hi jagi,

Was wondering if there's any way to make a list of fields required only when a condition is meet ? For example, a schema with an object that has multiple fields inside. And those would be required, if the object is actually there, otherwise, they would be null().

It actually permits to add data to a single schema from multiple form, while not permitting to have empty values when you don't want to in each form.

A more visual example:

'salary': {
     type: 'object',
     validators: Validators.or([
         Validators.object(),
         Validators.null()
     ]) 
},
'salary.start': {
     type: 'date',
     validators: Validators.if(this.salary, [
          Validators.required({}, 'La date de début est requise'),
          Validators.date()
          ],
          Validators.null()
     )
}

Here, it would mean that if this.salary actually exist, then salary.start is required and has to be a date, otherwise, it's null. Am i taking the problem the wrong way ? Or maybe there's already a way to do this ?

Right now, i'm simply only validating the values i want everytime i need to, instead of using validateAll()

lukejagodzinski commented 9 years ago

Right now it's not possible in easy way.

It's actually possible in the new version :-). Everything thanks to nested documents. I will write more about this feature in the documentation. I'm finishing working on the direct database access which is the last big feature. After that I have to make a few small fixes and I will be inviting all users to test 1.0 rc1. In the meantime I will be working on the documentation page, which I've already started creating. So we are not so far from the final release :-)

ghost commented 9 years ago

This new version is really, but like really, gonna make everything so smooth ! I'll be waiting for this to actually make changes on my app. Thanks.

lukejagodzinski commented 9 years ago

Hehe, yes I was working hard and for a long time on this :). Many features that I've introduced appears to solve many problems that I didn't even know that may exist :)