sarugaku / plette

Structured Pipfile and Pipfile.lock models.
ISC License
7 stars 7 forks source link

Validation fails on tomlkit parsed data #6

Closed frostming closed 5 years ago

frostming commented 5 years ago

When I try validating data parsed by tomlkit, it always fails. After some digging, the causes are as follows:

In cerberus/validator.py:

def __init_processing(self, document, schema=None):
        ...
        self.document = copy(document)

Calling copy on document, which is a tomlkit.container.Container instance, breaks the iterability for unknown reason:

>>> list(document)
['name', 'url', 'verify_ssl']
>>> list(copy(document))
[]

Therefore, all fields defined in the data will be recognized as missing.

I am not sure which of cerberus/tomlkit/plette is responsible to fix this. But it will be easy to fix it here: just calls data.copy() to pass to the validator.

uranusjr commented 5 years ago

Oops, I also saw this a couple of weeks ago, but forgot to raise it. You can work around the problem by changing line 25 below to explicitly copy to dict before validating:

https://github.com/sarugaku/plette/blob/fad7956dac45bbe3d4e03f2b61b67f9b9d6d4135/src/plette/models/base.py#L16-L27

if v.validate(dict(data), normalize=False):
    ...

This should be raised to TOMLkit to fix the copy behaviour (it should work). The above patch should probably also be applied as well.


Edit: You also mentioned a similar workaround; sorry I missed that.