@darkwolf/validator
Advanced tools
Comparing version 1.0.2 to 1.1.0
51
index.js
const CodeError = require('@darkwolf/code-error') | ||
const errors = require('@darkwolf/errors') | ||
@@ -11,2 +12,7 @@ class Validator { | ||
this.isArray = this.isArray.bind(this) | ||
this.isUsername = this.isUsername.bind(this) | ||
this.isEmail = this.isEmail.bind(this) | ||
this.isPhoneNumber = this.isPhoneNumber.bind(this) | ||
this.isDarkwolfId = this.isDarkwolfId.bind(this) | ||
this.isPassword = this.isPassword.bind(this) | ||
} | ||
@@ -66,2 +72,22 @@ | ||
isUsername(value) { | ||
return /^(?!.*_{2,}.*)[a-z][a-z\d_]{3,30}[a-z\d]$/.test(value) | ||
} | ||
isEmail(value) { | ||
return /^(?![^A-Z]*[A-Z])\S+@\S+\.\S+$/.test(value) | ||
} | ||
isPhoneNumber(value) { | ||
return /^\+\d{5,15}$/.test(value) | ||
} | ||
isDarkwolfId(value) { | ||
return this.isUsername(value) || this.isEmail(value) || this.isPhoneNumber(value) | ||
} | ||
isPassword(value) { | ||
return /^(?=[^A-Z]*[A-Z])(?=[^a-z]*[a-z])(?=\D*\d)(?=[^!@#$%^&*]*[!@#$%^&*])[\w!@#$%^&*]{6,20}$/.test(value) | ||
} | ||
isValidBoolean(value, name, code, data) { | ||
@@ -120,4 +146,29 @@ if (!this.isBoolean(value)) { | ||
} | ||
isValidUsername(value) { | ||
if (!this.isUsername(value)) throw errors.INVALID_USERNAME | ||
return true | ||
} | ||
isValidEmail(value) { | ||
if (!this.isEmail(value)) throw errors.INVALID_EMAIL | ||
return true | ||
} | ||
isValidPhoneNumber(value) { | ||
if (!this.isPhoneNumber(value)) throw errors.INVALID_PHONE_NUMBER | ||
return true | ||
} | ||
isValidDarkwolfId(value) { | ||
if (!this.isDarkwolfId(value)) throw errors.INVALID_DARKWOLF_ID | ||
return true | ||
} | ||
isValidPassword(value) { | ||
if (!this.isPassword(value)) throw errors.INVALID_PASSWORD | ||
return true | ||
} | ||
} | ||
module.exports = new Validator() |
{ | ||
"name": "@darkwolf/validator", | ||
"version": "1.0.2", | ||
"version": "1.1.0", | ||
"description": "Validator", | ||
@@ -23,4 +23,5 @@ "main": "index.js", | ||
"dependencies": { | ||
"@darkwolf/code-error": "^1.0.2" | ||
"@darkwolf/code-error": "^1.0.2", | ||
"@darkwolf/errors": "^1.1.0" | ||
} | ||
} |
4911
140
2
+ Added@darkwolf/errors@^1.1.0