Marshmallow: How to Validate ENUM field using marshmallow ?

Created on 9 Dec 2019  路  2Comments  路  Source: marshmallow-code/marshmallow

I have been using marshmallow for API's request json validation ,Its a great Library . Now i came across a situation where i have to validate ENUM filed . I have a ENUM field called 'gender' which will expect 3 values ('Male', 'Female', 'Others'). So here how do i add this validation field to Schema. Below i have mentioned one example .

from marshmallow import Schema, fields, validate
class UserDetails(Schema):
user_id = fields.Integer(required=True, validate=validate.Range(min=0,
error="user_id is invalid, it should be a positive integer."))
user_name = fields.String(required=True, validate=validate.Length(min=1, error="Field should not
be empty."))

user_gender = ?

Thanks in Advanced .

question

Most helpful comment

See the docs:

https://marshmallow.readthedocs.io/en/stable/quickstart.html#validation

permission = fields.Str(validate=validate.OneOf(["read", "write", "admin"]))

All 2 comments

See the docs:

https://marshmallow.readthedocs.io/en/stable/quickstart.html#validation

permission = fields.Str(validate=validate.OneOf(["read", "write", "admin"]))

Thanks a lot @lafrech

Was this page helpful?
0 / 5 - 0 ratings

Related issues

manoadamro picture manoadamro  路  3Comments

vke-code picture vke-code  路  4Comments

jayennis22 picture jayennis22  路  4Comments

imhoffd picture imhoffd  路  3Comments

DenisKuplyakov picture DenisKuplyakov  路  4Comments