ansman / validate.js

A declarative validation library written javascript
https://validatejs.org
MIT License
2.63k stars 336 forks source link

Nested validation with multiply properties #198

Open pto4 opened 7 years ago

pto4 commented 7 years ago

To validate this object: var o = { a: { b: { p1: '', p2: 'xxx' } } };

I had to write the next lines of code: validate(o, {'a.b.p1': {presence: true}, 'a.b.p2': {presence: true}}); But, in my opinion, it would be better to write validate(o, {'a.b': {p1: {presence: true}, p2: {presence: true}}});

Is this possible to implement?

The main reason to allow the latter approach is that this makes possible to create a plugin for validate.js allowing to validate each array item or value which is returned by function.

ansman commented 7 years ago

The problem is how you would then validate that a.b is required?

pto4 commented 7 years ago

Sorry, I didn't understand your question. The structure of my object o is constant.There are p1 and p2 can be changed only. The a.b exists all time and I don't need to validate it.

ansman commented 7 years ago

Yes, but we can't allow both as we would have not way of differentiating between the two scenarios