If unique_items=True is set on a validator.Array() whose items are a non-primitive type, a http request to the handler under inspection crashes the server with an assertion error. I am wondering if something along the lines of
validators.py
def make_hashable(self, element):
assert (element is None) or \
isinstance(element, (bool, int, float, str, list, dict)) or \
hasattr(element, '__hash__')
...
return hash(element)
TypeSystem now handles all the validation https://github.com/encode/typesystem (And yes - it gets uniquness correct on composite types, such as lists and dicts)
If
unique_items=True
is set on a validator.Array() whoseitems
are a non-primitive type, a http request to the handler under inspection crashes the server with an assertion error. I am wondering if something along the lines ofvalidators.py
to enable unique_items=True on custom types will be useful to users. Users should obviously be warned that they need to implement sane
__eq__
and__hash__
methods. The check above is rather primitive, perhaps there are better ways to consider a type as hashable; @nedbat says to simply attempt the hashing as per https://stackoverflow.com/questions/3460650/asking-is-hashable-about-a-python-value/3460725#3460725 With the above fix, a very slightly modified example from https://github.com/encode/apistar/issues/507#issuecomment-390854838works as expected and identifies the third entry of the array violating the unique constraint. Output: