cyrtophora

[EXPERIMENTAL] secure web node
git clone https://gitlab.com/kwatafana/cyrtophora.git
Log | Files | Refs | README

validate.dart (1873B)


      1 import "package:cyrtophora/cyrtophora.dart";
      2 
      3 class Validate {
      4   /// Validate username
      5   static bool username(String username) {
      6     // Username cannot be empty
      7     if (username.isEmpty) {
      8       return false;
      9     }
     10 
     11     // Username length cannot be greater than 15 characters
     12     if (username.length > 15) {
     13       return false;
     14     }
     15 
     16     final chars = username.split("");
     17     var underscoreCount = 0;
     18 
     19     for (var i = 0; i < chars.length; i++) {
     20       // Username cannot start with a underscore (_)
     21       if (i == 0 && chars[i] == '_') {
     22         return false;
     23       }
     24 
     25       // Username can only contain letters, numbers, one dot and one underscore
     26       if (chars[i] != '_') {
     27         if (!isAlphanumeric(chars[i])) {
     28           return false;
     29         }
     30       } else {
     31         // Count underscores because name can have only one [ _ ] underscore
     32         underscoreCount += 1;
     33       }
     34     }
     35     // Username can have only one [ _ ] underscore
     36     if (underscoreCount > 1) {
     37       return false;
     38     }
     39     return true;
     40   }
     41 
     42   /// Validate fullname if it is provided
     43   static bool fullname(String fullname) {
     44     // Full Name cannot be empty
     45     if (fullname.isEmpty) {
     46       return false;
     47     }
     48 
     49     // Full Name cannot be more than 70 characters
     50     if (fullname.length > 70) {
     51       return false;
     52     }
     53 
     54     return true;
     55   }
     56 
     57   /// Validate password
     58   static bool password(String password) {
     59     // Password should be at least 10 characters long
     60     if (password.length < 10) {
     61       return false;
     62     }
     63     return true;
     64   }
     65 
     66   // TODO: better email verification
     67   /// Validate email address
     68   static bool email(String email) {
     69     // Email address cannot be empty
     70     if (email.isEmpty) {
     71       return false;
     72     }
     73 
     74     // Email address should contain @ and . symbols
     75     if (!email.contains('@') || !email.contains('.')) {
     76       return false;
     77     }
     78     return true;
     79   }
     80 }