I'm using marshmallow for validate some fields of a schema.
class PlantDetailsSchema(Schema):
name: fields.Str(required=True, validate=validate.Length(min=3)),
sprout-time: fields.Str(required=True, validate=validate.Length(min=3)),
full-growth: fields.Str(required=True, validate=validate.Length(min=5)),
edible: fields.Bool(required=True)
class PlantInfoSchema(Schema):
plant = fields.Nested(PlantDetailsSchema)
num = fields.Int(required=True, validate=validate.Range(min=1))
def validate_json(json):
try:
schema = PlantInfoSchema().load(json)
ret = schema.dump()
return ret
except ValidationError:
return None
The json element to validate is: json = {'plant': {'name': 'Bonsai', 'sprout-time': '3 months', 'full-growth': '2 years', 'edible': False}, 'num': 3}
My issue is how to validate fields that contains a dash in the middle of the name (like 'sprout-time' and 'full-growth'. Do you have some ideas to solve this issue?