Open nickovs opened 5 years ago
Sorry dumb question here... how is this different from:
dimensions = Schema({
Optional("width", default=1024): Coerce(int),
Optional("height", default=576): Coerce(int),
}
If you put a set of values in an Inclusive
group then either all of the items in the group need to be supplied or none of them can be. It is reasonable to want to set a default value for the set of variables while preserving that behaviour. With your schema dimensions({'width':17})
returns {'width':17, 'height':576}
whereas with my schema it would be an error to leave it half-specified.
Ok so then how does the default
value come into play? If you are expecting dimensions({'width':17})
to raise an Invalid are you expecting dimensions({})
to return {'width': 1024, 'height': 576}
?
Yes, that is exactly the behaviour I'm looking for. A schema that uses Inclusive
requires the user to specify all or nothing. If they specify nothing then you get the default values. What you don't want is to get inconsistent results because someone half-specified what was needed. In the example above, allowing the default value for height to be used would give a box with a crazy aspect ratio and is likely not what was wanted.
Thank you for the explanation. I wasn't expecting the inclusive check to occur before default created the key with the default value if the key didn't exist. Updating the documentation to explain how the default value works on Inclusive would be helpful.
The
Inclusive
schema builder does not support default values even though it inherits fromOptional
, which does support them. It would be valuable if defaults were supported for situations such as this:In general, when using
Inclusive
to enforce "all or nothing", having default values for the "nothing" case would be very helpful.