schema icon indicating copy to clipboard operation
schema copied to clipboard

Validation depending on attribute of the instance

Open CausalReinforcer opened this issue 8 years ago • 3 comments

Is there any chance to enable the following function:

In the schema, there is an 'pet_type', which may take value from ('dog', 'cat'). There is also an attribute of 'pet_food', whose legitimate value depends on 'pet_type' - if it is a dog, then 'pet_food' can take value from ('dog_food_1', 'dog_food_2') , and if it is a cat, then 'pet_food' can take value from ('cat_food_1', 'cat_food_2').

This is quite needed for my ongoing project. Thank you!

CausalReinforcer avatar Aug 11 '17 04:08 CausalReinforcer

I'm looking for a similar feature. in my case that given two dates one is smaller than another. Can items really only be validated in isolation?

towr avatar Aug 24 '17 14:08 towr

So far, I'm afraid so. I'm not sure what it would take to change this, as the library is written with fields being independent of each other.

skorokithakis avatar Aug 24 '17 14:08 skorokithakis

I've just realized that in my case I can get what I want with something like And({'from':str, 'to':str}, lambda d:d['from'] < d['to']).validate({'from':'2016-01-01', 'to':'2017-01-01'}) Since I'm validating a dict, I can also use a validation function on the dict as a whole.

towr avatar Aug 24 '17 15:08 towr