All files / app/validators user-validators.ts

100% Statements 7/7
100% Branches 4/4
100% Functions 3/3
100% Lines 7/7

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43  1x                   1x         2x                     1x             178x 1x   177x          
import {ValidationChain} from 'express-validator';
import {Validators, ValidatorsImpl} from 'express-validator/src/chain';
 
export interface UserValidators extends Validators<ValidationChain> {
  isPassword(name?: string): ValidationChain;
  isUsername(name?: string): ValidationChain;
}
 
/**
 * Additional checks for user related checks.
 */
export class UserValidatorsImpl extends ValidatorsImpl<ValidationChain> {
  /**
   * Checks if field conforms to password rules.
   */
  isPassword(name = 'password'): ValidationChain {
    return this
        .isString()
        .withMessage(name + ' has to be a string')
        .isLength({min: 6, max: 255})
        .withMessage(name + ' has to be at least 6 characters long');
  }
 
  /**
   * Checks if conforms to username rules.
   */
  isUsername(name = 'username'): ValidationChain {
    return this
        .isString()
        .trim()
        .notEmpty()
        .isLength({min: 4, max: 25})
        .withMessage(name + ' has to be between 4 and 25 characters long')
        .custom((value: string) => {
          if (/\s/.test(value)) {
            throw new Error(name + ' should not contain whitespace');
          }
          return true;
        })
        .escape();
  }
}