Skip to content

Commit

Permalink
Additional schema validations
Browse files Browse the repository at this point in the history
  • Loading branch information
miguelgrinberg committed Dec 26, 2021
1 parent 224bb16 commit 0b61c67
Showing 1 changed file with 15 additions and 5 deletions.
20 changes: 15 additions & 5 deletions api/schemas.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from marshmallow import validates_schema, ValidationError, post_dump
from api import ma
from marshmallow import validate, validates, validates_schema, \
ValidationError, post_dump
from api import ma, db
from api.models import User, Post

paginated_schema_cache = {}
Expand Down Expand Up @@ -64,16 +65,24 @@ class Meta:

id = ma.auto_field(dump_only=True)
url = ma.String(dump_only=True)
username = ma.auto_field(required=True)
email = ma.auto_field(required=True)
password = ma.String(required=True, load_only=True)
username = ma.auto_field(required=True,
validate=validate.Length(min=3, max=64))
email = ma.auto_field(required=True, validate=[validate.Length(max=120),
validate.Email()])
password = ma.String(required=True, load_only=True,
validate=validate.Length(min=3))
avatar_url = ma.String(dump_only=True)
about_me = ma.auto_field()
first_seen = ma.auto_field(dump_only=True)
last_seen = ma.auto_field(dump_only=True)
posts_url = ma.URLFor('posts.user_all', values={'id': '<id>'},
dump_only=True)

@validates('username')
def validate_username(self, value):
if db.session.scalar(User.select().filter_by(username=value)):
raise ValidationError('Use a different username.')

@post_dump
def fix_timestamp(self, data, **kwargs):
data['first_seen'] += 'Z'
Expand All @@ -98,6 +107,7 @@ def fix_datetimes(self, data, **kwargs):
data['timestamp'] += 'Z'
return data


class TokenSchema(ma.Schema):
class Meta:
ordered = True
Expand Down

0 comments on commit 0b61c67

Please sign in to comment.