StringField in mongodb requirements

I'm wondering if I can add a requirement to my User document setup to validate a specific string. The idea is when a custom document is created with an email address, I want to make sure the email is from college, so it should end with ".edu", example: " john.doe@college.edu " is acceptable, but " john. doe@gmail.com "not

Here is my code:

class User(db.Document, UserMixin):
    name = db.StringField(max_length=255, unique=True)
    email = db.StringField(max_length=255, unique=True)
    phone = db.StringField(max_length=255, unique=True)
    password = db.StringField(max_length=255)
    active = db.BooleanField(default=True)
    confirmed_at = db.DateTimeField()
    roles = db.ListField(db.ReferenceField(Role), default=[])

      

+3


source to share


1 answer


There is an optional regex

argument
that you can define:

email = db.StringField(max_length=255, unique=True, regex=r'.*?boston\.edu$')

      



And besides, why not use the specific one EmailField

in this case:

email = db.EmailField(max_length=255, unique=True, regex=r'.*?boston\.edu$')

      

+3


source







All Articles