Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validation does not work #62

Open
sooslaca opened this issue Sep 25, 2018 · 2 comments
Open

validation does not work #62

sooslaca opened this issue Sep 25, 2018 · 2 comments

Comments

@sooslaca
Copy link

Hi guys,

I try to do a simple validation with dynamorm and marshmallow

bash-4.4# pip list | egrep "dynamo|mars"
dynamorm            0.9.1
flask-marshmallow   0.9.0
marshmallow         2.15.6

code:

from dynamorm import DynaModel, GlobalIndex, ProjectAll, ProjectInclude
from marshmallow import fields, validate, validates, ValidationError, validates_schema

class PSubnet(DynaModel):
    class Table: 
        name = 'p_subnet'
        hash_key = 'id'
        read = 15
        write = 5
    class Schema:
        id = fields.String()
        test = fields.String(validate=validate_test, required=True)

validate_test is:

def validate_test(value):
    try:
        if value.isalnum():
            return True
    finally:
        raise ValidationError('Test must be alphanumeric')

I then do:

PSubnet.Schema.dynamorm_validate({
        "id": "xxx",
        "test": "+!%/=()",
},native=False)

When I do a simple validation with native = False (this is the default) it fails. with native = True it works

Is native=False expected to work?

Not seeing too much in the documentation or in the code, only in types/base.py:

        If native is true then the underlying validation library should return a dictionary of native python values
        (i.e. datetime.datetime), otherwise it should return a dictionary of primitive values (i.e. a string
        representation of a date time value).

Thanks

@Sharadh
Copy link
Contributor

Sharadh commented Nov 21, 2019

Looks like this is because Schema.load(), which actually does validation, is not called in the native=False case:

if native:
data = cls().load(obj, partial=partial, unknown="EXCLUDE")
else:
data = cls(partial=partial, unknown="EXCLUDE").dump(obj)

if native:
data, errors = cls().load(obj, partial=partial)
else:
data, errors = cls(partial=partial).dump(obj)

I faced the same issue with schematics validation, where .validate() is not explicitly being called (although some amount of "conversion" validation does occur):

inst = cls(obj, strict=False, partial=partial)

@Sharadh
Copy link
Contributor

Sharadh commented Nov 21, 2019

I opened #92 to fix the schematics case. Potentially, the marshmallow issues can also be addressed there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants