node-ratify
A library of string, array and javascript object validators.
Server-side usage
var ratify = ; ratify; //=> true
The library can also be installed through npm
$ npm install -g node-ratify$ npm install --save node-ratify
Client-side usage
The library can also be installed through bower
$ bower install node-ratify
Clone the repo
$ git clone https://github.com/luthraG/node-validate.git
Validators
- contains(str) - check if the string contains another string(Case sensitive search).
- containsDeepKey(obj) - Safely reach into a 'nested' object and returns true if specified key is present.
- containsIgnoreCase(str) - check if the string contains another string(Case insensitive search).
- containsJapanese(str) - check if the string contains japanese characters.
- containsKey(obj, key) - Safely reach into a object and returns true if specified key is present.
- hasAnyMethod(obj) - check if the object has any method present inside it.
- isAlpha(str) - check if the string contains only alphabets, both upper and lower case.
- isAlphanumeric(str) - check if the string contains alphabets and numbers.
- isArray(value) - check if the value passed is Array.
- isAscii(value) - check if the string contains only ascii characters.
- isBase64(str) - check if a string is base64 encoded.
- isBlank(str) - check if a string is blank.
- isBoolean(str) - check if the value is boolean value i.e. true, false, 1, or 0.
- isDataURI(value) - check if value is a valid data URI or not.
- isDate(value) - check if the value is date object or not.
- isEmpty(str) - check if a string is empty. A string containing only whitespaces is blank string but not empty string.
- isError(value) - check if value is an error object or not
- isFalse(value) - check if the value passed is false.
- isFilePath(value) - check if the value is a valid file path i.e. window file path and unix file path.
- isFullWidth(str) - check if the specified string contains any full width characters or not.
- isFunction(value) - check if the value is function or not.
- isHalfWidth(str) - check if the specified string contains any half width characters or not.
- isHexaColor(str) - check if the specified string is valid hexadecimal color value or not.
- isHexadecimal(str) - check if the string is a hexadecimal number.
- isInRange(value, left, right) - check if string lies between two values i.e. left and right.
- isISO8601(str) - check if string is a valid ISO8601 value.
- isJSON(str) - check if a string is a valid JSON string.
- isLatitude(str) - check if the string is valid latitude value.
- isLongitude(str) - check if the string is valid longitude value.
- isLowerCase(str) - check if a string is in lowercase.
- isMACAddress(str) - check if the string value is a valid MAC address.
- isMap(value) - check if the value is map or not.
- isMD5(str) - check if the string value is MD5 hash.
- isMethodPresent(obj, methodName) - check if the object has the specified method present inside it.
- isMultiByte(str) - check if string contains one or more multibyte characters.
- isNegative(value) - check if input value if negative value or not.
- isNonNegative(value) - check if input value if a non negative value or not.
- isNonPositive(value) - check if input value if a non positive value or not.
- isNull(value) - check if the value is null or undefined or NaN.
- isNumber(value) - check if the value passed is Number object.
- isNumeric(str) - check if a string is number(Integer/Floating point et al.).
- isObject(value) - check if the value passed is Object.
- isPalindrome(str) - check if input string is a palindrome or not.
- isPort(str) - check if input string is a valid port number or not.
- isPositive(str) - check if input value if positive value or not.
- isPrintableASCII(str) - check if all the characters of input string are printable ASCII characters or not.
- isRegExp(value) - check if the value is RegExp or not.
- isRGBColor(str) - check if string is a valid rgb value.
- isSemver(str) - check if specified string is valid semver as per semantics versioning rule.
- isSet(value) - check if the specified object is set or not
- isStrictBoolean(value) - check if the value is boolean object or not.
- isStrictObject(value) - check if the value is Object or not.
- isString(value) - check if the value passed is string.
- isSystemError(value) - check is the value is system error object or not.
- isTitleCase(value) - check is the string is title case i.e. first letter of each word in the string is capital case.
- isTrue(value) - check if the value passed is true.
- isUndefined(value) - check if value is a undefined or not.
- isUnixFilePath(value) - check if value is valid unix file path.
- isUpperCase(str) - check if a string is in uppercase.
- isURIEncoded(str) - check if multi word string is URI encoded.
- isUUID(str) - check if the specified string is uuid(of specified version).
- isValidEmail(str) - check if the string is a valid email address.
- isVariableWidth(str) - check if the specified string contains both full width and half width characters.
- isWeakMap(value) - check if the value is weakmap or not.
- isWeakSet(value) - check if value is a weak set or not.
- isWinFilePath(value) - check if value is valid window file path.
- isWhiteSpace(str) - check if a string contains only whitespaces.
- objectEquals(obj1, obj2) - check if two objects(String, Object, Number, Date, function, RegExp, Array) are equal or not.
- startsWith(str, starts, position, strict) - check if a string/number/array starts with another string/number/array.
It accepts two optional parameters - position and strict. Client can specify the optional position value to tell where it will check for start.
The optional parameters strict, which is set to false by default, indicates whether number values should be converted to string or not while comparing - endsWith(str, ends, position, strict) - check if a string/number/array ends with another string/number/array.
It accepts two optional parameters - position and strict. Client can specify the optional position value to tell where it will check for end.
The optional parameters strict, which is set to false by default, indicates whether number values should be converted to string or not while comparing
Examples
var ratify = ; // Check if the value is string or notratify; // returns true // Check if the value is Number or notratify; // returns trueratify; // returns false // Check if the value is Array or notratify; // returns falseratify; // returns true // Check if the value is Object or not // (Value could be String object, Number Object, Array Object, Date object etc.)ratify; // returns trueratify; // returns false // Check if the value is Object or not ratify; // returns trueratify; // returns false // Check if the value is RegExp or notratify; // returns falseratify; // returns true // Check if the value is Boolean object or notratify // returns true // Check if the value is Boolean or notratify // returns falseratify // returns trueratify // returns true // Check if the value is Function or not {return true;} {};var arr = 'Item1' 'Item2' 'Item3';var obj = {}; ratify // returns trueratify // returns trueratify // returns falseratify // returns falseratify // returns false // Check if the string is a lowercase stringratify // returns trueratify // returns false // Check if the string is a uppercase stringratify // returns trueratify // returns false // Check is the string is title case // i.e. first letter of each word in the string is capital caseratify // returns falseratify // returns true // Check if the string is a numberratify // returns trueratify // returns true // Check if the string/object is valid JSONvar a = a : 'a' b : 'b' c : 'c' d : 'd';var b = a : 'a' b : 'b' c : x : 'x' y : 'y' z : key1 : 'value1' key2 : 'value2';var c = a : 'a' b : 'b' c : x : 'x' y : 'y' z : key1 : 'value1' key2 : 'value2' ; // Extra comma ratify; // returns trueratify; // returns trueratify; // returns trueratify; // returns trueratify; // returns false // Check if the string is base64 encodedratify // returns falseratify // returns true // Check if the string contains only alphabets, both upper and lower caseratify // returns falseratify // returns trueratify // returns false // Check if the string contains alphabets and numbersratify // returns trueratify // returns falseratify // returns true // Check if the string is a hexadecimal numberratify // returns trueratify // returns falseratify // returns true // Check if the string contains only ascii charactersratify // returns falseratify // returns false // Check if the value is nullratify // returns falseratify // returns true // Check if the string is a valid email addressratify; // returns trueratify; // returns false // Check if the specified string is uuid(of specified version)ratify; // returns trueratify; // returns true // Check if String is URI encodedratify; // returns true // Check if the string doesn't contain anythingratify; // returns trueratify; // returns trueratify; // returns false // Check if the string contains only whitespacesratify; // returns trueratify; // returns false // Check if the string is blank : i.e. // either it is empty or only contains whitespacesratify; // returns trueratify; // returns true // Check if the string contains another string(Case sensitive search)var baseStr = 'This is me and that is you';var searchStr = 'this'; ratify; // returns falseratify; // returns true // Check if the string contains another string(Case insensitive search)var baseStr = 'This is this and that is that';var searchStr = 'this'; ratify; // returns trueratify; // returns false // Safely search into a object and returns true if specified key is present. // Returns false if the intended target is not present. var o = qwe : asd : zxc : 123 ;ratify; // returns trueratify; // returns false, as it is not present at top level // Safely reach into a nested object and returns true if specified key is present. // Returns false if any of the intermediate subobjects or the intended target are not present. var o = qwe : asd : zxc : 123 ;ratify; // returns trueratify; // returns trueratify; // returns true // Check if input string is a palindrome or not var str1 = 'abcdefghijklmnopqrstuvwxyzzyxwvutsrqponmlkjihgfedcba';var str2 = 'eye';var str3 = 'Hello'; ratify; // returns trueratify; // returns trueratify; // returns false // Check if two objects(String, Object, Number, Date, function, RegExp, Array)// are equal var o = qwe : asd : zxc : 123 ;var m = qwe : asd : zxc : 123 ; ratify; // returns trueratify; // returns trueratify; // returns false // Check if the object has any method present inside it var o = qwe : asd : zxc : 123 ;var m = qwe : 123 {console} ; ratify; // returns falseratify; // returns true // Check if the object has the specified method present inside it var o = qwe : asd : zxc : 123 ;var m = qwe : 123 {console} ; ratify; // returns falseratify; // returns falseratify; // returns falseratify; // returns true //// Check if a string/number/array starts with another string/number/array at specified position.// Returns boolean true or false// Setting strict to true will not convert number to string while comparing//ratify; // returns trueratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns trueratify; // returns trueratify; // returns trueratify; // returns false as strict mode = true //// Check if a string/number/array ends with another string/number/array at specified position.// Returns boolean true or false// Setting strict to true will not convert number to string while comparing//ratify; // returns trueratify; // returns falseratify; // returns trueratify; // returns trueratify; // returns falseratify; // returns trueratify; // returns falseratify; // returns trueratify; // returns trueratify; // returns false as strict mode = true //// Check if the specified string is md5 hash or not//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns trueratify; // returns false //// Check if the specified string is valid MAC address or not//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns trueratify; // returns false //// Check if the specified string is valid hexadecimal color value or not//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns trueratify; // returns trueratify; // returns trueratify; // returns falseratify; // returns false //// Check if the specified object is set or not//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns trueratify; // returns falseratify; // returns true //// Check if the specified object is weakset or not//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns falseratify; // returns true //// Check if passed value is undefined or not//ratify; // returns trueratify; // returns falseratify; // returns trueratify; // returns false //// Check if passed object is error object or not//ratify; // returns trueratify; // returns trueratify; // returns trueratify; // returns true //// Check if passed object is system error object or not//var error = ;errorcode = 'ECONNRESET'; ratify; // returns falseratify; // returns trueratify; // returns falseratify; // returns false //// Check if passed object is date object or not//ratify; // returns trueratify; // returns false //// Check if passed value is map or not//ratifyisMap''; // returns falseratifyisMapnull; // returns falseratifyisMap; // returns trueratifyisMap; // returns falseratifyisMap'key1' 'value1' 'key2' 'value2'; // returns true //// Check if passed value is weak map or not//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns false //// Check if passed string contains half width characters//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns true //// Check if passed string contains full width characters//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns true //// Check if passed string contains japanese characters//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns true //// Check if passed all the characters of passed string are printable ASCII characters or not//ratify; // returns falseratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns false //// Check if string is both half width and full width characters//ratify; // returns trueratify; // returns falseratify; // returns false //// Check if string is a valid port number//ratify; // returns falseratify; // returns trueratify; // returns false //// Check if string is a value that follows semantics versioning rule//ratify; // returns trueratify; // returns trueratify; // returns false //// Check if the string is a valid latitude value//ratify; // returns trueratify; // returns trueratify; // returns false //// Check if the string is a valid longitude value//ratify; // returns trueratify; // returns trueratify; // returns false //// Check if string is a valid windows path//ratify; // returns trueratify; // returns falseratify; // returns false //// Check if string is a valid unix path//ratify; // returns falseratify; // returns trueratify; // returns false //// Check is string is a valid file path i.e. wither unix file path or windows file path//ratify; // returns trueratify; // returns trueratify; // returns false //// Check if string contains one or more multibyte characters//ratify; // returns trueratify; // returns falseratify; // returns true //// Check if string lies between two values i.e. left and right//ratify; // returns trueratify; // returns falseratify; // returns false //// Check if the value being passed is positive value//ratify; // returns trueratify; // returns trueratify; // returns falseratify; // returns false //// Check if the value being passed is negative value//ratify; // returns falseratify; // returns falseratify; // returns trueratify; // returns false //// Check if value being passed is non positive value.//ratify; // returns falseratify; // returns trueratify; // returns true //// Check if value being passed is non negative value.//ratify; // returns trueratify; // returns falseratify; // returns true //// Check if value is a valid data URI//ratify; // returns trueratify; // returns trueratify; // returns false //// Check if value is a valid iso8601 string//ratify; // returns trueratify; // returns trueratify; // returns false //// Check if value is valid RGB color value//ratify; // returns trueratify; // returns trueratify; // returns false
License(MIT)
Copyright (c) 2017 Gaurav Luthra(luthra.zenith@gmail.com)
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.