The library serve a collection of validator functions.
npm install @baloise/web-app-validators
isCustom(validatorFn: BalValidatorFn) => BalValidatorFn
Returns true
if the value date is before the given date.
BalValidators.isCustom(value => value > 2)(3) // true
isBefore(date: any) => BalValidatorFn
Returns true
if the value date is before the given date
BalValidators.isBefore('2000-01-02')('2000-01-01') // true
BalValidators.isBefore(new Date(2020, 0, 2))(new Date(2020, 0, 1)) // true
isAfter(date: any) => BalValidatorFn
Returns true
if the value date is before the given date
BalValidators.isAfter('2000-01-01')('2000-01-02') // true
BalValidators.isAfter(new Date(2020, 0, 1))(new Date(2020, 0, 2)) // true
isDate() => BalValidatorFn
Returns true
if the value is valid date
BalValidators.isDate()('2000-01-02') // true
BalValidators.isDate()(new Date(2000, 0, 1)) // true
isMin(min: number) => BalValidatorFn
Returns true
if the number is bigger or equal than the min number
BalValidators.isMin(10)(10) // true
BalValidators.isMin(10)(11) // true
BalValidators.isMin(10)(9) // false
isMax(max: number) => BalValidatorFn
Returns true
if the number is smaller or equal than the max number
BalValidators.isMax(10)(10) // true
BalValidators.isMax(10)(9) // true
BalValidators.isMax(10)(11) // false
isNumber() => BalValidatorFn
Returns true
if the number is valid
BalValidators.isNumber()(10) // true
BalValidators.isNumber()('10') // true
BalValidators.isNumber()('a') // false
isMonetaryNumber() => BalValidatorFn
Returns true
if the value is a valid formatted number
BalValidators.isMonetaryNumber()(10) // true
BalValidators.isMonetaryNumber()(`1'000.99`) // true
BalValidators.isMonetaryNumber()(`a`) // false
matchesRegex(regex: RegExp) => BalValidatorFn
Returns true
if the value matches the regex
BalValidators.matchesRegex(new RegExp('^\\d+$'))('1') // true
isEmail() => BalValidatorFn
Returns true
if the value matches the regex
BalValidators.isEmail()('[email protected]') // true
isPhone() => BalValidatorFn
Returns true
if the value matches the regex
BalValidators.isPhone()('123 456 78 90') // true
isRequired() => BalValidatorFn
Returns true
if the value is a non-empty value
BalValidators.isRequired()('foo') // true
BalValidators.isRequired()('') // false
isRequiredTrue() => BalValidatorFn
Returns true
if the value is true. This validator is commonly used for required checkboxes.
BalValidators.isRequiredTrue()(true) // true
BalValidators.isRequiredTrue()('') // false
isMinLength(minLength: number) => BalValidatorFn
Returns true
if the string is bigger or equal than the min length
BalValidators.isMinLength(3)('123') // true
BalValidators.isMinLength(3)('12') // false
isMaxLength(maxLength: number) => BalValidatorFn
Returns true
if the string is smaller or equal than the max length
BalValidators.isMaxLength(3)('123') // true
BalValidators.isMaxLength(3)('1234') // false
validateConditionally(validatorFn: BalValidatorFn, conditionFn: BalValidatorFn) => BalValidatorFn
Returns true
if the condition is true and the validations is true too.
BalValidators.validateConditionally(
value => value > 2,
() => true,
)(3) // true
BalValidators.validateConditionally(
value => value > 2,
() => false,
)(3) // undefined