Validations
🖍️ 🛡 🚀
Validations done right.
Platform-agnostic validation library for JavaScript applications with extra focus on composable validations and message translations. Includes (pretty much) out-of-the-box support for both Redux Form and React Intl.
Create your validation schema based on priciples of functional programming.
Open Validarium in a RunKit sandbox!
This package contains common validation functions ## API referencevalidations
This package contains common validation functions
-
validations
-
number
-
.hasNoWhiteSpace ⇒
Object
-
.isInteger ⇒
Object
-
.isNegativeNumber ⇒
Object
-
.isPositiveNumber ⇒
Object
-
.hasAgeInInterval(minAge, maxAge) ⇒
Object
-
.hasValueInInterval(min, max) ⇒
Object
-
.hasValueMax(max) ⇒
Object
-
.hasValueMin(min) ⇒
Object
-
.isDivisibleBy(divisor) ⇒
Object
-
.hasNoWhiteSpace ⇒
-
other
-
.isRequired ⇒
Object
-
.isRequiredNumber ⇒
Object
-
.isRequired ⇒
-
string
-
.hasNoSpecialSymbols ⇒
Object
-
.hasOnlyDigits ⇒
Object
-
.isEmail ⇒
Object
-
.isNumber ⇒
Object
-
.visPhoneNumber ⇒
Object
-
.isString ⇒
Object
-
.isTrimmed ⇒
Object
-
.isTrimmedLeft ⇒
Object
-
.isTrimmedRight ⇒
Object
-
.isValidIban ⇒
Object
-
.matches ⇒
Object
-
.hasDateMax(max) ⇒
Object
-
.hasDateMin(min) ⇒
Object
-
.hasLength(length) ⇒
Object
-
.hasLengthInInterval(min, max) ⇒
Object
-
.hasLengthMax(max) ⇒
Object
-
.hasLengthMin(min) ⇒
Object
-
.isNumber(list) ⇒
Object
-
.isOneOf(list) ⇒
Object
-
.startsWith() ⇒
Object
-
.hasNoSpecialSymbols ⇒
-
number
Object
validations.hasNoWhiteSpace ⇒ Checks if a string contains no white space.
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Example
> hasNoWhiteSpace("")
null
> hasNoWhiteSpace("validarium")
null
> hasNoWhiteSpace("vali darium")
{message Object}
Object
validations.isInteger ⇒ Checks if the value is an integer
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Example
> isInteger(2)
null
> isInteger(2.1)
{message Object}
Object
validations.isNegativeNumber ⇒ Checks if the value is a negative number
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Example
> isNegativeNumber(-5)
null
> isNegativeNumber(5)
{message Object}
> isNegativeNumber(0)
{message Object}
Object
validations.isPositiveNumber ⇒ Checks if the value is a positive number
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Example
> isPositiveNumber(5)
null
> isPositiveNumber(-5)
{message Object}
> isPositiveNumber(0)
{message Object}
Object
validations.hasAgeInInterval(minAge, maxAge) ⇒ Checks if the age is in specified interval
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Param | Type | Description |
---|---|---|
minAge | number |
minimal age |
maxAge | number |
maximal age |
Example
> hasAgeInInterval(1, 3)(2)
null
> hasAgeInInterval(1, 3)(3)
null
> hasAgeInInterval(1, 3)(5)
{message Object}
Object
validations.hasValueInInterval(min, max) ⇒ Checks if the value has only digits
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Param | Type | Description |
---|---|---|
min | number |
lower interval endpoint |
max | number |
upper interval endpoint |
Example
> hasValueInInterval(1, 3)(2)
null
> hasValueInInterval(1, 3)(3)
null
> hasValueInInterval(1, 3)(5)
{message Object}
Object
validations.hasValueMax(max) ⇒ Checks if the value is lower or equal to max
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Param | Type | Description |
---|---|---|
max | number |
maximum value |
Example
> hasValueMax(2)(1)
null
> hasValueMax(2)(2)
null
> hasValueMax(2)(3)
{message Object}
Object
validations.hasValueMin(min) ⇒ Checks if the value is higher or equal to min
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Param | Type | Description |
---|---|---|
min | number |
minimum value |
Example
> hasValueMin(2)(3)
null
> hasValueMin(2)(2)
null
> hasValueMin(2)(1)
{message Object}
Object
validations.isDivisibleBy(divisor) ⇒ Checks is value is divisible by desired divisor
Returns: Object
- {message Object} when predicate fails or null when pass
Category: number
Param | Type | Description |
---|---|---|
divisor | number |
divisor |
Example
> isDivisibleBy(5)(10)
null
> isDivisibleBy(6)(10)
{message Object}
Object
validations.isRequired ⇒ Checks if the value is present
Returns: Object
- {message Object} when predicate fails or null when pass
Category: other
Example
> isRequired('abc')
null
> isRequired(null)
{message Object}
Object
validations.isRequiredNumber ⇒ Checks if the value is present
Returns: Object
- {message Object} when predicate fails or null when pass
Category: other
Example
> isRequiredNumber(0)
null
> isRequiredNumber(null)
{message Object}
Object
validations.hasNoSpecialSymbols ⇒ Checks if the value doesn't contain any special symbol
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> hasNoSpecialSymbols('abc')
null
> hasNoSpecialSymbols('a%b')
{message Object}
Object
validations.hasOnlyDigits ⇒ Checks if the value has only digits
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> hasOnlyDigits('123')
null
> hasOnlyDigits('12n3')
{message Object}
Object
validations.isEmail ⇒ Checks if the value is valid email
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isEmail('email@postemail.em')
null
> isEmail('emailpostemail')
{message Object}
Object
validations.isNumber ⇒ Checks if the value is a number
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isNumber('3')
null
> isNumber('abc')
{message Object}
Object
validations.visPhoneNumber ⇒ Checks if the value is a valid phone number
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isPhoneNumber('980123456')
null
> isPhoneNumber('23')
{message Object}
Object
validations.isString ⇒ Checks if value is type of string
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isString('abc')
null
> isString('')
null
> isString(123)
{message Object}
Object
validations.isTrimmed ⇒ Checkes if the string do not starts or ends with whitespaces.
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isTrimmed('valid value')
null
> isTrimmed(' invalid value')
{message Object}
> isTrimmed('invalid value ')
{message Object}
> isTrimmed(' invalid value ')
{message Object}
Object
validations.isTrimmedLeft ⇒ Checkes if the string do not starts with whitespaces.
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isTrimmedLeft('valid value')
null
> isTrimmedLeft(' invalid value')
{message Object}
Object
validations.isTrimmedRight ⇒ Checkes if the string do not ends with whitespaces.
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isTrimmedRight('valid value')
null
> isTrimmedRight('invalid value ')
{message Object}
Object
validations.isValidIban ⇒ Checks if the value is a valid IBAN
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> isValidIban('CH4217423156868474686')
null
> isValidIban('CZ123')
{message Object}
Object
validations.matches ⇒ Checks if value matches the given predicate
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Type | Description |
---|---|
string |
predicate |
Example
> matches('/([a-z]a)/g')('banana')
null
> matches('/([a-z]a)/g')('blueberry')
{message Object}
Object
validations.hasDateMax(max) ⇒ Checks if the the date is maximally the specified value
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
max | string |
maximum date |
Example
> hasDateMax('2032-07-01')('2020-07-01')
null
> hasDateMax('2032-07-01')('2032-07-01')
null
> hasDateMax('2032-07-01')('2032-07-02')
{message Object}
Object
validations.hasDateMin(min) ⇒ Checks if the the date is minimally the specified value
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
min | string |
minimal date |
Example
> hasDateMin('2032-07-01')('2040-07-01')
null
> hasDateMin('2032-07-01')('2032-07-01')
null
> hasDateMin('2032-07-02')('2032-07-01')
{message Object}
Object
validations.hasLength(length) ⇒ Checks if the value has exact length
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
length | number |
desired length for value |
Example
> hasLength(6)('abcdef')
null
> hasLength(6)('abc')
{message Object}
Object
validations.hasLengthInInterval(min, max) ⇒ Checks if the value has length in interval
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
min | number |
lower interval endpoint |
max | number |
upper interval endpoint |
Example
> hasLengthInInterval(2, 6)('abcd')
null
> hasLengthInInterval(2, 6)('abcdef')
null
> hasLengthInInterval(2, 6)('a')
{message Object}
Object
validations.hasLengthMax(max) ⇒ Checks if the values length is lower or equal to max
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
max | number |
maximum value |
Example
> hasLengthMax(2)('a')
null
> hasLengthMax(2)('ab')
null
> hasLengthMax(2)('abc')
{message Object}
Object
validations.hasLengthMin(min) ⇒ Checks if the values length is higher or equal to min
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
min | number |
minimum value |
Example
> hasLengthMin(2)('abc')
null
> hasLengthMin(2)('ab')
null
> hasLengthMin(2)('a')
{message Object}
Object
validations.isNumber(list) ⇒ Checks if the value doesn't equal any list item
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
list | array |
array of values |
Example
> isNotOneOf(['apple', 'pineapple', 'banana'])('banana')
null
> isNotOneOf(['apple', 'pineapple', 'banana'])('blueberry')
{message Object}
Object
validations.isOneOf(list) ⇒ Checks if the value equals any list item
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Param | Type | Description |
---|---|---|
list | array |
array of values |
Example
> isOneOf(['apple', 'pineapple', 'banana'])('banana')
null
> isOneOf(['apple', 'pineapple', 'banana'])('blueberry')
{message Object}
Object
validations.startsWith() ⇒ Checks if the value starts with specific string
Returns: Object
- {message Object} when predicate fails or null when pass
Category: string
Example
> startsWith('dog')('dogo')
null
> startsWith('dog')('cato')
{message Object}
Related projects
- @redux-tools – Maintaining large Redux applications with ease.
- react-union – Integrate React apps into various CMSs seamlessly.
- lundium – Beautiful React component library.
License
All packages are distributed under the MIT license. See the license here.