Typescript validation library
Sentinel provides lots of validations
const isValidNumber: boolean = new Sentinel(36, [Sentinel.isNumber]).isValid()
const isValidNumberGreaterThan10 = new Sentinel(15, [Sentinel.isNumber, Sentinel.isGreaterThan(10)]).isValid()
// Same results:
const isValidNumberGreaterThan10 = new Sentinel(15, Sentinel.all(Sentinel.isNumber, Sentinel.isGreaterThan(10))).isValid()
// isInvalid()
const isInvalidNumberGreaterThan100 = new Sentinel(15, [Sentinel.isNumber, Sentinel.isGreaterThan(100)]).isInvalid()
const validContact = new Sentinel('[email protected]', Sentinel.any(Sentinel.isEmail, Sentinel.isBrazilianCellPhone(true))).isValid()
Method | Assertation |
---|---|
isArray | Check if passed value is an array |
isNumber | Check if passed value is a number |
isRequired | Check if passed value is present |
isPresent | Alias for isRequired |
hasValue | Alias for isRequired |
hasMinLength(size) | Checks if string value has at least size chars |
hasMaxLength(size) | Checks if string value has maximum of size chars |
hasMinMaxLength(min, max) | Checks if string value has between min and max chars |
hasExactLength(size) | Checks if string value has exact size chars |
isLessThan(num) | Checks if number value is smaller than num |
isLessOrEqualThan(num) | Checks if number value is smaller than or equal to num |
isGreaterThan(num) | Checks if number value is bigger than num |
isGreaterOrEqualThan(num) | Checks if number value is bigger than or equal to num |
isEmail | Check if passed value is a valid email based on RFC 5322 |
isDate(format = undefined) | Check if passed value is a valid date |
isDateGreaterThan(referenceDate) | Check if date is bigger than referenceDate |
isPasswordValid(lowerPresent = true, upperPresent = true, numberPresent = true, specialPresent = true, minimumRules = 3, minChars = 8) | Checks if at least minimumRules are valid, considering boolean options: lowerPresent , upperPresent , numberPresent , specialPresent . Password must be minChars long. |
isCpfValid | Validates brazilian CPF |
isUSZipCodeValid | Validates US zip codes |
isBRZipCodeValid | Validates brazilian zip vodes |
isZipCodeValid | Validates US and brazilian zip codes |
isBRPhoneValid | Validates brazilian phones |
isBRCellPhoneValid | Validates brazilian cellphones |
Method | Assertion |
---|---|
Sentinel.all | All checks must be valid |
Sentinel.any | One check must be valid |
Method | Assertion |
---|---|
isValid | Return if assertions were validated |
isInvalid | Return if assertions were not validated |
- Modularize validations
- Modularize locale based validations
Pull requests are welcome!