sarugaku / plette

Structured Pipfile and Pipfile.lock models.

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Validation fails on tomlkit parsed data

frostming opened this issue · comments

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.

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:

def validate(cls, data):
if not cerberus: # Skip validation if Cerberus is not available.
return
schema = cls.__SCHEMA__
key = id(schema)
try:
v = VALIDATORS[key]
except KeyError:
v = VALIDATORS[key] = cerberus.Validator(schema, allow_unknown=True)
if v.validate(data, normalize=False):
return
raise ValidationError(data, v)

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.