Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting regex pattern for mongoengine 0.9.0 StringField

I use python 2.7.12 with pymongo 2.8.1, django 1.11.7, mongoengine 0.9.0 and mongodb 3.4.10. I am creating a custom user model which inserts documents in a regular mongodb collection. I inherited the 'Document' class from mongoengine to create the string password field. I've been trying to create a regex pattern for the following conditions:

  • Length: 8-25 characters
  • At least 1 uppercase letter
  • At least 1 lowercase letter
  • At least 1 number
  • At least 1 special character

I tried using re.compile() in the following way:

import re
regexp = re.compile('[A-Za-z0-9@#$%^&+=]', max_length=25, min_length=8)
password = StringField(regex=regexp)

I sent HTTP POST requests with invalid passwords and it only throws an error when the length is lesser than 8 or greater than 25, otherwise they pass. I'm new to mongoengine and I'm not really sure what kind of input the regex parameter takes, neither have I been able to find examples for StringField. So what am I doing wrong?

like image 232
Shamail Mulla Avatar asked Dec 22 '25 10:12

Shamail Mulla


1 Answers

^(?=.*[A-Z])(?=.*[a-z])(?=.*[0-9])(?=.*[@#$%^&+=]).{8,25}$

You can try this.Each condition is added using lookahead.Add more special characters if you want.

See demo.

like image 141
vks Avatar answered Dec 23 '25 23:12

vks



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!