138 lines
3.7 KiB
JavaScript
138 lines
3.7 KiB
JavaScript
import { translate } from '@/plugins/i18n'
|
|
|
|
// 👉 IsEmpty
|
|
export const isEmpty = (value) => {
|
|
if (value === null || value === undefined || value === '')
|
|
return true
|
|
|
|
return !!(Array.isArray(value) && value.length === 0)
|
|
}
|
|
// 👉 IsNullOrUndefined
|
|
export const isNullOrUndefined = (value) => {
|
|
return value === null || value === undefined
|
|
}
|
|
|
|
// 👉 IsEmptyArray
|
|
export const isEmptyArray = (arr) => {
|
|
return Array.isArray(arr) && arr.length === 0
|
|
}
|
|
|
|
// 👉 Required Validator
|
|
export const requiredValidator = (value) => {
|
|
if (isNullOrUndefined(value) || isEmptyArray(value) || value === false)
|
|
return translate('validation.requiredField')
|
|
|
|
return !!String(value).trim().length || translate('validation.requiredField')
|
|
}
|
|
|
|
// 👉 Email Validator
|
|
export const emailValidator = (value) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
const re = /^(([^<>()\[\]\\.,;:\s@"]+(\.[^<>()\[\]\\.,;:\s@"]+)*)|(".+"))@((\[[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\])|(([a-zA-Z\-0-9]+\.)+[a-zA-Z]{2,}))$/
|
|
|
|
if (Array.isArray(value))
|
|
return value.every(val => re.test(String(val))) || translate('validation.emailField')
|
|
|
|
return re.test(String(value)) || translate('validation.emailField')
|
|
}
|
|
|
|
// 👉 Password Validator
|
|
export const passwordValidator = (password) => {
|
|
const regExp = /(?=.*\d)(?=.*[a-z])(?=.*[A-Z])(?=.*[!@#$%&*()]).{8,}/
|
|
|
|
const validPassword = regExp.test(password)
|
|
|
|
return (
|
|
// eslint-disable-next-line operator-linebreak
|
|
validPassword ||
|
|
translate('validation.passwordField')
|
|
)
|
|
}
|
|
|
|
// 👉 Confirm Password Validator
|
|
export const confirmedValidator = (value, target) =>
|
|
|
|
value === target || translate('validation.confirmedField')
|
|
|
|
// 👉 Between Validator
|
|
export const betweenValidator = (value, min, max) => {
|
|
const valueAsNumber = Number(value)
|
|
|
|
return (Number(min) <= valueAsNumber && Number(max) >= valueAsNumber)
|
|
|| translate('validation.betweenField', { min, max })
|
|
}
|
|
|
|
// 👉 Integer Validator
|
|
export const integerValidator = (value) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
if (Array.isArray(value))
|
|
return value.every(val => /^-?[0-9]+$/.test(String(val))) || translate('validation.integerField')
|
|
|
|
return /^-?[0-9]+$/.test(String(value)) || translate('validation.integerField')
|
|
}
|
|
|
|
// 👉 Regex Validator
|
|
export const regexValidator = (value, regex) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
let regeX = regex
|
|
if (typeof regeX === 'string')
|
|
regeX = new RegExp(regeX)
|
|
|
|
if (Array.isArray(value))
|
|
return value.every(val => regexValidator(val, regeX))
|
|
|
|
return regeX.test(String(value)) || translate('validation.regexField')
|
|
}
|
|
|
|
// 👉 Alpha Validator
|
|
export const alphaValidator = (value) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
return /^[A-Z]*$/i.test(String(value)) || translate('validation.alphaField')
|
|
}
|
|
|
|
// 👉 URL Validator
|
|
export const urlValidator = (value) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
const re = /^(http[s]?:\/\/){0,1}(www\.){0,1}[a-zA-Z0-9\.\-]+\.[a-zA-Z]{2,5}[\.]{0,1}/
|
|
|
|
return re.test(String(value)) || translate('validation.urlField')
|
|
}
|
|
|
|
// 👉 Time Validator
|
|
export const timeValidator = (value) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
const re = /^(?:[01]\d|2[0-3]):[0-5]\d$/
|
|
|
|
return re.test(String(value)) || translate('validation.timeField')
|
|
}
|
|
|
|
// 👉 Length Validator
|
|
export const lengthValidator = (value, length) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
return String(value).length === length || translate('validation.lengthedField', { length })
|
|
}
|
|
|
|
// 👉 Alpha-dash Validator
|
|
export const alphaDashValidator = (value) => {
|
|
if (isEmpty(value))
|
|
return true
|
|
|
|
const valueAsString = String(value)
|
|
|
|
return /^[0-9A-Z_-]*$/i.test(valueAsString) || translate('validation.alphaDashField')
|
|
}
|