Added Marshmallow validation to views
parent
ef75599675
commit
4f3ee63faf
@ -1,12 +1,25 @@
|
|||||||
from marshmallow import Schema, fields
|
from marshmallow import Schema, fields, ValidationError
|
||||||
|
|
||||||
|
|
||||||
|
def non_neg(value):
|
||||||
|
if not value >= 0:
|
||||||
|
raise ValidationError('Value cannot be negative')
|
||||||
|
|
||||||
|
|
||||||
|
def non_neg_non_zero(value):
|
||||||
|
if not value > 0:
|
||||||
|
raise ValidationError('Value cannot be negative or zero')
|
||||||
|
|
||||||
|
|
||||||
class CookieSchema(Schema):
|
class CookieSchema(Schema):
|
||||||
id = fields.Int()
|
id = fields.Int(dump_only=True)
|
||||||
created_date = fields.DateTime()
|
created_date = fields.DateTime(dump_only=True)
|
||||||
modified_date = fields.DateTime()
|
modified_date = fields.DateTime()
|
||||||
name = fields.Str(required=True)
|
name = fields.Str(required=True, error_messages={'required': "Cookie name is required"})
|
||||||
recipe_url = fields.Str()
|
recipe_url = fields.Str()
|
||||||
sku = fields.Str(required=True)
|
sku = fields.Str(required=True, error_messages={'required': "Cookie sku is required"})
|
||||||
qoh = fields.Int(required=True)
|
qoh = fields.Int(validate=non_neg, required=True, error_messages={'required': "Cookie qoh is required"})
|
||||||
unit_cost = fields.Decimal(required=True)
|
unit_cost = fields.Decimal(validate=non_neg_non_zero, required=True, error_messages={'required': "Cookie unit_cost is required"})
|
||||||
|
|
||||||
|
class Meta:
|
||||||
|
ordered = True
|
||||||
|
Loading…
Reference in New Issue