keleshev / schema

Schema validation just got Pythonic
MIT License
2.86k stars 214 forks source link

Callable not called as documentation implies? #295

Closed geokala closed 5 months ago

geokala commented 1 year ago

The documentation on pypi states: If Schema(...) encounters a callable (function, class, or object with __call__ method) it will call it, and if its return value evaluates to True it will continue validating, else—it will raise SchemaError.

However, this is not the behaviour seen when using, e.g. UUID: `>>> from uuid import UUID

from schema import Schema bool(UUID('62aba560-bc73-49c9-b629-ae2a5bf14f17')) True hasattr(UUID, 'call') True Schema(UUID).validate('62aba560-bc73-49c9-b629-ae2a5bf14f17')`

This is in python3.11, with schema==0.7.5

mutricyl commented 5 months ago

Actually UUID qualifies as a type.

>>> type(UUID)
<class 'type'>
>>> type('62aba560-bc73-49c9-b629-ae2a5bf14f17')
<class 'str'>
>>> Schema(UUID).validate(UUID('62aba560-bc73-49c9-b629-ae2a5bf14f17'))
UUID('62aba560-bc73-49c9-b629-ae2a5bf14f17')

So you should refer to type section of the documentation

You might use the following:

>>> from uuid import UUID
>>> from schema import Schema, And, Use
>>> Schema(And(Use(UUID), UUID)).validate('62aba560-bc73-49c9-b629-ae2a5bf14f17')
UUID('62aba560-bc73-49c9-b629-ae2a5bf14f17')
geokala commented 5 months ago

OK, makes sense- it might be worth updating the callable section of the documentation to refer to that, but thanks for the clarification!