Created
March 1, 2022 12:09
-
-
Save aliemir/7f01a1a514434f45672e0fd7ca485a8f to your computer and use it in GitHub Desktop.
simple validation functions in typescript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const emailRegexp = | |
/[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?/i; | |
const alphabeticRegexp = /^[a-zA-Z\s]*$/g; | |
const specialsRegexp = /[`\+=\?!\]\[\$}{:\/&\\@#\|<>;]/g; | |
const nonNumericRegexp = /\D/g; | |
export const isEmail = (email?: string | number | undefined) => { | |
return Boolean(`${email ?? ''}`.match(emailRegexp)); | |
}; | |
export const isAlphabetic = (value?: string | number | undefined) => { | |
return Boolean(`${value ?? ''}`.length > 0 && alphabeticRegexp.test(`${value ?? ''}`)); | |
}; | |
export const hasSpecialCharacters = (value?: string | number | undefined) => { | |
return Boolean(`${value ?? ''}`.length > 0 && specialsRegexp.test(`${value ?? ''}`)); | |
}; | |
export const isNumeric = (value?: string | number | undefined) => { | |
return Boolean(`${value ?? ''}`.length > 0 && !nonNumericRegexp.test(`${value ?? ''}`)); | |
}; | |
export const isDefined = (value?: string | number | undefined) => { | |
return Boolean(typeof value !== 'undefined'); | |
}; | |
export const isNonEmpty = (value?: string | number | undefined) => { | |
return `${value ?? ''}`.length > 0; | |
}; | |
export const isNonEmptyString = (value?: string | number | undefined) => { | |
return `${value ?? ''}`.trim().length > 0; | |
}; | |
export const isGreaterThanLength = (length: number) => (value?: string | number | undefined) => { | |
return `${value ?? ''}`.length > length; | |
}; | |
export const isLessThanLength = (length: number) => (value?: string | number | undefined) => { | |
return `${value ?? ''}`.length < length; | |
}; | |
export const isEqualsToLength = (length: number) => (value?: string | number | undefined) => { | |
return `${value ?? ''}`.length === length; | |
}; | |
export const isEqual = (expected: string | number | undefined) => (value?: string | number | undefined) => { | |
return `${value ?? ''}` === expected; | |
}; | |
export const isGreaterThan = (expected: string | number | undefined) => (value?: string | number | undefined) => { | |
return parseInt(`${value ?? '0'}`) > parseInt(`${expected ?? '0'}`); | |
}; | |
export const isSmallerThan = (expected: string | number | undefined) => (value?: string | number | undefined) => { | |
return parseInt(`${value ?? '0'}`) < parseInt(`${expected ?? '0'}`); | |
}; | |
export const OR = | |
(...fns: ((value: string | number | undefined) => boolean)[]) => | |
(value?: string | number | undefined) => { | |
return fns.some((fn) => fn(value)); | |
}; | |
export const AND = | |
(...fns: ((value: string | number | undefined) => boolean)[]) => | |
(value?: string | number | undefined) => { | |
return fns.every((fn) => fn(value)); | |
}; | |
export const isNot = | |
(validator: (value?: string | number | undefined) => boolean) => (value?: string | number | undefined) => { | |
return !validator(value); | |
}; | |
export const validators = { | |
isEmail, | |
isNumeric, | |
isNonEmpty, | |
isGreaterThanLength, | |
isLessThanLength, | |
isEqualsToLength, | |
isNonEmptyString, | |
isNot, | |
isEqual, | |
isDefined, | |
OR, | |
}; | |
/** | |
* | |
* @param value value to be validated | |
* @param validationFns single or multiple functions of validation, function or keyof predefined validators | |
* @returns boolean | |
*/ | |
export const validate = ( | |
value: string | number | undefined, | |
...validationFns: ((value: string | number | undefined) => boolean)[] | |
) => { | |
return validationFns.every((fn) => fn(value)); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Example usage;