validations-js
v1.0.1
Published
Simple DSL for performing simple validations.
Downloads
4
Readme
README
This document explains how to use the library.
Usage
First, you need to import the package into your code:
require('validations-js');
...
Then, follow these instructions for each function:
isEven(number)
Checks if a number is even.
Parameters:
number: int value.
Examples:
>> isEven(2)
>> true
>> isEven(1)
>> false
isOdd(number)
Checks if a number is odd.
Parameters:
number: int value.
Examples:
>> isOdd(2)
>> false
>> isOdd(1)
>> true
isPositive(number)
Checks if a number is positive or zero.
Parameters:
number: numeric value.
Examples:
>> isPositive(2)
>> true
>> isPositive(0.0)
>> true
>> isPositive(-3.1)
>> false
nonPositive(number)
Checks if a number is strictly negative
Parameters:
number: numeric value.
Examples:
>> nonPositive(2)
>> false
>> nonPositive(0.0)
>> false
>> nonPositive(-3.1)
>> true
isAlphabetic(string)
Checks if an string contains only letters.
Parameters:
string: string value.
Examples:
>> isAlphabetic("Hello world")
>> true
>> isAlphabetic("This w11l be f4ls3")
>> false
isAlphanumeric(string)
Checks if a string contains only letters and numbers.
Parameters:
string: string value.
Examples:
>> isAlphanumeric("Hello world")
>> true
>> isAlphanumeric("This w0n'7 be f4ls3")
>> true
>> isAlphanumeric("?????")
>> false
isNumeric(string)
Checks if a string contains only numbers.
Parameters:
string: string value.
Examples:
>> isNumeric("Hello world")
>> false
>> isNumeric("This w111 be f4ls3")
>> false
>> isNumeric("1234")
>> true
>> isNumeric("3.1415")
>> false
isValidPercentage(number)
Checks that a number that represents a percentage is within the proper bounds.
Parameters:
number: numeric value.
Examples:
>> isValidPercentage(2)
>> false
>> isValidPercentage(0.0)
>> true
>> isValidPercentage(1.)
>> true
>> isValidPercentage(50.0)
>> false
isValidTimestamp(date, unit):
Checks that a number that represents a date as milliseconds or seconds is correct.
Parameters:
date: int.
unit: String. 'millis' if date is in milliseconds, 'seconds' if date is in seconds.
Examples:
>> isValidTimestamp(2, "seconds")
>> false
>> isValidTimestamp(1234567890, "millis")
>> false
>> isValidTimestamp(1465496168, "seconds")
>> true
>> isValidTimestamp(1465496168000, unit="millis")
>> true
present(field_name, dictionary)
Checks if a field is present in an object.
Parameters:
field_name: string.
dictionary: object instance.
Examples:
dictionary = {'a': 1, 'b': 2}
>> present('c', dictionary)
>> false
>> present('a', dictionary)
>> true
isEmpty(iterable)
Checks if an iterable is empty
Parameters:
iterable: Iterable instance (an array or object).
Examples:
>> isEmpty([])
>> true
>> isEmpty([1,2,3])
>> false
>> isEmpty({'a': 1})
>> false
>> isEmpty({})
>> true
nonEmpty(iterable)
Checks if an iterable has at least one element.
Parameters:
iterable: Iterable instance (an array or object).
Examples:
>> nonEmpty([])
>> false
>> nonEmpty([1,2,3])
>> true
>> nonEmpty({'a': 1})
>> true
>> nonEmpty({})
>> false