@kingshott/iodine
Advanced tools
Comparing version 3.0.2 to 3.0.3
{ | ||
"name": "@kingshott/iodine", | ||
"version": "3.0.2", | ||
"version": "3.0.3", | ||
"description": "A micro client-side validation library", | ||
@@ -9,3 +9,3 @@ "repository": { | ||
}, | ||
"keywords": [ | ||
"keywords": [ | ||
"iodine", | ||
@@ -18,3 +18,3 @@ "validation", | ||
"build": "microbundle", | ||
"watch": "microbundle watch", | ||
"watch": "microbundle watch", | ||
"test": "jest" | ||
@@ -21,0 +21,0 @@ }, |
@@ -139,31 +139,31 @@ <!-- Screenshot --> | ||
| Rule | Description | | ||
| Rule | Description | | ||
| ----------------------------- | ------------------------------------------------------------------------------- | | ||
| isAfter(date/integer) | Verify that the item is a `Date` after a given `Date` or timestamp | ||
| isAfterOrEqual(date/integer) | Verify that the item is a `Date` after or equal to a given `Date` or timestamp | ||
| isArray | Verify that the item is an `array` | ||
| isBefore(date/integer) | Verify that the item is a `Date` before a given `Date` or timestamp | ||
| isAfter(date/integer) | Verify that the item is a `Date` after a given `Date` or timestamp | ||
| isAfterOrEqual(date/integer) | Verify that the item is a `Date` after or equal to a given `Date` or timestamp | ||
| isArray | Verify that the item is an `array` | ||
| isBefore(date/integer) | Verify that the item is a `Date` before a given `Date` or timestamp | ||
| isBeforeOrEqual(date/integer) | Verify that the item is a `Date` before or equal to a given `Date` or timestamp | ||
| isBoolean | Verify that the item is either `true` or `false` | ||
| isDate | Verify that the item is a `Date` object | ||
| isDifferent(value) | Verify that the item is different to the supplied value (uses loose compare) | ||
| isEndingWith(value) | Verify that the item ends with the given value | ||
| isEmail | Verify that the item is a valid email address | ||
| isFalsy | Verify that the item is either `false`, `'false'`, `0` or `'0'` | ||
| isIn(array) | Verify that the item is within the given `array` | ||
| isInteger | Verify that the item is an `integer` | ||
| isJson | Verify that the item is a parsable JSON object `string` | ||
| isMaximum(limit) | Verify that the item does not exceed the given limit (number or char length) | ||
| isMinimum(limit) | Verify that the item is not under the given limit (number or char length) | ||
| isNotIn(array) | Verify that the item is not within the given `array` | ||
| isNumeric | Verify that the item is `number` or a numeric `string` | ||
| isOptional | Allow for optional values (only for use with multiple checks) | ||
| isRegexMatch(exp) | Verify that the item satisifies the given regular expression | ||
| isRequired | Verify that the item is not `null`, `undefined` or an empty `string` | ||
| isSame(value) | Verify that the item is the same as the supplied value (uses loose compare) | ||
| isStartingWith(value) | Verify that the item starts with the given value | ||
| isString | Verify that the item is a `string` | ||
| isTruthy | Verify that the item is either `true`, `'true'`, `1` or `'1'` | ||
| isUrl | Verify that the item is a valid URL | ||
| isUuid | Verify that the item is a `UUID` | ||
| isBoolean | Verify that the item is either `true` or `false` | ||
| isDate | Verify that the item is a `Date` object | ||
| isDifferent(value) | Verify that the item is different to the supplied value (uses loose compare) | ||
| isEndingWith(value) | Verify that the item ends with the given value | ||
| isEmail | Verify that the item is a valid email address | ||
| isFalsy | Verify that the item is either `false`, `'false'`, `0` or `'0'` | ||
| isIn(array) | Verify that the item is within the given `array` | ||
| isInteger | Verify that the item is an `integer` | ||
| isJson | Verify that the item is a parsable JSON object `string` | ||
| isMaximum(limit) | Verify that the item does not exceed the given limit (number or char length) | ||
| isMinimum(limit) | Verify that the item is not under the given limit (number or char length) | ||
| isNotIn(array) | Verify that the item is not within the given `array` | ||
| isNumeric | Verify that the item is `number` or a numeric `string` | ||
| isOptional | Allow for optional values (only for use with multiple checks) | ||
| isRegexMatch(exp) | Verify that the item satisifies the given regular expression | ||
| isRequired | Verify that the item is not `null`, `undefined` or an empty `string` | ||
| isSame(value) | Verify that the item is the same as the supplied value (uses loose compare) | ||
| isStartingWith(value) | Verify that the item starts with the given value | ||
| isString | Verify that the item is a `string` | ||
| isTruthy | Verify that the item is either `true`, `'true'`, `1` or `'1'` | ||
| isUrl | Verify that the item is a valid URL | ||
| isUuid | Verify that the item is a `UUID` | ||
@@ -170,0 +170,0 @@ Examine the tests for examples of how to use each rule. |
@@ -13,460 +13,452 @@ /* | ||
/** | ||
* Constructor. | ||
* | ||
**/ | ||
constructor() | ||
{ | ||
this.messages = this._defaultMessages(); | ||
} | ||
/** | ||
* Constructor. | ||
* | ||
**/ | ||
constructor() | ||
{ | ||
this.messages = this._defaultMessages(); | ||
} | ||
/** | ||
* @internal. | ||
* | ||
**/ | ||
_dateCompare(first, second, type, equals = false) | ||
{ | ||
if (! this.isDate(first)) return false; | ||
/** | ||
* @internal. | ||
* | ||
**/ | ||
_dateCompare(first, second, type, equals = false) | ||
{ | ||
if (! this.isDate(first)) return false; | ||
if (! this.isDate(second) && ! this.isInteger(second)) return false; | ||
if (! this.isDate(second) && ! this.isInteger(second)) return false; | ||
second = typeof second === 'number' ? second : second.getTime(); | ||
second = typeof second === 'number' ? second : second.getTime(); | ||
if (type === 'less' && equals) return first.getTime() <= second; | ||
if (type === 'less' && ! equals) return first.getTime() < second; | ||
if (type === 'more' && equals) return first.getTime() >= second; | ||
if (type === 'more' && ! equals) return first.getTime() > second; | ||
} | ||
if (type === 'less' && equals) return first.getTime() <= second; | ||
if (type === 'less' && ! equals) return first.getTime() < second; | ||
if (type === 'more' && equals) return first.getTime() >= second; | ||
if (type === 'more' && ! equals) return first.getTime() > second; | ||
} | ||
/** | ||
* @internal. | ||
* | ||
**/ | ||
_defaultMessages() | ||
{ | ||
return { | ||
after : `The date must be after: '[PARAM]'`, | ||
afterOrEqual : `The date must be after or equal to: '[PARAM]'`, | ||
array : `Value must be an array`, | ||
before : `The date must be before: '[PARAM]'`, | ||
beforeOrEqual : `The date must be before or equal to: '[PARAM]'`, | ||
boolean : `Value must be true or false`, | ||
date : `Value must be a date`, | ||
different : `Value must be different to '[PARAM]'`, | ||
endingWith : `Value must end with '[PARAM]'`, | ||
email : `Value must be a valid email address`, | ||
falsy : `Value must be a falsy value (false, 'false', 0 or '0')`, | ||
in : `Value must be one of the following options: [PARAM]`, | ||
integer : `Value must be an integer`, | ||
json : `Value must be a parsable JSON object string`, | ||
maximum : `Value must not be greater than '[PARAM]' in size or character length`, | ||
minimum : `Value must not be less than '[PARAM]' in size or character length`, | ||
notIn : `Value must not be one of the following options: [PARAM]`, | ||
numeric : `Value must be numeric`, | ||
optional : `Value is optional`, | ||
regexMatch : `Value must satisify the regular expression: [PARAM]`, | ||
required : `Value must be present`, | ||
same : `Value must be '[PARAM]'`, | ||
startingWith : `Value must start with '[PARAM]'`, | ||
string : `Value must be a string`, | ||
truthy : `Value must be a truthy value (true, 'true', 1 or '1')`, | ||
url : `Value must be a valid url`, | ||
uuid : `Value must be a valid UUID`, | ||
}; | ||
} | ||
/** | ||
* @internal. | ||
* | ||
**/ | ||
_defaultMessages() | ||
{ | ||
return { | ||
after : `The date must be after: '[PARAM]'`, | ||
afterOrEqual : `The date must be after or equal to: '[PARAM]'`, | ||
array : `Value must be an array`, | ||
before : `The date must be before: '[PARAM]'`, | ||
beforeOrEqual : `The date must be before or equal to: '[PARAM]'`, | ||
boolean : `Value must be true or false`, | ||
date : `Value must be a date`, | ||
different : `Value must be different to '[PARAM]'`, | ||
endingWith : `Value must end with '[PARAM]'`, | ||
email : `Value must be a valid email address`, | ||
falsy : `Value must be a falsy value (false, 'false', 0 or '0')`, | ||
in : `Value must be one of the following options: [PARAM]`, | ||
integer : `Value must be an integer`, | ||
json : `Value must be a parsable JSON object string`, | ||
maximum : `Value must not be greater than '[PARAM]' in size or character length`, | ||
minimum : `Value must not be less than '[PARAM]' in size or character length`, | ||
notIn : `Value must not be one of the following options: [PARAM]`, | ||
numeric : `Value must be numeric`, | ||
optional : `Value is optional`, | ||
regexMatch : `Value must satisify the regular expression: [PARAM]`, | ||
required : `Value must be present`, | ||
same : `Value must be '[PARAM]'`, | ||
startingWith : `Value must start with '[PARAM]'`, | ||
string : `Value must be a string`, | ||
truthy : `Value must be a truthy value (true, 'true', 1 or '1')`, | ||
url : `Value must be a valid url`, | ||
uuid : `Value must be a valid UUID`, | ||
}; | ||
} | ||
/** | ||
* Attach a custom validation rule to the library. | ||
* | ||
**/ | ||
addRule(name, closure) | ||
{ | ||
Iodine.prototype[`is${name[0].toUpperCase()}${name.slice(1)}`] = closure; | ||
} | ||
/** | ||
* Attach a custom validation rule to the library. | ||
* | ||
**/ | ||
addRule(name, closure) | ||
{ | ||
Iodine.prototype[`is${name[0].toUpperCase()}${name.slice(1)}`] = closure; | ||
} | ||
/** | ||
* Retrieve an error message for the given rule. | ||
* | ||
**/ | ||
getErrorMessage(rule, arg = undefined) | ||
{ | ||
let key = rule.split(':')[0]; | ||
let param = arg || rule.split(':')[1]; | ||
/** | ||
* Retrieve an error message for the given rule. | ||
* | ||
**/ | ||
getErrorMessage(rule, arg = undefined) | ||
{ | ||
let key = rule.split(':')[0]; | ||
let param = arg || rule.split(':')[1]; | ||
if (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) { | ||
param = new Date(parseInt(param)).toLocaleTimeString(undefined, { | ||
year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric' | ||
}); | ||
} | ||
if (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) { | ||
param = new Date(parseInt(param)).toLocaleTimeString(undefined, { | ||
year: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric' | ||
}); | ||
} | ||
return [null, undefined].includes(param) | ||
? this.messages[key] | ||
: this.messages[key].replace("[PARAM]", param); | ||
} | ||
return [null, undefined].includes(param) | ||
? this.messages[key] | ||
: this.messages[key].replace('[PARAM]', param); | ||
} | ||
/** | ||
* Determine if the given date is after another given date. | ||
* | ||
**/ | ||
isAfter(value, after) | ||
{ | ||
return this._dateCompare(value, after, 'more', false); | ||
} | ||
/** | ||
* Determine if the given date is after another given date. | ||
* | ||
**/ | ||
isAfter(value, after) | ||
{ | ||
return this._dateCompare(value, after, 'more', false); | ||
} | ||
/** | ||
* Determine if the given date is after or equal to another given date. | ||
* | ||
**/ | ||
isAfterOrEqual(value, after) | ||
{ | ||
return this._dateCompare(value, after, 'more', true); | ||
} | ||
/** | ||
* Determine if the given date is after or equal to another given date. | ||
* | ||
**/ | ||
isAfterOrEqual(value, after) | ||
{ | ||
return this._dateCompare(value, after, 'more', true); | ||
} | ||
/** | ||
* Determine if the given value is an array. | ||
* | ||
**/ | ||
isArray(value) | ||
{ | ||
return Array.isArray(value); | ||
} | ||
/** | ||
* Determine if the given value is an array. | ||
* | ||
**/ | ||
isArray(value) | ||
{ | ||
return Array.isArray(value); | ||
} | ||
/** | ||
* Determine if the given date is before another given date. | ||
* | ||
**/ | ||
isBefore(value, before) | ||
{ | ||
return this._dateCompare(value, before, 'less', false); | ||
} | ||
/** | ||
* Determine if the given date is before another given date. | ||
* | ||
**/ | ||
isBefore(value, before) | ||
{ | ||
return this._dateCompare(value, before, 'less', false); | ||
} | ||
/** | ||
* Determine if the given date is before or equal to another given date. | ||
* | ||
**/ | ||
isBeforeOrEqual(value, before) | ||
{ | ||
return this._dateCompare(value, before, 'less', true); | ||
} | ||
/** | ||
* Determine if the given date is before or equal to another given date. | ||
* | ||
**/ | ||
isBeforeOrEqual(value, before) | ||
{ | ||
return this._dateCompare(value, before, 'less', true); | ||
} | ||
/** | ||
* Determine if the given value is a boolean. | ||
* | ||
**/ | ||
isBoolean(value) | ||
{ | ||
return [true, false].includes(value); | ||
} | ||
/** | ||
* Determine if the given value is a boolean. | ||
* | ||
**/ | ||
isBoolean(value) | ||
{ | ||
return [true, false].includes(value); | ||
} | ||
/** | ||
* Determine if the given value is a date object. | ||
* | ||
**/ | ||
isDate(value) | ||
{ | ||
return value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value); | ||
} | ||
/** | ||
* Determine if the given value is a date object. | ||
* | ||
**/ | ||
isDate(value) | ||
{ | ||
return value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value); | ||
} | ||
/** | ||
* Determine if the given value is different to another given value. | ||
* | ||
**/ | ||
isDifferent(value, different) | ||
{ | ||
return value != different; | ||
} | ||
/** | ||
* Determine if the given value is different to another given value. | ||
* | ||
**/ | ||
isDifferent(value, different) | ||
{ | ||
return value != different; | ||
} | ||
/** | ||
* Determine if the given value ends with another given value. | ||
* | ||
**/ | ||
isEndingWith(value, sub) | ||
{ | ||
return this.isString(value) && value.endsWith(sub); | ||
} | ||
/** | ||
* Determine if the given value ends with another given value. | ||
* | ||
**/ | ||
isEndingWith(value, sub) | ||
{ | ||
return this.isString(value) && value.endsWith(sub); | ||
} | ||
/** | ||
* Determine if the given value is a valid email address. | ||
* | ||
**/ | ||
isEmail(value) | ||
{ | ||
return new RegExp('^\\S+@\\S+[\\.][0-9a-z]+$').test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine if the given value is a valid email address. | ||
* | ||
**/ | ||
isEmail(value) | ||
{ | ||
return new RegExp('^\\S+@\\S+[\\.][0-9a-z]+$').test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine if the given value is falsy. | ||
* | ||
**/ | ||
isFalsy(value) | ||
{ | ||
return [0, '0', false, 'false'].includes(value); | ||
} | ||
/** | ||
* Determine if the given value is falsy. | ||
* | ||
**/ | ||
isFalsy(value) | ||
{ | ||
return [0, '0', false, 'false'].includes(value); | ||
} | ||
/** | ||
* Determine if the given value is within the given array of options. | ||
* | ||
**/ | ||
isIn(value, options) | ||
{ | ||
options = typeof options === 'string' | ||
? options.split(",") | ||
: options; | ||
/** | ||
* Determine if the given value is within the given array of options. | ||
* | ||
**/ | ||
isIn(value, options) | ||
{ | ||
options = typeof options === 'string' | ||
? options.split(',') | ||
: options; | ||
return options.includes(value); | ||
} | ||
return options.includes(value); | ||
} | ||
/** | ||
* Determine if the given value is an integer. | ||
* | ||
**/ | ||
isInteger(value) | ||
{ | ||
return Number.isInteger(value) && parseInt(value).toString() === value.toString(); | ||
} | ||
/** | ||
* Determine if the given value is an integer. | ||
* | ||
**/ | ||
isInteger(value) | ||
{ | ||
return Number.isInteger(value) && parseInt(value).toString() === value.toString(); | ||
} | ||
/** | ||
* Determine if the given value is a JSON string. | ||
* | ||
**/ | ||
isJson(value) | ||
{ | ||
try { | ||
return typeof JSON.parse(value) === 'object'; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
/** | ||
* Determine if the given value is a JSON string. | ||
* | ||
**/ | ||
isJson(value) | ||
{ | ||
try { | ||
return typeof JSON.parse(value) === 'object'; | ||
} catch (e) { | ||
return false; | ||
} | ||
} | ||
/** | ||
* Determine if the given value meets the given maximum limit. | ||
* | ||
**/ | ||
isMaximum(value, limit) | ||
{ | ||
value = typeof value === 'string' ? value.length : value; | ||
/** | ||
* Determine if the given value meets the given maximum limit. | ||
* | ||
**/ | ||
isMaximum(value, limit) | ||
{ | ||
value = typeof value === 'string' ? value.length : value; | ||
return parseFloat(value) <= limit; | ||
} | ||
return parseFloat(value) <= limit; | ||
} | ||
/** | ||
* Determine if the given value meets the given minimum limit. | ||
* | ||
**/ | ||
isMinimum(value, limit) | ||
{ | ||
value = typeof value === 'string' ? value.length : value; | ||
/** | ||
* Determine if the given value meets the given minimum limit. | ||
* | ||
**/ | ||
isMinimum(value, limit) | ||
{ | ||
value = typeof value === 'string' ? value.length : value; | ||
return parseFloat(value) >= limit; | ||
} | ||
return parseFloat(value) >= limit; | ||
} | ||
/** | ||
* Determine if the given value is not within the given array of options. | ||
* | ||
**/ | ||
isNotIn(value, options) | ||
{ | ||
return ! this.isIn(value, options); | ||
} | ||
/** | ||
* Determine if the given value is not within the given array of options. | ||
* | ||
**/ | ||
isNotIn(value, options) | ||
{ | ||
return ! this.isIn(value, options); | ||
} | ||
/** | ||
* Determine if the given value is numeric (an integer or a float). | ||
* | ||
**/ | ||
isNumeric(value) | ||
{ | ||
return ! isNaN(parseFloat(value)) && isFinite(value); | ||
} | ||
/** | ||
* Determine if the given value is numeric (an integer or a float). | ||
* | ||
**/ | ||
isNumeric(value) | ||
{ | ||
return ! isNaN(parseFloat(value)) && isFinite(value); | ||
} | ||
/** | ||
* Determine if the given value is optional. | ||
* | ||
**/ | ||
isOptional(value) | ||
{ | ||
return [null, undefined, ''].includes(value); | ||
} | ||
/** | ||
* Determine if the given value is optional. | ||
* | ||
**/ | ||
isOptional(value) | ||
{ | ||
return [null, undefined, ''].includes(value); | ||
} | ||
/** | ||
* Determine if the given value satisifies the given regular expression. | ||
* | ||
**/ | ||
isRegexMatch(value, expression) | ||
{ | ||
return new RegExp(expression).test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine if the given value satisifies the given regular expression. | ||
* | ||
**/ | ||
isRegexMatch(value, expression) | ||
{ | ||
return new RegExp(expression).test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine if the given value is present. | ||
* | ||
**/ | ||
isRequired(value) | ||
{ | ||
return ! this.isOptional(value); | ||
} | ||
/** | ||
* Determine if the given value is present. | ||
* | ||
**/ | ||
isRequired(value) | ||
{ | ||
return ! this.isOptional(value); | ||
} | ||
/** | ||
* Determine if the given value is the same as another given value. | ||
* | ||
**/ | ||
isSame(value, same) | ||
{ | ||
return value == same; | ||
} | ||
/** | ||
* Determine if the given value is the same as another given value. | ||
* | ||
**/ | ||
isSame(value, same) | ||
{ | ||
return value == same; | ||
} | ||
/** | ||
* Determine if the given value starts with another given value. | ||
* | ||
**/ | ||
isStartingWith(value, sub) | ||
{ | ||
return this.isString(value) && value.startsWith(sub); | ||
} | ||
/** | ||
* Determine if the given value starts with another given value. | ||
* | ||
**/ | ||
isStartingWith(value, sub) | ||
{ | ||
return this.isString(value) && value.startsWith(sub); | ||
} | ||
/** | ||
* Determine if the given value is a string. | ||
* | ||
**/ | ||
isString(value) | ||
{ | ||
return typeof value === 'string'; | ||
} | ||
/** | ||
* Determine if the given value is a string. | ||
* | ||
**/ | ||
isString(value) | ||
{ | ||
return typeof value === 'string'; | ||
} | ||
/** | ||
* Determine if the given value is truthy. | ||
* | ||
**/ | ||
isTruthy(value) | ||
{ | ||
return [1, '1', true, 'true'].includes(value); | ||
} | ||
/** | ||
* Determine if the given value is truthy. | ||
* | ||
**/ | ||
isTruthy(value) | ||
{ | ||
return [1, '1', true, 'true'].includes(value); | ||
} | ||
/** | ||
* Determine if the given value is a valid URL. | ||
* | ||
**/ | ||
isUrl(value) | ||
{ | ||
return new RegExp('^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$') | ||
.test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine if the given value is a valid URL. | ||
* | ||
**/ | ||
isUrl(value) | ||
{ | ||
return new RegExp('^(https?:\\/\\/)?((([a-z\\d]([a-z\\d-]*[a-z\\d])*)\\.)+[a-z]{2,}|((\\d{1,3}\\.){3}\\d{1,3}))(\\:\\d+)?(\\/[-a-z\\d%_.~+]*)*(\\?[;&a-z\\d%_.~+=-]*)?(\\#[-a-z\\d_]*)?$') | ||
.test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine if the given value is a valid UUID. | ||
* | ||
**/ | ||
isUuid(value) | ||
{ | ||
return new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') | ||
.test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine if the given value is a valid UUID. | ||
* | ||
**/ | ||
isUuid(value) | ||
{ | ||
return new RegExp('^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$') | ||
.test(String(value).toLowerCase()); | ||
} | ||
/** | ||
* Determine whether the given value meets the given rules. | ||
* | ||
**/ | ||
is(value, rules = []) | ||
{ | ||
// Check if no rules were specified | ||
if (! rules.length) return true; | ||
/** | ||
* Determine whether the given value meets the given rules. | ||
* | ||
**/ | ||
is(value, rules = []) | ||
{ | ||
if (! rules.length) return true; | ||
// Check for an optional value | ||
if (rules[0] === 'optional' && this.isOptional(value)) return true; | ||
if (rules[0] === 'optional' && this.isOptional(value)) return true; | ||
// Iterate through the rules | ||
for (let index in rules) { | ||
for (let index in rules) { | ||
// Ignore optional rules | ||
if (rules[index] === 'optional') continue; | ||
if (rules[index] === 'optional') continue; | ||
// Determine the method to use | ||
let rule = rules[index].split(':')[0][0].toUpperCase() | ||
+ rules[index].split(':')[0].slice(1); | ||
let rule = rules[index].split(':')[0][0].toUpperCase() | ||
+ rules[index].split(':')[0].slice(1); | ||
// Validate the value against the method | ||
let result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]); | ||
let result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]); | ||
// Check if the value failed validation | ||
if (! result) return rules[index]; | ||
if (! result) return rules[index]; | ||
} | ||
} | ||
// Otherwise, the value is valid | ||
return true; | ||
} | ||
return true; | ||
} | ||
/** | ||
* Replace the default error messages with a new set. | ||
* | ||
**/ | ||
setErrorMessages(messages) | ||
{ | ||
this.messages = messages; | ||
} | ||
/** | ||
* Replace the default error messages with a new set. | ||
* | ||
**/ | ||
setErrorMessages(messages) | ||
{ | ||
this.messages = messages; | ||
} | ||
@@ -473,0 +465,0 @@ } |
@@ -1,2 +0,1 @@ | ||
// Import Iodine | ||
import '../dist/iodine.min.js'; | ||
@@ -11,3 +10,3 @@ | ||
test('it validates after date values', () => { | ||
let year = new Date().getFullYear(); | ||
let year = new Date().getFullYear(); | ||
expect(Iodine.isAfter(new Date(year + 1, 12, 18), new Date(year, 12, 18))).toBe(true); | ||
@@ -29,3 +28,3 @@ expect(Iodine.isAfter(new Date(year + 1, 12, 17), Date.now())).toBe(true); | ||
test('it validates after or equal date values', () => { | ||
let year = new Date().getFullYear(); | ||
let year = new Date().getFullYear(); | ||
expect(Iodine.isAfterOrEqual(new Date(year + 1, 12, 18), new Date(year, 12, 18))).toBe(true); | ||
@@ -48,3 +47,3 @@ expect(Iodine.isAfterOrEqual(new Date(year + 1, 12, 17), Date.now())).toBe(true); | ||
test('it validates before date values', () => { | ||
let year = new Date().getFullYear(); | ||
let year = new Date().getFullYear(); | ||
expect(Iodine.isBefore(new Date(year - 1, 12, 18), new Date(year, 12, 18))).toBe(true); | ||
@@ -66,3 +65,3 @@ expect(Iodine.isBefore(new Date(year - 1, 12, 17), Date.now())).toBe(true); | ||
test('it validates before or equal date values', () => { | ||
let year = new Date().getFullYear(); | ||
let year = new Date().getFullYear(); | ||
expect(Iodine.isBeforeOrEqual(new Date(year - 1, 12, 18), new Date(year, 12, 18))).toBe(true); | ||
@@ -405,3 +404,3 @@ expect(Iodine.isBeforeOrEqual(new Date(year - 1, 12, 17), Date.now())).toBe(true); | ||
test('it retrieves formatted error messages for rules', () => { | ||
let time = Date.UTC(2020, 4, 2, 3, 17, 0); | ||
let time = Date.UTC(2020, 4, 2, 3, 17, 0); | ||
expect(Iodine.getErrorMessage('array')).toBe('Value must be an array'); | ||
@@ -422,6 +421,6 @@ expect(Iodine.getErrorMessage('endingWith')).toBe(`Value must end with '[PARAM]'`); | ||
test('it can replace the default error messages', () => { | ||
Iodine.setErrorMessages({ array : 'Hello world', endingWith : 'Hello, [PARAM]' }); | ||
Iodine.setErrorMessages({ array : 'Hello world', endingWith : 'Hello, [PARAM]' }); | ||
expect(Iodine.getErrorMessage('array')).toBe('Hello world'); | ||
expect(Iodine.getErrorMessage('endingWith:John')).toBe('Hello, John'); | ||
expect(Iodine.getErrorMessage('endingWith', "John")).toBe('Hello, John'); | ||
expect(Iodine.getErrorMessage('endingWith', 'John')).toBe('Hello, John'); | ||
}); | ||
@@ -436,10 +435,10 @@ | ||
test('it can add simple custom rules', () => { | ||
Iodine.addRule('lowerCase', (value) => value === value.toLowerCase()); | ||
Iodine.setErrorMessages({ lowerCase : 'Value must be in lower case' }); | ||
Iodine.addRule('lowerCase', (value) => value === value.toLowerCase()); | ||
Iodine.setErrorMessages({ lowerCase : 'Value must be in lower case' }); | ||
expect(Iodine.isLowerCase('hello')).toBe(true); | ||
expect(Iodine.isLowerCase('Hello')).toBe(false); | ||
expect(Iodine.isLowerCase('HELLO')).toBe(false); | ||
expect(Iodine.is('hello', ['required', 'lowerCase'])).toBe(true); | ||
expect(Iodine.is('Hello', ['required', 'lowerCase'])).toBe('lowerCase'); | ||
expect(Iodine.is('HELLO', ['required', 'lowerCase'])).toBe('lowerCase'); | ||
expect(Iodine.is('hello', ['required', 'lowerCase'])).toBe(true); | ||
expect(Iodine.is('Hello', ['required', 'lowerCase'])).toBe('lowerCase'); | ||
expect(Iodine.is('HELLO', ['required', 'lowerCase'])).toBe('lowerCase'); | ||
expect(Iodine.getErrorMessage('lowerCase')).toBe('Value must be in lower case'); | ||
@@ -455,12 +454,12 @@ }); | ||
test('it can add advanced custom rules', () => { | ||
Iodine.addRule('equals', (value, param) => value == param); | ||
Iodine.setErrorMessages({ equals : `Value must be equal to '[PARAM]'` }); | ||
Iodine.addRule('equals', (value, param) => value == param); | ||
Iodine.setErrorMessages({ equals : `Value must be equal to '[PARAM]'` }); | ||
expect(Iodine.isEquals(1, 1)).toBe(true); | ||
expect(Iodine.isEquals(1, 2)).toBe(false); | ||
expect(Iodine.isEquals(1, 3)).toBe(false); | ||
expect(Iodine.is(1, ['required', 'equals:1'])).toBe(true); | ||
expect(Iodine.is(1, ['required', 'equals:2'])).toBe('equals:2'); | ||
expect(Iodine.is(1, ['required', 'equals:3'])).toBe('equals:3'); | ||
expect(Iodine.is(1, ['required', 'equals:1'])).toBe(true); | ||
expect(Iodine.is(1, ['required', 'equals:2'])).toBe('equals:2'); | ||
expect(Iodine.is(1, ['required', 'equals:3'])).toBe('equals:3'); | ||
expect(Iodine.getErrorMessage('equals:2')).toBe(`Value must be equal to '2'`); | ||
expect(Iodine.getErrorMessage('equals', 2)).toBe(`Value must be equal to '2'`); | ||
}); |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
119879
772