@validarium/validations

0.9.0 • Public • Published

Validations

Validarium

by Lundegaard

🖍️ 🛡 🚀

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!

See our documentation site.

Github MIT License Travis Downloads Version

This package contains common validation functions ## API reference

validations

This package contains common validation functions


validations.hasNoWhiteSpace ⇒ Object

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}

validations.isInteger ⇒ Object

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}

validations.isNegativeNumber ⇒ Object

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}

validations.isPositiveNumber ⇒ Object

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}

validations.hasAgeInInterval(minAge, maxAge) ⇒ Object

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}

validations.hasValueInInterval(min, max) ⇒ Object

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}

validations.hasValueMax(max) ⇒ Object

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}

validations.hasValueMin(min) ⇒ Object

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}

validations.isDivisibleBy(divisor) ⇒ Object

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}

validations.isRequired ⇒ Object

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}

validations.isRequiredNumber ⇒ Object

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}

validations.hasNoSpecialSymbols ⇒ Object

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}

validations.hasOnlyDigits ⇒ Object

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}

validations.isEmail ⇒ Object

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}

validations.isNumber ⇒ Object

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}

validations.visPhoneNumber ⇒ Object

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}

validations.isString ⇒ Object

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}

validations.isTrimmed ⇒ Object

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}

validations.isTrimmedLeft ⇒ Object

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}

validations.isTrimmedRight ⇒ Object

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}

validations.isValidIban ⇒ Object

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}

validations.matches ⇒ Object

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}

validations.hasDateMax(max) ⇒ Object

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}

validations.hasDateMin(min) ⇒ Object

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}

validations.hasLength(length) ⇒ Object

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}

validations.hasLengthInInterval(min, max) ⇒ Object

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}

validations.hasLengthMax(max) ⇒ Object

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}

validations.hasLengthMin(min) ⇒ Object

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}

validations.isNumber(list) ⇒ Object

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}

validations.isOneOf(list) ⇒ Object

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}

validations.startsWith() ⇒ Object

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.

Install

DownloadsWeekly Downloads

87

Version

0.9.0

License

MIT

Unpacked Size

228 kB

Total Files

39

Last publish

Collaborators

  • zstanek
  • tommmyy
  • xzayda
  • kohout.jakub