Validation
Package, implementation and tests
This commit is contained in:
@@ -0,0 +1,52 @@
|
||||
import * as EmailValidator from 'email-validator'
|
||||
import logger from 'log'
|
||||
import zxcvbn from 'zxcvbn'
|
||||
import {parseDocument} from 'htmlparser2'
|
||||
|
||||
let _verbose: boolean = false
|
||||
export function verbose(v: boolean) {
|
||||
_verbose = v
|
||||
}
|
||||
|
||||
const log = logger('validation')
|
||||
|
||||
export enum PasswordStrength {
|
||||
Weak = 0,
|
||||
Easy = 1,
|
||||
Medium = 2,
|
||||
Strong = 3,
|
||||
Hard = 4,
|
||||
}
|
||||
|
||||
export function password_is_strong(password: string, strength: PasswordStrength = PasswordStrength.Medium, email?: string) : boolean {
|
||||
log.trace("Verify password's strength")
|
||||
|
||||
if (zxcvbn(password, email !== undefined ? [email] : []).score < strength) {
|
||||
if (_verbose) log.warn("Password is too weak")
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
export function contains_html(text: string) : boolean {
|
||||
log.trace("Verify if text contains HTML")
|
||||
|
||||
if (parseDocument(text).children.some(node => node.nodeType === 1)) {
|
||||
if (_verbose) log.warn("")
|
||||
return true
|
||||
}
|
||||
|
||||
return false
|
||||
}
|
||||
|
||||
export function email_is_valid(email: string) : boolean {
|
||||
log.trace("Verify email's validity")
|
||||
|
||||
if (!EmailValidator.validate(email)) {
|
||||
if (_verbose) log.warn("Invalid email")
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
Reference in New Issue
Block a user