Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

no error after forgetting Schema() in voluptuous

According to examples, this is the correct way to create a validation Schema:

import voluptuous as vol
PORT1 = vol.Schema(vol.All(int, vol.Range(min=0, max=65535)))

However, I noticed, that the Schema call is missing in some of my validators, e.g.:

PORT2 = vol.All(int, vol.Range(min=0, max=65535))

I checked that PORT1 and PORT2 are not of the same type. The catch is that PORT2 works fine for me and gives the same results as the correct PORT1.

I don't know if I made a mistake. Could somebode please clearly state if it is an error to omit the Schema(...)? Why it works so well without the Schema(...) that I did not notice any problems?

like image 453
VPfB Avatar asked Dec 15 '25 14:12

VPfB


1 Answers

Every validator has a __call__ defined for in the validators. You can see the source code below

https://github.com/alecthomas/voluptuous/blob/master/voluptuous/validators.py#L279

So even if you have

PORT3 = vol.Range(min=0, max=65535)
PORT3(100)

This will also work. As you said, PORT1 and PORT2 are different objects but the __call__ method is defined on all validators as well ones derived from _WithSubValidators

https://github.com/alecthomas/voluptuous/blob/2e557f71db6260e3ab40a6848a6bf4705d434f2d/voluptuous/validators.py#L184

The Schema object is wrapper around these validators to check an object as such.

In your case since you are only validating individual fields or combining them together with other validators, they will work perfectly fine

Working

like image 70
Tarun Lalwani Avatar answered Dec 17 '25 07:12

Tarun Lalwani



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!