Schemas: ValidateEmail

Validating an email type field.

🪶 The validateEmail option in MerlinDB is useful when you want to validate an email
  • .

    1
  • import
  • MerlinDB,
  • {
  • Schema
  • }
  • from
  • "@chrisaxxwell/merlin-db";
    2
  • //Or if you are using 'merlindb.max.js' just call 'new MerlinDB()';
  • 3
    4
  • const
  • merlin =
  • new
  • MerlinDB(
  • )
  • ;
    5merlin
  • .connect(
  • "MY-STORE-DATABASE"
  • )
  • ;
    6
    7
  • const
  • ClientSchema =
  • Schema(
  • {
  • 8  
  • email:
  • {
  • 9      
  • type:
  • String
  • ,
    10      
  • unique:
  • true
  • ,
    11      
  • validateEmail:
  • [
    12        
  • true
  • ,
    13         "Invalid email",  
    14          /[your-regex-to-validate-email]/g  
    15      ]
    16  
  • }
  • ,
    17
  • }
  • )
  • ;
    18
    19
  • var
  • clientModel = merlin
  • .model(
  • "Users", ClientSchema
  • )
  • ;
    20
    21clientModel
  • .insert(
  • {
  • 22  
  • email:
  • "lalala"
    23
  • }
  • )
  • .then(
  • e =>
  • {
  • 24  
  • /*  OK...  */
  • 25
  • }
  • )
  • .catch(
  • e =>
  • {
  • 26   console
  • .log(
  • e
  • )
  • ;
    27  
  • //returns Invalid email
  • 28
  • }
  • )
  • ;

    By default the email regex is '/^[a-zA-Z0-9!#$%&*_=.+}/?'{~|~-]{2,}(?<!\.)@[a-z]{2,}\.[a-z]{2,}(\.[a-z]{2,})?$/g'

    You can change the regex in the 'validateEmail' property in the second index.
    1: (ccTLD): The email is considered valid if it has less than 1 ccTLD, for example chris@gmail.com.br is valid, but chris@gmail .com.br.br is not valid;
    2: (.>...@): Emails with . before @ are not allowed.
    3: (not allowed): Emails with :;><,][)¨(`^ are not allowed.
    4: (@): Emails more than 1 @ are not allowed.
    5: (min@): Minimum of 2 characters allowed before @;