Create schema for RequestVerifyCodeSchema

Previously we were using a schema that mapped onto db.Model. However, the json
in the request did not reflect the VerfiyCode db Model.
I did not add validation on the to field, we did not have that previously.
This commit is contained in:
Rebecca Law
2016-02-01 10:48:33 +00:00
parent 8c20c3e7be
commit cec0d40e5b
3 changed files with 37 additions and 22 deletions

View File

@@ -1,6 +1,8 @@
from flask_marshmallow.fields import fields
from . import ma
from . import models
from marshmallow import post_load
from marshmallow import post_load, ValidationError
# TODO I think marshmallow provides a better integration and error handling.
# Would be better to replace functionality in dao with the marshmallow supported
@@ -9,7 +11,6 @@ from marshmallow import post_load
class BaseSchema(ma.ModelSchema):
def __init__(self, *args, load_json=False, **kwargs):
self.load_json = load_json
super(BaseSchema, self).__init__(*args, **kwargs)
@@ -27,7 +28,6 @@ class BaseSchema(ma.ModelSchema):
class UserSchema(BaseSchema):
class Meta:
model = models.User
exclude = (
@@ -36,36 +36,35 @@ class UserSchema(BaseSchema):
class ServiceSchema(BaseSchema):
class Meta:
model = models.Service
exclude = ("updated_at", "created_at", "api_keys", "templates", "jobs", "queue_name")
class TemplateSchema(BaseSchema):
class Meta:
model = models.Template
exclude = ("updated_at", "created_at", "service_id", "jobs")
class ApiKeySchema(BaseSchema):
class Meta:
model = models.ApiKey
exclude = ("service", "secret")
class JobSchema(BaseSchema):
class Meta:
model = models.Job
class VerifyCodeSchema(BaseSchema):
class Meta:
model = models.VerifyCode
exclude = ('user', "_code", "expiry_datetime", "code_used", "created_at")
class RequestVerifyCodeSchema(ma.Schema):
def verify_code_type(self):
if self not in ['sms', 'email']:
raise ValidationError('Invalid code type')
code_type = fields.Str(required=True, validate=verify_code_type)
to = fields.Str(required=False)
user_schema = UserSchema()
@@ -83,4 +82,4 @@ api_keys_schema = ApiKeySchema(many=True)
job_schema = JobSchema()
job_schema_load_json = JobSchema(load_json=True)
jobs_schema = JobSchema(many=True)
verify_code_schema = VerifyCodeSchema()
request_verify_code_schema = RequestVerifyCodeSchema()