From 15e8a8e251741bf9b149e594a9e1296ecc48f438 Mon Sep 17 00:00:00 2001 From: Matt Kingshott <51963402+mattkingshott@users.noreply.github.com> Date: Fri, 3 Apr 2020 13:05:35 +0100 Subject: [PATCH] added custom rules, changed error messages, fixed same and different rules --- README.md | 34 ++++++++++++++++++-- dist/iodine.min.js | 2 +- dist/iodine.min.js.map | 2 +- dist/iodine.min.mjs | 2 +- dist/iodine.min.mjs.map | 2 +- dist/iodine.min.umd.js | 2 +- dist/iodine.min.umd.js.map | 2 +- package.json | 2 +- resources/version.svg | 4 +-- src/iodine.js | 63 ++++++++++++++++++++++---------------- tests/test.js | 47 +++++++++++++++++++++++++--- 11 files changed, 119 insertions(+), 43 deletions(-) diff --git a/README.md b/README.md index 971b6eb..1a7ba45 100755 --- a/README.md +++ b/README.md @@ -21,7 +21,7 @@ Iodine.js is a micro client-side validation library. It has no dependencies and The easiest way to pull Iodine into your project is via a CDN: ```html - + ``` You can also pull Iodine into your project via NPM: @@ -145,7 +145,7 @@ The following validation rules are available: | 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 strict compare) +| 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'` @@ -159,7 +159,7 @@ The following validation rules are available: | 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 strict compare) +| 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'` @@ -168,6 +168,34 @@ The following validation rules are available: Examine the tests for examples of how to use each rule. +## Custom rules + +Iodine allows you to add your own custom validation rules through the `addRule` method. This method excepts two parameters. The first, is the name of the rule. The second, is the `closure` that Iodine should execute when calling the rule e.g. + +```js +Iodine.addRule('lowerCase', (value) => value === value.toLowerCase()); +``` + +**IMPORTANT**: Iodine will automatically make the first letter of the rule's name uppercase and prefix it with 'is'. You should therefore avoid adding the prefix yourself e.g. + +```js +Iodine.addRule('lowerCase'); // correct +Iodine.addRule('isLowerCase'); // wrong +``` + +If your rule needs to accept a parameter, simply include it in your `closure` as the second argument e.g. + +```js +Iodine.addRule('equals', (value, param) => value == param); +``` + +You can also add error messages for your custom rules e.g. + +```js +Iodine.addRule('equals', (value, param) => value == param); +Iodine.setErrorMessages({ equals : `Value must be equal to '[PARAM]'` }); +``` + ## Contributing Thank you for considering a contribution to Iodine. You are welcome to submit a PR containing additional rules, however to be accepted, they must explain what they do, be useful to others, and include a suitable test to confirm they work correctly. diff --git a/dist/iodine.min.js b/dist/iodine.min.js index 023adcc..f552b5e 100644 --- a/dist/iodine.min.js +++ b/dist/iodine.min.js @@ -1,2 +1,2 @@ -var t=function(){this.messages=this._defaultMessages()};t.prototype._dateCompare=function(t,e,r,i){return void 0===i&&(i=!1),!!this.isDate(t)&&!(!this.isDate(e)&&!this.isInteger(e))&&(e="number"==typeof e?e:e.getTime(),"less"===r&&i?t.getTime()<=e:"less"!==r||i?"more"===r&&i?t.getTime()>=e:"more"!==r||i?void 0:t.getTime()>e:t.getTime()=e},t.prototype.isNotIn=function(t,e){return!this.isIn(t,e)},t.prototype.isNumeric=function(t){return!isNaN(parseFloat(t))&&isFinite(t)},t.prototype.isOptional=function(t){return[null,void 0,""].includes(t)},t.prototype.isRegexMatch=function(t,e){return new RegExp(e).test(String(t).toLowerCase())},t.prototype.isRequired=function(t){return!this.isOptional(t)},t.prototype.isSame=function(t,e){return t===e},t.prototype.isStartingWith=function(t,e){return this.isString(t)&&t.startsWith(e)},t.prototype.isString=function(t){return"string"==typeof t},t.prototype.isTruthy=function(t){return[1,"1",!0,"true"].includes(t)},t.prototype.isUrl=function(t){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(t).toLowerCase())},t.prototype.isUuid=function(t){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(t).toLowerCase())},t.prototype.is=function(t,e){if(void 0===e&&(e=[]),0===e.length)return!0;if("optional"===e[0]&&this.isOptional(t))return!0;for(var r=0;r=e:"more"!==r||i?void 0:t.getTime()>e:t.getTime()=e},t.prototype.isNotIn=function(t,e){return!this.isIn(t,e)},t.prototype.isNumeric=function(t){return!isNaN(parseFloat(t))&&isFinite(t)},t.prototype.isOptional=function(t){return[null,void 0,""].includes(t)},t.prototype.isRegexMatch=function(t,e){return new RegExp(e).test(String(t).toLowerCase())},t.prototype.isRequired=function(t){return!this.isOptional(t)},t.prototype.isSame=function(t,e){return t==e},t.prototype.isStartingWith=function(t,e){return this.isString(t)&&t.startsWith(e)},t.prototype.isString=function(t){return"string"==typeof t},t.prototype.isTruthy=function(t){return[1,"1",!0,"true"].includes(t)},t.prototype.isUrl=function(t){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(t).toLowerCase())},t.prototype.isUuid=function(t){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(t).toLowerCase())},t.prototype.is=function(t,e){if(void 0===e&&(e=[]),0===e.length)return!0;if("optional"===e[0]&&this.isOptional(t))return!0;for(var r=0;r= second;\n\t\tif (type === 'more' && ! equals) return first.getTime() > second;\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_defaultMessages()\n\t{\n\t\treturn {\n\t\t\tafter : `The date must be after: '[PARAM]'`,\n\t\t\tafterOrEqual : `The date must be after or equal to: '[PARAM]'`,\n\t\t\tarray : `Field must be an array`,\n\t\t\tbefore : `The date must be before: '[PARAM]'`,\n\t\t\tbeforeOrEqual : `The date must be before or equal to: '[PARAM]'`,\n\t\t\tboolean : `Field must be true or false`,\n\t\t\tdate : `Field must be a date`,\n\t\t\tdifferent : `Field must be different to '[PARAM]'`,\n\t\t\tendingWith : `Field must end with '[PARAM]'`,\n\t\t\temail : `Field must be a valid email address`,\n\t\t\tfalsy : `Field must be a falsy value (false, 'false', 0 or '0')`,\n\t\t\tin \t\t : `Field must be one of the following options: [PARAM]`,\n\t\t\tinteger : `Field must be an integer`,\n\t\t\tjson : `Field must be a parsable JSON object string`,\n\t\t\tmaximum : `Field must not be greater than '[PARAM]' in size or character length`,\n\t\t\tminimum : `Field must not be less than '[PARAM]' in size or character length`,\n\t\t\tnotIn : `Field must not be one of the following options: [PARAM]`,\n\t\t\tnumeric : `Field must be numeric`,\n\t\t\toptional : `Field is optional`,\n\t\t\tregexMatch : `Field must satisify the regular expression: [PARAM]`,\n\t\t\trequired : `Field must be present`,\n\t\t\tsame : `Field must be '[PARAM]'`,\n\t\t\tstartingWith : `Field must start with '[PARAM]'`,\n\t\t\tstring : `Field must be a string`,\n\t\t\ttruthy : `Field must be a truthy value (true, 'true', 1 or '1')`,\n\t\t\turl : `Field must be a valid url`,\n\t\t\tuuid : `Field must be a valid UUID`,\n\t\t};\n\t}\n\n\n\n\t/**\n\t * Retrieve an error message for the given rule.\n\t *\n\t **/\n\tgetErrorMessage(rule, arg = null)\n\t{\n\t\tlet key = rule.split(':')[0];\n\t\tlet param = arg || rule.split(':')[1];\n\n\t\tif (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {\n\t\t\tparam = new Date(parseInt(param)).toLocaleTimeString(undefined, {\n\t\t\t\tyear: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric'\n\t\t\t});\n\t\t}\n\n\t\treturn param === undefined\n\t\t\t ? this.messages[key]\n\t\t\t : this.messages[key].replace(\"[PARAM]\", param);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after another given date.\n\t *\n\t **/\n\tisAfter(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after or equal to another given date.\n\t *\n\t **/\n\tisAfterOrEqual(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an array.\n\t *\n\t **/\n\tisArray(value)\n\t{\n\t\treturn Array.isArray(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before another given date.\n\t *\n\t **/\n\tisBefore(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before or equal to another given date.\n\t *\n\t **/\n\tisBeforeOrEqual(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a boolean.\n\t *\n\t **/\n\tisBoolean(value)\n\t{\n\t\treturn [true, false].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a date object.\n\t *\n\t **/\n\tisDate(value)\n\t{\n\t\treturn value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is different to another given value.\n\t *\n\t **/\n\tisDifferent(value, different)\n\t{\n\t\treturn value !== different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value ends with another given value.\n\t *\n\t **/\n\tisEndingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.endsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid email address.\n\t *\n\t **/\n\tisEmail(value)\n\t{\n\t\treturn new RegExp('^\\\\S+@\\\\S+[\\\\.][0-9a-z]+$').test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is falsy.\n\t *\n\t **/\n\tisFalsy(value)\n\t{\n\t\treturn [0, '0', false, 'false'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is within the given array of options.\n\t *\n\t **/\n\tisIn(value, options)\n\t{\n\t\toptions = typeof options === 'string'\n\t\t\t\t? options.split(\",\")\n\t\t\t\t: options;\n\n\t\treturn options.includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an integer.\n\t *\n\t **/\n\tisInteger(value)\n\t{\n\t\treturn Number.isInteger(value) && parseInt(value).toString() === value.toString();\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a JSON string.\n\t *\n\t **/\n\tisJson(value)\n\t{\n\t\ttry {\n \treturn typeof JSON.parse(value) === 'object';\n\t\t} catch (e) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given maximum limit.\n\t *\n\t **/\n\tisMaximum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) <= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given minimum limit.\n\t *\n\t **/\n\tisMinimum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) >= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is not within the given array of options.\n\t *\n\t **/\n\tisNotIn(value, options)\n\t{\n\t\treturn ! this.isIn(value, options);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is numeric (an integer or a float).\n\t *\n\t **/\n\tisNumeric(value)\n\t{\n\t\treturn ! isNaN(parseFloat(value)) && isFinite(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is optional.\n\t *\n\t **/\n\tisOptional(value)\n\t{\n\t\treturn [null, undefined, ''].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value satisifies the given regular expression.\n\t *\n\t **/\n\tisRegexMatch(value, expression)\n\t{\n\t\treturn new RegExp(expression).test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is present.\n\t *\n\t **/\n\tisRequired(value)\n\t{\n\t\treturn ! this.isOptional(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is the same as another given value.\n\t *\n\t **/\n\tisSame(value, different)\n\t{\n\t\treturn value === different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value starts with another given value.\n\t *\n\t **/\n\tisStartingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.startsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a string.\n\t *\n\t **/\n\tisString(value)\n\t{\n\t\treturn typeof value === 'string';\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is truthy.\n\t *\n\t **/\n\tisTruthy(value)\n\t{\n\t\treturn [1, '1', true, 'true'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid URL.\n\t *\n\t **/\n\tisUrl(value)\n\t{\n\t\treturn 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_]*)?$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid UUID.\n\t *\n\t **/\n\tisUuid(value)\n\t{\n\t\treturn 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}$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine whether the given value meets the given rules.\n\t *\n\t **/\n\tis(value, rules = [])\n\t{\n\t\t// Check if no rules were specified\n\t\tif (rules.length === 0) return true;\n\n\t\t// Check for an optional value\n\t\tif (rules[0] === 'optional' && this.isOptional(value)) return true;\n\n\t\t// Iterate through the rules\n\t\tfor (let index = 0; index < rules.length; index++) {\n\n\t\t\t// Ignore optional rules\n\t\t\tif (rules[index] === 'optional') continue;\n\n\t\t\t// Determine the method to use\n\t\t\tlet rule = rules[index].split(':')[0][0].toUpperCase()\n\t\t\t\t\t + rules[index].split(':')[0].slice(1);\n\n\t\t\t// Validate the value against the method\n\t\t\tlet result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]);\n\n\t\t\t// Check if the value failed validation\n\t\t\tif (! result) return rules[index];\n\n\t\t}\n\n\t\t// Otherwise, the value is valid\n\t\treturn true;\n\t}\n\n\n\n\t/**\n\t * Replace the default error messages with a new set.\n\t *\n\t **/\n\tsetErrorMessages(messages)\n\t{\n\t\tthis.messages = messages;\n\t}\n\n}\n\n\n\n/**\n * Create an instance of the library.\n *\n **/\nwindow.Iodine = new Iodine();"],"names":["Iodine","constructor","messages","this","_defaultMessages","_dateCompare","first","second","type","equals","isDate","isInteger","getTime","after","afterOrEqual","array","before","beforeOrEqual","boolean","date","different","endingWith","email","falsy","in","integer","json","maximum","minimum","notIn","numeric","optional","regexMatch","required","same","startingWith","string","truthy","url","uuid","getErrorMessage","rule","arg","key","split","param","includes","Date","parseInt","toLocaleTimeString","undefined","year","month","day","hour","minute","replace","isAfter","value","isAfterOrEqual","isArray","Array","isBefore","isBeforeOrEqual","isBoolean","Object","prototype","toString","call","isNaN","isDifferent","isEndingWith","sub","isString","endsWith","isEmail","RegExp","test","String","toLowerCase","isFalsy","isIn","options","Number","isJson","JSON","parse","e","isMaximum","limit","length","parseFloat","isMinimum","isNotIn","isNumeric","isFinite","isOptional","isRegexMatch","expression","isRequired","isSame","isStartingWith","startsWith","isTruthy","isUrl","isUuid","is","rules","let","index","toUpperCase","slice","apply","setErrorMessages","window"],"mappings":"AASe,IAAMA,EAOpBC,gBAEMC,SAAWC,KAAKC,gCAStBC,sBAAaC,EAAOC,EAAQC,EAAMC,0BAAS,KAEpCN,KAAKO,OAAOJ,OAEZH,KAAKO,OAAOH,KAAaJ,KAAKQ,UAAUJ,MAE9CA,EAA2B,iBAAXA,EAAsBA,EAASA,EAAOK,UAEzC,SAATJ,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,EACZ,SAATD,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,SAAeH,EAAMM,UAAYL,EAFlBD,EAAMM,UAAYL,gBAW3DH,kCAEQ,CACNS,MAAiB,oCACjBC,aAAiB,gDACjBC,MAAiB,yBACjBC,OAAiB,qCACjBC,cAAiB,iDACjBC,QAAiB,8BACjBC,KAAiB,uBACjBC,UAAiB,uCACjBC,WAAiB,gCACjBC,MAAiB,sCACjBC,MAAiB,yDACjBC,GAAc,sDACdC,QAAiB,2BACjBC,KAAiB,8CACjBC,QAAiB,uEACjBC,QAAiB,oEACjBC,MAAiB,0DACjBC,QAAiB,wBACjBC,SAAiB,oBACjBC,WAAiB,sDACjBC,SAAiB,wBACjBC,KAAiB,0BACjBC,aAAiB,kCACjBC,OAAiB,yBACjBC,OAAiB,wDACjBC,IAAiB,4BACjBC,KAAiB,2CAUnBC,yBAAgBC,EAAMC,kBAAM,UAEvBC,EAAQF,EAAKG,MAAM,KAAK,GACxBC,EAAQH,GAAOD,EAAKG,MAAM,KAAK,SAE/B,CAAC,QAAS,eAAgB,SAAU,iBAAiBE,SAASH,KACjEE,EAAQ,IAAIE,KAAKC,SAASH,IAAQI,wBAAmBC,EAAW,CAC/DC,KAAM,UAAWC,MAAO,QAASC,IAAK,UAAWC,KAAM,UAAWC,OAAQ,kBAI3DL,IAAVL,EACH1C,KAAKD,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B,UAEL/E,KAAK8E,KAAKvB,EAAOwB,gBAS3BY,mBAAUpC,UAEAW,MAAMsB,WAAWjC,KAAWqC,SAASrC,gBAS/CsC,oBAAWtC,SAEH,CAAC,UAAMR,EAAW,IAAIJ,SAASY,gBASvCuC,sBAAavC,EAAOwC,UAEZ,IAAItB,OAAOsB,GAAYrB,KAAKC,OAAOpB,GAAOqB,4BASlDoB,oBAAWzC,UAEDvD,KAAK6F,WAAWtC,gBAS1B0C,gBAAO1C,EAAOtC,UAENsC,IAAUtC,eASlBiF,wBAAe3C,EAAOc,UAEdrE,KAAKsE,SAASf,IAAUA,EAAM4C,WAAW9B,gBASjDC,kBAASf,SAEgB,iBAAVA,eASf6C,kBAAS7C,SAED,CAAC,EAAG,KAAK,EAAM,QAAQZ,SAASY,gBASxC8C,eAAM9C,UAEE,IAAIkB,OAAO,yKACfC,KAAKC,OAAOpB,GAAOqB,4BASvB0B,gBAAO/C,UAEC,IAAIkB,OAAO,6EACfC,KAAKC,OAAOpB,GAAOqB,4BASvB2B,YAAGhD,EAAOiD,qBAAQ,IAGI,IAAjBA,EAAMjB,OAAc,OAAO,KAGd,aAAbiB,EAAM,IAAqBxG,KAAK6F,WAAWtC,GAAQ,OAAO,MAGzDkD,IAAIC,EAAQ,EAAGA,EAAQF,EAAMjB,OAAQmB,OAGpB,aAAjBF,EAAME,KAOG1G,WAJFwG,EAAME,GAAOjE,MAAM,KAAK,GAAG,GAAGkE,cACpCH,EAAME,GAAOjE,MAAM,KAAK,GAAGmE,MAAM,KAGPC,MAAM7G,KAAM,CAACuD,EAAOiD,EAAME,GAAOjE,MAAM,KAAK,KAG7D,OAAO+D,EAAME,UAKrB,eASRI,0BAAiB/G,QAEXA,SAAWA,GAWlBgH,OAAOlH,OAAS,IAAIA"} \ No newline at end of file +{"version":3,"file":"iodine.min.js","sources":["../src/iodine.js"],"sourcesContent":["/*\n|--------------------------------------------------------------------------\n| Iodine - JavaScript Library\n|--------------------------------------------------------------------------\n|\n| This library contains a collection of useful validation rules that can\n| be used to quickly verify whether items meet certain conditions.\n|\n*/\nexport default class Iodine\n{\n\n\t/**\n\t * Constructor.\n\t *\n\t **/\n\tconstructor()\n\t{\n\t\tthis.messages = this._defaultMessages();\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_dateCompare(first, second, type, equals = false)\n\t{\n\t\tif (! this.isDate(first)) return false;\n\n\t\tif (! this.isDate(second) && ! this.isInteger(second)) return false;\n\n\t\tsecond = typeof second === 'number' ? second : second.getTime();\n\n\t\tif (type === 'less' && equals) return first.getTime() <= second;\n\t\tif (type === 'less' && ! equals) return first.getTime() < second;\n\t\tif (type === 'more' && equals) return first.getTime() >= second;\n\t\tif (type === 'more' && ! equals) return first.getTime() > second;\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_defaultMessages()\n\t{\n\t\treturn {\n\t\t\tafter : `The date must be after: '[PARAM]'`,\n\t\t\tafterOrEqual : `The date must be after or equal to: '[PARAM]'`,\n\t\t\tarray : `Value must be an array`,\n\t\t\tbefore : `The date must be before: '[PARAM]'`,\n\t\t\tbeforeOrEqual : `The date must be before or equal to: '[PARAM]'`,\n\t\t\tboolean : `Value must be true or false`,\n\t\t\tdate : `Value must be a date`,\n\t\t\tdifferent : `Value must be different to '[PARAM]'`,\n\t\t\tendingWith : `Value must end with '[PARAM]'`,\n\t\t\temail : `Value must be a valid email address`,\n\t\t\tfalsy : `Value must be a falsy value (false, 'false', 0 or '0')`,\n\t\t\tin \t\t : `Value must be one of the following options: [PARAM]`,\n\t\t\tinteger : `Value must be an integer`,\n\t\t\tjson : `Value must be a parsable JSON object string`,\n\t\t\tmaximum : `Value must not be greater than '[PARAM]' in size or character length`,\n\t\t\tminimum : `Value must not be less than '[PARAM]' in size or character length`,\n\t\t\tnotIn : `Value must not be one of the following options: [PARAM]`,\n\t\t\tnumeric : `Value must be numeric`,\n\t\t\toptional : `Value is optional`,\n\t\t\tregexMatch : `Value must satisify the regular expression: [PARAM]`,\n\t\t\trequired : `Value must be present`,\n\t\t\tsame : `Value must be '[PARAM]'`,\n\t\t\tstartingWith : `Value must start with '[PARAM]'`,\n\t\t\tstring : `Value must be a string`,\n\t\t\ttruthy : `Value must be a truthy value (true, 'true', 1 or '1')`,\n\t\t\turl : `Value must be a valid url`,\n\t\t\tuuid : `Value must be a valid UUID`,\n\t\t};\n\t}\n\n\n\n\t/**\n\t * Attach a custom validation rule to the library.\n\t *\n\t **/\n\taddRule(name, closure)\n\t{\n\t\tIodine.prototype[`is${name[0].toUpperCase()}${name.slice(1)}`] = closure;\n\t}\n\n\n\n\t/**\n\t * Retrieve an error message for the given rule.\n\t *\n\t **/\n\tgetErrorMessage(rule, arg = undefined)\n\t{\n\t\tlet key = rule.split(':')[0];\n\t\tlet param = arg || rule.split(':')[1];\n\n\t\tif (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {\n\t\t\tparam = new Date(parseInt(param)).toLocaleTimeString(undefined, {\n\t\t\t\tyear: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric'\n\t\t\t});\n\t\t}\n\n\t\treturn [null, undefined].includes(param)\n\t\t\t ? this.messages[key]\n\t\t\t : this.messages[key].replace(\"[PARAM]\", param);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after another given date.\n\t *\n\t **/\n\tisAfter(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after or equal to another given date.\n\t *\n\t **/\n\tisAfterOrEqual(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an array.\n\t *\n\t **/\n\tisArray(value)\n\t{\n\t\treturn Array.isArray(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before another given date.\n\t *\n\t **/\n\tisBefore(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before or equal to another given date.\n\t *\n\t **/\n\tisBeforeOrEqual(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a boolean.\n\t *\n\t **/\n\tisBoolean(value)\n\t{\n\t\treturn [true, false].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a date object.\n\t *\n\t **/\n\tisDate(value)\n\t{\n\t\treturn value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is different to another given value.\n\t *\n\t **/\n\tisDifferent(value, different)\n\t{\n\t\treturn value != different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value ends with another given value.\n\t *\n\t **/\n\tisEndingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.endsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid email address.\n\t *\n\t **/\n\tisEmail(value)\n\t{\n\t\treturn new RegExp('^\\\\S+@\\\\S+[\\\\.][0-9a-z]+$').test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is falsy.\n\t *\n\t **/\n\tisFalsy(value)\n\t{\n\t\treturn [0, '0', false, 'false'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is within the given array of options.\n\t *\n\t **/\n\tisIn(value, options)\n\t{\n\t\toptions = typeof options === 'string'\n\t\t\t\t? options.split(\",\")\n\t\t\t\t: options;\n\n\t\treturn options.includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an integer.\n\t *\n\t **/\n\tisInteger(value)\n\t{\n\t\treturn Number.isInteger(value) && parseInt(value).toString() === value.toString();\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a JSON string.\n\t *\n\t **/\n\tisJson(value)\n\t{\n\t\ttry {\n \treturn typeof JSON.parse(value) === 'object';\n\t\t} catch (e) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given maximum limit.\n\t *\n\t **/\n\tisMaximum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) <= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given minimum limit.\n\t *\n\t **/\n\tisMinimum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) >= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is not within the given array of options.\n\t *\n\t **/\n\tisNotIn(value, options)\n\t{\n\t\treturn ! this.isIn(value, options);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is numeric (an integer or a float).\n\t *\n\t **/\n\tisNumeric(value)\n\t{\n\t\treturn ! isNaN(parseFloat(value)) && isFinite(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is optional.\n\t *\n\t **/\n\tisOptional(value)\n\t{\n\t\treturn [null, undefined, ''].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value satisifies the given regular expression.\n\t *\n\t **/\n\tisRegexMatch(value, expression)\n\t{\n\t\treturn new RegExp(expression).test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is present.\n\t *\n\t **/\n\tisRequired(value)\n\t{\n\t\treturn ! this.isOptional(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is the same as another given value.\n\t *\n\t **/\n\tisSame(value, same)\n\t{\n\t\treturn value == same;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value starts with another given value.\n\t *\n\t **/\n\tisStartingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.startsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a string.\n\t *\n\t **/\n\tisString(value)\n\t{\n\t\treturn typeof value === 'string';\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is truthy.\n\t *\n\t **/\n\tisTruthy(value)\n\t{\n\t\treturn [1, '1', true, 'true'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid URL.\n\t *\n\t **/\n\tisUrl(value)\n\t{\n\t\treturn 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_]*)?$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid UUID.\n\t *\n\t **/\n\tisUuid(value)\n\t{\n\t\treturn 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}$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine whether the given value meets the given rules.\n\t *\n\t **/\n\tis(value, rules = [])\n\t{\n\t\t// Check if no rules were specified\n\t\tif (rules.length === 0) return true;\n\n\t\t// Check for an optional value\n\t\tif (rules[0] === 'optional' && this.isOptional(value)) return true;\n\n\t\t// Iterate through the rules\n\t\tfor (let index = 0; index < rules.length; index++) {\n\n\t\t\t// Ignore optional rules\n\t\t\tif (rules[index] === 'optional') continue;\n\n\t\t\t// Determine the method to use\n\t\t\tlet rule = rules[index].split(':')[0][0].toUpperCase()\n\t\t\t\t\t + rules[index].split(':')[0].slice(1);\n\n\t\t\t// Validate the value against the method\n\t\t\tlet result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]);\n\n\t\t\t// Check if the value failed validation\n\t\t\tif (! result) return rules[index];\n\n\t\t}\n\n\t\t// Otherwise, the value is valid\n\t\treturn true;\n\t}\n\n\n\n\t/**\n\t * Replace the default error messages with a new set.\n\t *\n\t **/\n\tsetErrorMessages(messages)\n\t{\n\t\tthis.messages = messages;\n\t}\n\n}\n\n\n\n/**\n * Create an instance of the library.\n *\n **/\nwindow.Iodine = new Iodine();"],"names":["Iodine","constructor","messages","this","_defaultMessages","_dateCompare","first","second","type","equals","isDate","isInteger","getTime","after","afterOrEqual","array","before","beforeOrEqual","boolean","date","different","endingWith","email","falsy","in","integer","json","maximum","minimum","notIn","numeric","optional","regexMatch","required","same","startingWith","string","truthy","url","uuid","addRule","name","closure","prototype","toUpperCase","slice","getErrorMessage","rule","arg","undefined","key","split","param","includes","Date","parseInt","toLocaleTimeString","year","month","day","hour","minute","replace","isAfter","value","isAfterOrEqual","isArray","Array","isBefore","isBeforeOrEqual","isBoolean","Object","toString","call","isNaN","isDifferent","isEndingWith","sub","isString","endsWith","isEmail","RegExp","test","String","toLowerCase","isFalsy","isIn","options","Number","isJson","JSON","parse","e","isMaximum","limit","length","parseFloat","isMinimum","isNotIn","isNumeric","isFinite","isOptional","isRegexMatch","expression","isRequired","isSame","isStartingWith","startsWith","isTruthy","isUrl","isUuid","is","rules","let","index","apply","setErrorMessages","window"],"mappings":"AASe,IAAMA,EAOpBC,gBAEMC,SAAWC,KAAKC,gCAStBC,sBAAaC,EAAOC,EAAQC,EAAMC,0BAAS,KAEpCN,KAAKO,OAAOJ,OAEZH,KAAKO,OAAOH,KAAaJ,KAAKQ,UAAUJ,MAE9CA,EAA2B,iBAAXA,EAAsBA,EAASA,EAAOK,UAEzC,SAATJ,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,EACZ,SAATD,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,SAAeH,EAAMM,UAAYL,EAFlBD,EAAMM,UAAYL,gBAW3DH,kCAEQ,CACNS,MAAiB,oCACjBC,aAAiB,gDACjBC,MAAiB,yBACjBC,OAAiB,qCACjBC,cAAiB,iDACjBC,QAAiB,8BACjBC,KAAiB,uBACjBC,UAAiB,uCACjBC,WAAiB,gCACjBC,MAAiB,sCACjBC,MAAiB,yDACjBC,GAAc,sDACdC,QAAiB,2BACjBC,KAAiB,8CACjBC,QAAiB,uEACjBC,QAAiB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4BASvB2B,YAAG/C,EAAOgD,qBAAQ,IAGI,IAAjBA,EAAMjB,OAAc,OAAO,KAGd,aAAbiB,EAAM,IAAqB7G,KAAKkG,WAAWrC,GAAQ,OAAO,MAGzDiD,IAAIC,EAAQ,EAAGA,EAAQF,EAAMjB,OAAQmB,OAGpB,aAAjBF,EAAME,KAOG/G,WAJF6G,EAAME,GAAO/D,MAAM,KAAK,GAAG,GAAGP,cACpCoE,EAAME,GAAO/D,MAAM,KAAK,GAAGN,MAAM,KAGPsE,MAAMhH,KAAM,CAAC6D,EAAOgD,EAAME,GAAO/D,MAAM,KAAK,KAG7D,OAAO6D,EAAME,UAKrB,eASRE,0BAAiBlH,QAEXA,SAAWA,GAWlBmH,OAAOrH,OAAS,IAAIA"} \ No newline at end of file diff --git a/dist/iodine.min.mjs b/dist/iodine.min.mjs index cd1e2b1..e38dc8c 100644 --- a/dist/iodine.min.mjs +++ b/dist/iodine.min.mjs @@ -1,2 +1,2 @@ -var t=function(){this.messages=this._defaultMessages()};t.prototype._dateCompare=function(t,e,r,i){return void 0===i&&(i=!1),!!this.isDate(t)&&!(!this.isDate(e)&&!this.isInteger(e))&&(e="number"==typeof e?e:e.getTime(),"less"===r&&i?t.getTime()<=e:"less"!==r||i?"more"===r&&i?t.getTime()>=e:"more"!==r||i?void 0:t.getTime()>e:t.getTime()=e},t.prototype.isNotIn=function(t,e){return!this.isIn(t,e)},t.prototype.isNumeric=function(t){return!isNaN(parseFloat(t))&&isFinite(t)},t.prototype.isOptional=function(t){return[null,void 0,""].includes(t)},t.prototype.isRegexMatch=function(t,e){return new RegExp(e).test(String(t).toLowerCase())},t.prototype.isRequired=function(t){return!this.isOptional(t)},t.prototype.isSame=function(t,e){return t===e},t.prototype.isStartingWith=function(t,e){return this.isString(t)&&t.startsWith(e)},t.prototype.isString=function(t){return"string"==typeof t},t.prototype.isTruthy=function(t){return[1,"1",!0,"true"].includes(t)},t.prototype.isUrl=function(t){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(t).toLowerCase())},t.prototype.isUuid=function(t){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(t).toLowerCase())},t.prototype.is=function(t,e){if(void 0===e&&(e=[]),0===e.length)return!0;if("optional"===e[0]&&this.isOptional(t))return!0;for(var r=0;r=e:"more"!==r||i?void 0:t.getTime()>e:t.getTime()=e},t.prototype.isNotIn=function(t,e){return!this.isIn(t,e)},t.prototype.isNumeric=function(t){return!isNaN(parseFloat(t))&&isFinite(t)},t.prototype.isOptional=function(t){return[null,void 0,""].includes(t)},t.prototype.isRegexMatch=function(t,e){return new RegExp(e).test(String(t).toLowerCase())},t.prototype.isRequired=function(t){return!this.isOptional(t)},t.prototype.isSame=function(t,e){return t==e},t.prototype.isStartingWith=function(t,e){return this.isString(t)&&t.startsWith(e)},t.prototype.isString=function(t){return"string"==typeof t},t.prototype.isTruthy=function(t){return[1,"1",!0,"true"].includes(t)},t.prototype.isUrl=function(t){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(t).toLowerCase())},t.prototype.isUuid=function(t){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(t).toLowerCase())},t.prototype.is=function(t,e){if(void 0===e&&(e=[]),0===e.length)return!0;if("optional"===e[0]&&this.isOptional(t))return!0;for(var r=0;r= second;\n\t\tif (type === 'more' && ! equals) return first.getTime() > second;\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_defaultMessages()\n\t{\n\t\treturn {\n\t\t\tafter : `The date must be after: '[PARAM]'`,\n\t\t\tafterOrEqual : `The date must be after or equal to: '[PARAM]'`,\n\t\t\tarray : `Field must be an array`,\n\t\t\tbefore : `The date must be before: '[PARAM]'`,\n\t\t\tbeforeOrEqual : `The date must be before or equal to: '[PARAM]'`,\n\t\t\tboolean : `Field must be true or false`,\n\t\t\tdate : `Field must be a date`,\n\t\t\tdifferent : `Field must be different to '[PARAM]'`,\n\t\t\tendingWith : `Field must end with '[PARAM]'`,\n\t\t\temail : `Field must be a valid email address`,\n\t\t\tfalsy : `Field must be a falsy value (false, 'false', 0 or '0')`,\n\t\t\tin \t\t : `Field must be one of the following options: [PARAM]`,\n\t\t\tinteger : `Field must be an integer`,\n\t\t\tjson : `Field must be a parsable JSON object string`,\n\t\t\tmaximum : `Field must not be greater than '[PARAM]' in size or character length`,\n\t\t\tminimum : `Field must not be less than '[PARAM]' in size or character length`,\n\t\t\tnotIn : `Field must not be one of the following options: [PARAM]`,\n\t\t\tnumeric : `Field must be numeric`,\n\t\t\toptional : `Field is optional`,\n\t\t\tregexMatch : `Field must satisify the regular expression: [PARAM]`,\n\t\t\trequired : `Field must be present`,\n\t\t\tsame : `Field must be '[PARAM]'`,\n\t\t\tstartingWith : `Field must start with '[PARAM]'`,\n\t\t\tstring : `Field must be a string`,\n\t\t\ttruthy : `Field must be a truthy value (true, 'true', 1 or '1')`,\n\t\t\turl : `Field must be a valid url`,\n\t\t\tuuid : `Field must be a valid UUID`,\n\t\t};\n\t}\n\n\n\n\t/**\n\t * Retrieve an error message for the given rule.\n\t *\n\t **/\n\tgetErrorMessage(rule, arg = null)\n\t{\n\t\tlet key = rule.split(':')[0];\n\t\tlet param = arg || rule.split(':')[1];\n\n\t\tif (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {\n\t\t\tparam = new Date(parseInt(param)).toLocaleTimeString(undefined, {\n\t\t\t\tyear: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric'\n\t\t\t});\n\t\t}\n\n\t\treturn param === undefined\n\t\t\t ? this.messages[key]\n\t\t\t : this.messages[key].replace(\"[PARAM]\", param);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after another given date.\n\t *\n\t **/\n\tisAfter(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after or equal to another given date.\n\t *\n\t **/\n\tisAfterOrEqual(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an array.\n\t *\n\t **/\n\tisArray(value)\n\t{\n\t\treturn Array.isArray(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before another given date.\n\t *\n\t **/\n\tisBefore(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before or equal to another given date.\n\t *\n\t **/\n\tisBeforeOrEqual(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a boolean.\n\t *\n\t **/\n\tisBoolean(value)\n\t{\n\t\treturn [true, false].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a date object.\n\t *\n\t **/\n\tisDate(value)\n\t{\n\t\treturn value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is different to another given value.\n\t *\n\t **/\n\tisDifferent(value, different)\n\t{\n\t\treturn value !== different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value ends with another given value.\n\t *\n\t **/\n\tisEndingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.endsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid email address.\n\t *\n\t **/\n\tisEmail(value)\n\t{\n\t\treturn new RegExp('^\\\\S+@\\\\S+[\\\\.][0-9a-z]+$').test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is falsy.\n\t *\n\t **/\n\tisFalsy(value)\n\t{\n\t\treturn [0, '0', false, 'false'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is within the given array of options.\n\t *\n\t **/\n\tisIn(value, options)\n\t{\n\t\toptions = typeof options === 'string'\n\t\t\t\t? options.split(\",\")\n\t\t\t\t: options;\n\n\t\treturn options.includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an integer.\n\t *\n\t **/\n\tisInteger(value)\n\t{\n\t\treturn Number.isInteger(value) && parseInt(value).toString() === value.toString();\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a JSON string.\n\t *\n\t **/\n\tisJson(value)\n\t{\n\t\ttry {\n \treturn typeof JSON.parse(value) === 'object';\n\t\t} catch (e) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given maximum limit.\n\t *\n\t **/\n\tisMaximum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) <= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given minimum limit.\n\t *\n\t **/\n\tisMinimum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) >= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is not within the given array of options.\n\t *\n\t **/\n\tisNotIn(value, options)\n\t{\n\t\treturn ! this.isIn(value, options);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is numeric (an integer or a float).\n\t *\n\t **/\n\tisNumeric(value)\n\t{\n\t\treturn ! isNaN(parseFloat(value)) && isFinite(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is optional.\n\t *\n\t **/\n\tisOptional(value)\n\t{\n\t\treturn [null, undefined, ''].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value satisifies the given regular expression.\n\t *\n\t **/\n\tisRegexMatch(value, expression)\n\t{\n\t\treturn new RegExp(expression).test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is present.\n\t *\n\t **/\n\tisRequired(value)\n\t{\n\t\treturn ! this.isOptional(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is the same as another given value.\n\t *\n\t **/\n\tisSame(value, different)\n\t{\n\t\treturn value === different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value starts with another given value.\n\t *\n\t **/\n\tisStartingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.startsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a string.\n\t *\n\t **/\n\tisString(value)\n\t{\n\t\treturn typeof value === 'string';\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is truthy.\n\t *\n\t **/\n\tisTruthy(value)\n\t{\n\t\treturn [1, '1', true, 'true'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid URL.\n\t *\n\t **/\n\tisUrl(value)\n\t{\n\t\treturn 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_]*)?$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid UUID.\n\t *\n\t **/\n\tisUuid(value)\n\t{\n\t\treturn 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}$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine whether the given value meets the given rules.\n\t *\n\t **/\n\tis(value, rules = [])\n\t{\n\t\t// Check if no rules were specified\n\t\tif (rules.length === 0) return true;\n\n\t\t// Check for an optional value\n\t\tif (rules[0] === 'optional' && this.isOptional(value)) return true;\n\n\t\t// Iterate through the rules\n\t\tfor (let index = 0; index < rules.length; index++) {\n\n\t\t\t// Ignore optional rules\n\t\t\tif (rules[index] === 'optional') continue;\n\n\t\t\t// Determine the method to use\n\t\t\tlet rule = rules[index].split(':')[0][0].toUpperCase()\n\t\t\t\t\t + rules[index].split(':')[0].slice(1);\n\n\t\t\t// Validate the value against the method\n\t\t\tlet result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]);\n\n\t\t\t// Check if the value failed validation\n\t\t\tif (! result) return rules[index];\n\n\t\t}\n\n\t\t// Otherwise, the value is valid\n\t\treturn true;\n\t}\n\n\n\n\t/**\n\t * Replace the default error messages with a new set.\n\t *\n\t **/\n\tsetErrorMessages(messages)\n\t{\n\t\tthis.messages = messages;\n\t}\n\n}\n\n\n\n/**\n * Create an instance of the library.\n *\n **/\nwindow.Iodine = new Iodine();"],"names":["Iodine","constructor","messages","this","_defaultMessages","_dateCompare","first","second","type","equals","isDate","isInteger","getTime","after","afterOrEqual","array","before","beforeOrEqual","boolean","date","different","endingWith","email","falsy","in","integer","json","maximum","minimum","notIn","numeric","optional","regexMatch","required","same","startingWith","string","truthy","url","uuid","getErrorMessage","rule","arg","key","split","param","includes","Date","parseInt","toLocaleTimeString","undefined","year","month","day","hour","minute","replace","isAfter","value","isAfterOrEqual","isArray","Array","isBefore","isBeforeOrEqual","isBoolean","Object","prototype","toString","call","isNaN","isDifferent","isEndingWith","sub","isString","endsWith","isEmail","RegExp","test","String","toLowerCase","isFalsy","isIn","options","Number","isJson","JSON","parse","e","isMaximum","limit","length","parseFloat","isMinimum","isNotIn","isNumeric","isFinite","isOptional","isRegexMatch","expression","isRequired","isSame","isStartingWith","startsWith","isTruthy","isUrl","isUuid","is","rules","let","index","toUpperCase","slice","apply","setErrorMessages","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rC,gBAS/CsC,oBAAWtC,SAEH,CAAC,UAAMR,EAAW,IAAIJ,SAASY,gBASvCuC,sBAAavC,EAAOwC,UAEZ,IAAItB,OAAOsB,GAAYrB,KAAKC,OAAOpB,GAAOqB,4BASlDoB,oBAAWzC,UAEDvD,KAAK6F,WAAWtC,gBAS1B0C,gBAAO1C,EAAOtC,UAENsC,IAAUtC,eASlBiF,wBAAe3C,EAAOc,UAEdrE,KAAKsE,SAASf,IAAUA,EAAM4C,WAAW9B,gBASjDC,kBAASf,SAEgB,iBAAVA,eASf6C,kBAAS7C,SAED,CAAC,EAAG,KAAK,EAAM,QAAQZ,SAASY,gBASxC8C,eAAM9C,UAEE,IAAIkB,OAAO,yKACfC,KAAKC,OAAOpB,GAAOqB,4BASvB0B,gBAAO/C,UAEC,IAAIkB,OAAO,6EACfC,KAAKC,OAAOpB,GAAOqB,4BASvB2B,YAAGhD,EAAOiD,qBAAQ,IAGI,IAAjBA,EAAMjB,OAAc,OAAO,KAGd,aAAbiB,EAAM,IAAqBxG,KAAK6F,WAAWtC,GAAQ,OAAO,MAGzDkD,IAAIC,EAAQ,EAAGA,EAAQF,EAAMjB,OAAQmB,OAGpB,aAAjBF,EAAME,KAOG1G,WAJFwG,EAAME,GAAOjE,MAAM,KAAK,GAAG,GAAGkE,cACpCH,EAAME,GAAOjE,MAAM,KAAK,GAAGmE,MAAM,KAGPC,MAAM7G,KAAM,CAACuD,EAAOiD,EAAME,GAAOjE,MAAM,KAAK,KAG7D,OAAO+D,EAAME,UAKrB,eASRI,0BAAiB/G,QAEXA,SAAWA,GAWlBgH,OAAOlH,OAAS,IAAIA"} \ No newline at end of file +{"version":3,"file":"iodine.min.mjs","sources":["../src/iodine.js"],"sourcesContent":["/*\n|--------------------------------------------------------------------------\n| Iodine - JavaScript Library\n|--------------------------------------------------------------------------\n|\n| This library contains a collection of useful validation rules that can\n| be used to quickly verify whether items meet certain conditions.\n|\n*/\nexport default class Iodine\n{\n\n\t/**\n\t * Constructor.\n\t *\n\t **/\n\tconstructor()\n\t{\n\t\tthis.messages = this._defaultMessages();\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_dateCompare(first, second, type, equals = false)\n\t{\n\t\tif (! this.isDate(first)) return false;\n\n\t\tif (! this.isDate(second) && ! this.isInteger(second)) return false;\n\n\t\tsecond = typeof second === 'number' ? second : second.getTime();\n\n\t\tif (type === 'less' && equals) return first.getTime() <= second;\n\t\tif (type === 'less' && ! equals) return first.getTime() < second;\n\t\tif (type === 'more' && equals) return first.getTime() >= second;\n\t\tif (type === 'more' && ! equals) return first.getTime() > second;\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_defaultMessages()\n\t{\n\t\treturn {\n\t\t\tafter : `The date must be after: '[PARAM]'`,\n\t\t\tafterOrEqual : `The date must be after or equal to: '[PARAM]'`,\n\t\t\tarray : `Value must be an array`,\n\t\t\tbefore : `The date must be before: '[PARAM]'`,\n\t\t\tbeforeOrEqual : `The date must be before or equal to: '[PARAM]'`,\n\t\t\tboolean : `Value must be true or false`,\n\t\t\tdate : `Value must be a date`,\n\t\t\tdifferent : `Value must be different to '[PARAM]'`,\n\t\t\tendingWith : `Value must end with '[PARAM]'`,\n\t\t\temail : `Value must be a valid email address`,\n\t\t\tfalsy : `Value must be a falsy value (false, 'false', 0 or '0')`,\n\t\t\tin \t\t : `Value must be one of the following options: [PARAM]`,\n\t\t\tinteger : `Value must be an integer`,\n\t\t\tjson : `Value must be a parsable JSON object string`,\n\t\t\tmaximum : `Value must not be greater than '[PARAM]' in size or character length`,\n\t\t\tminimum : `Value must not be less than '[PARAM]' in size or character length`,\n\t\t\tnotIn : `Value must not be one of the following options: [PARAM]`,\n\t\t\tnumeric : `Value must be numeric`,\n\t\t\toptional : `Value is optional`,\n\t\t\tregexMatch : `Value must satisify the regular expression: [PARAM]`,\n\t\t\trequired : `Value must be present`,\n\t\t\tsame : `Value must be '[PARAM]'`,\n\t\t\tstartingWith : `Value must start with '[PARAM]'`,\n\t\t\tstring : `Value must be a string`,\n\t\t\ttruthy : `Value must be a truthy value (true, 'true', 1 or '1')`,\n\t\t\turl : `Value must be a valid url`,\n\t\t\tuuid : `Value must be a valid UUID`,\n\t\t};\n\t}\n\n\n\n\t/**\n\t * Attach a custom validation rule to the library.\n\t *\n\t **/\n\taddRule(name, closure)\n\t{\n\t\tIodine.prototype[`is${name[0].toUpperCase()}${name.slice(1)}`] = closure;\n\t}\n\n\n\n\t/**\n\t * Retrieve an error message for the given rule.\n\t *\n\t **/\n\tgetErrorMessage(rule, arg = undefined)\n\t{\n\t\tlet key = rule.split(':')[0];\n\t\tlet param = arg || rule.split(':')[1];\n\n\t\tif (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {\n\t\t\tparam = new Date(parseInt(param)).toLocaleTimeString(undefined, {\n\t\t\t\tyear: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric'\n\t\t\t});\n\t\t}\n\n\t\treturn [null, undefined].includes(param)\n\t\t\t ? this.messages[key]\n\t\t\t : this.messages[key].replace(\"[PARAM]\", param);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after another given date.\n\t *\n\t **/\n\tisAfter(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after or equal to another given date.\n\t *\n\t **/\n\tisAfterOrEqual(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an array.\n\t *\n\t **/\n\tisArray(value)\n\t{\n\t\treturn Array.isArray(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before another given date.\n\t *\n\t **/\n\tisBefore(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before or equal to another given date.\n\t *\n\t **/\n\tisBeforeOrEqual(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a boolean.\n\t *\n\t **/\n\tisBoolean(value)\n\t{\n\t\treturn [true, false].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a date object.\n\t *\n\t **/\n\tisDate(value)\n\t{\n\t\treturn value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is different to another given value.\n\t *\n\t **/\n\tisDifferent(value, different)\n\t{\n\t\treturn value != different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value ends with another given value.\n\t *\n\t **/\n\tisEndingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.endsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid email address.\n\t *\n\t **/\n\tisEmail(value)\n\t{\n\t\treturn new RegExp('^\\\\S+@\\\\S+[\\\\.][0-9a-z]+$').test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is falsy.\n\t *\n\t **/\n\tisFalsy(value)\n\t{\n\t\treturn [0, '0', false, 'false'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is within the given array of options.\n\t *\n\t **/\n\tisIn(value, options)\n\t{\n\t\toptions = typeof options === 'string'\n\t\t\t\t? options.split(\",\")\n\t\t\t\t: options;\n\n\t\treturn options.includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an integer.\n\t *\n\t **/\n\tisInteger(value)\n\t{\n\t\treturn Number.isInteger(value) && parseInt(value).toString() === value.toString();\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a JSON string.\n\t *\n\t **/\n\tisJson(value)\n\t{\n\t\ttry {\n \treturn typeof JSON.parse(value) === 'object';\n\t\t} catch (e) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given maximum limit.\n\t *\n\t **/\n\tisMaximum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) <= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given minimum limit.\n\t *\n\t **/\n\tisMinimum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) >= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is not within the given array of options.\n\t *\n\t **/\n\tisNotIn(value, options)\n\t{\n\t\treturn ! this.isIn(value, options);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is numeric (an integer or a float).\n\t *\n\t **/\n\tisNumeric(value)\n\t{\n\t\treturn ! isNaN(parseFloat(value)) && isFinite(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is optional.\n\t *\n\t **/\n\tisOptional(value)\n\t{\n\t\treturn [null, undefined, ''].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value satisifies the given regular expression.\n\t *\n\t **/\n\tisRegexMatch(value, expression)\n\t{\n\t\treturn new RegExp(expression).test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is present.\n\t *\n\t **/\n\tisRequired(value)\n\t{\n\t\treturn ! this.isOptional(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is the same as another given value.\n\t *\n\t **/\n\tisSame(value, same)\n\t{\n\t\treturn value == same;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value starts with another given value.\n\t *\n\t **/\n\tisStartingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.startsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a string.\n\t *\n\t **/\n\tisString(value)\n\t{\n\t\treturn typeof value === 'string';\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is truthy.\n\t *\n\t **/\n\tisTruthy(value)\n\t{\n\t\treturn [1, '1', true, 'true'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid URL.\n\t *\n\t **/\n\tisUrl(value)\n\t{\n\t\treturn 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_]*)?$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid UUID.\n\t *\n\t **/\n\tisUuid(value)\n\t{\n\t\treturn 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}$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine whether the given value meets the given rules.\n\t *\n\t **/\n\tis(value, rules = [])\n\t{\n\t\t// Check if no rules were specified\n\t\tif (rules.length === 0) return true;\n\n\t\t// Check for an optional value\n\t\tif (rules[0] === 'optional' && this.isOptional(value)) return true;\n\n\t\t// Iterate through the rules\n\t\tfor (let index = 0; index < rules.length; index++) {\n\n\t\t\t// Ignore optional rules\n\t\t\tif (rules[index] === 'optional') continue;\n\n\t\t\t// Determine the method to use\n\t\t\tlet rule = rules[index].split(':')[0][0].toUpperCase()\n\t\t\t\t\t + rules[index].split(':')[0].slice(1);\n\n\t\t\t// Validate the value against the method\n\t\t\tlet result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]);\n\n\t\t\t// Check if the value failed validation\n\t\t\tif (! result) return rules[index];\n\n\t\t}\n\n\t\t// Otherwise, the value is valid\n\t\treturn true;\n\t}\n\n\n\n\t/**\n\t * Replace the default error messages with a new set.\n\t *\n\t **/\n\tsetErrorMessages(messages)\n\t{\n\t\tthis.messages = messages;\n\t}\n\n}\n\n\n\n/**\n * Create an instance of the library.\n *\n **/\nwindow.Iodine = new Iodine();"],"names":["Iodine","constructor","messages","this","_defaultMessages","_dateCompare","first","second","type","equals","isDate","isInteger","getTime","after","afterOrEqual","array","before","beforeOrEqual","boolean","date","different","endingWith","email","falsy","in","integer","json","maximum","minimum","notIn","numeric","optional","regexMatch","required","same","startingWith","string","truthy","url","uuid","addRule","name","closure","prototype","toUpperCase","slice","getErrorMessage","rule","arg","undefined","key","split","param","includes","Date","parseInt","toLocaleTimeString","year","month","day","hour","minute","replace","isAfter","value","isAfterOrEqual","isArray","Array","isBefore","isBeforeOrEqual","isBoolean","Object","toString","call","isNaN","isDifferent","isEndingWith","sub","isString","endsWith","isEmail","RegExp","test","String","toLowerCase","isFalsy","isIn","options","Number","isJson","JSON","parse","e","isMaximum","limit","length","parseFloat","isMinimum","isNotIn","isNumeric","isFinite","isOptional","isRegexMatch","expression","isRequired","isSame","isStartingWith","startsWith","isTruthy","isUrl","isUuid","is","rules","let","index","apply","setErrorMessages","window"],"mappings":"AASe,IAAMA,EAOpBC,gBAEMC,SAAWC,KAAKC,gCAStBC,sBAAaC,EAAOC,EAAQC,EAAMC,0BAAS,KAEpCN,KAAKO,OAAOJ,OAEZH,KAAKO,OAAOH,KAAaJ,KAAKQ,UAAUJ,MAE9CA,EAA2B,iBAAXA,EAAsBA,EAASA,EAAOK,UAEzC,SAATJ,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,EACZ,SAATD,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,SAAeH,EAAMM,UAAYL,EAFlBD,EAAMM,UAAYL,gBAW3DH,kCAEQ,CACNS,MAAiB,oCACjBC,aAAiB,gDACjBC,MAAiB,yBACjBC,OAAiB,qCACjBC,cAAiB,iDACjBC,QAAiB,8BACjBC,KAAiB,uBACjBC,UAAiB,uCACjBC,WAAiB,gCACjBC,MAAiB,sCACjBC,MAAiB,yDACjBC,GAAc,sDACdC,QAAiB,2BACjBC,KAAiB,8CACjBC,QAAiB,uEACjBC,QAAiB,oEACjBC,MAAiB,0DACjBC,QAAiB,wBACjBC,SAAiB,oBACjBC,WAAiB,sDACjBC,SAAiB,wBACjBC,K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,IAAqB7G,KAAKkG,WAAWrC,GAAQ,OAAO,MAGzDiD,IAAIC,EAAQ,EAAGA,EAAQF,EAAMjB,OAAQmB,OAGpB,aAAjBF,EAAME,KAOG/G,WAJF6G,EAAME,GAAO/D,MAAM,KAAK,GAAG,GAAGP,cACpCoE,EAAME,GAAO/D,MAAM,KAAK,GAAGN,MAAM,KAGPsE,MAAMhH,KAAM,CAAC6D,EAAOgD,EAAME,GAAO/D,MAAM,KAAK,KAG7D,OAAO6D,EAAME,UAKrB,eASRE,0BAAiBlH,QAEXA,SAAWA,GAWlBmH,OAAOrH,OAAS,IAAIA"} \ No newline at end of file diff --git a/dist/iodine.min.umd.js b/dist/iodine.min.umd.js index 1c0c1be..43c9326 100644 --- a/dist/iodine.min.umd.js +++ b/dist/iodine.min.umd.js @@ -1,2 +1,2 @@ -!function(t,e){"object"==typeof exports&&"undefined"!=typeof module?module.exports=e():"function"==typeof define&&define.amd?define(e):t.iodine=e()}(this,function(){var t=function(){this.messages=this._defaultMessages()};return t.prototype._dateCompare=function(t,e,r,i){return void 0===i&&(i=!1),!!this.isDate(t)&&!(!this.isDate(e)&&!this.isInteger(e))&&(e="number"==typeof e?e:e.getTime(),"less"===r&&i?t.getTime()<=e:"less"!==r||i?"more"===r&&i?t.getTime()>=e:"more"!==r||i?void 0:t.getTime()>e:t.getTime()=e},t.prototype.isNotIn=function(t,e){return!this.isIn(t,e)},t.prototype.isNumeric=function(t){return!isNaN(parseFloat(t))&&isFinite(t)},t.prototype.isOptional=function(t){return[null,void 0,""].includes(t)},t.prototype.isRegexMatch=function(t,e){return new RegExp(e).test(String(t).toLowerCase())},t.prototype.isRequired=function(t){return!this.isOptional(t)},t.prototype.isSame=function(t,e){return t===e},t.prototype.isStartingWith=function(t,e){return this.isString(t)&&t.startsWith(e)},t.prototype.isString=function(t){return"string"==typeof t},t.prototype.isTruthy=function(t){return[1,"1",!0,"true"].includes(t)},t.prototype.isUrl=function(t){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(t).toLowerCase())},t.prototype.isUuid=function(t){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(t).toLowerCase())},t.prototype.is=function(t,e){if(void 0===e&&(e=[]),0===e.length)return!0;if("optional"===e[0]&&this.isOptional(t))return!0;for(var r=0;r=e:"more"!==r||i?void 0:t.getTime()>e:t.getTime()=e},t.prototype.isNotIn=function(t,e){return!this.isIn(t,e)},t.prototype.isNumeric=function(t){return!isNaN(parseFloat(t))&&isFinite(t)},t.prototype.isOptional=function(t){return[null,void 0,""].includes(t)},t.prototype.isRegexMatch=function(t,e){return new RegExp(e).test(String(t).toLowerCase())},t.prototype.isRequired=function(t){return!this.isOptional(t)},t.prototype.isSame=function(t,e){return t==e},t.prototype.isStartingWith=function(t,e){return this.isString(t)&&t.startsWith(e)},t.prototype.isString=function(t){return"string"==typeof t},t.prototype.isTruthy=function(t){return[1,"1",!0,"true"].includes(t)},t.prototype.isUrl=function(t){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(t).toLowerCase())},t.prototype.isUuid=function(t){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(t).toLowerCase())},t.prototype.is=function(t,e){if(void 0===e&&(e=[]),0===e.length)return!0;if("optional"===e[0]&&this.isOptional(t))return!0;for(var r=0;r= second;\n\t\tif (type === 'more' && ! equals) return first.getTime() > second;\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_defaultMessages()\n\t{\n\t\treturn {\n\t\t\tafter : `The date must be after: '[PARAM]'`,\n\t\t\tafterOrEqual : `The date must be after or equal to: '[PARAM]'`,\n\t\t\tarray : `Field must be an array`,\n\t\t\tbefore : `The date must be before: '[PARAM]'`,\n\t\t\tbeforeOrEqual : `The date must be before or equal to: '[PARAM]'`,\n\t\t\tboolean : `Field must be true or false`,\n\t\t\tdate : `Field must be a date`,\n\t\t\tdifferent : `Field must be different to '[PARAM]'`,\n\t\t\tendingWith : `Field must end with '[PARAM]'`,\n\t\t\temail : `Field must be a valid email address`,\n\t\t\tfalsy : `Field must be a falsy value (false, 'false', 0 or '0')`,\n\t\t\tin \t\t : `Field must be one of the following options: [PARAM]`,\n\t\t\tinteger : `Field must be an integer`,\n\t\t\tjson : `Field must be a parsable JSON object string`,\n\t\t\tmaximum : `Field must not be greater than '[PARAM]' in size or character length`,\n\t\t\tminimum : `Field must not be less than '[PARAM]' in size or character length`,\n\t\t\tnotIn : `Field must not be one of the following options: [PARAM]`,\n\t\t\tnumeric : `Field must be numeric`,\n\t\t\toptional : `Field is optional`,\n\t\t\tregexMatch : `Field must satisify the regular expression: [PARAM]`,\n\t\t\trequired : `Field must be present`,\n\t\t\tsame : `Field must be '[PARAM]'`,\n\t\t\tstartingWith : `Field must start with '[PARAM]'`,\n\t\t\tstring : `Field must be a string`,\n\t\t\ttruthy : `Field must be a truthy value (true, 'true', 1 or '1')`,\n\t\t\turl : `Field must be a valid url`,\n\t\t\tuuid : `Field must be a valid UUID`,\n\t\t};\n\t}\n\n\n\n\t/**\n\t * Retrieve an error message for the given rule.\n\t *\n\t **/\n\tgetErrorMessage(rule, arg = null)\n\t{\n\t\tlet key = rule.split(':')[0];\n\t\tlet param = arg || rule.split(':')[1];\n\n\t\tif (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {\n\t\t\tparam = new Date(parseInt(param)).toLocaleTimeString(undefined, {\n\t\t\t\tyear: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric'\n\t\t\t});\n\t\t}\n\n\t\treturn param === undefined\n\t\t\t ? this.messages[key]\n\t\t\t : this.messages[key].replace(\"[PARAM]\", param);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after another given date.\n\t *\n\t **/\n\tisAfter(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after or equal to another given date.\n\t *\n\t **/\n\tisAfterOrEqual(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an array.\n\t *\n\t **/\n\tisArray(value)\n\t{\n\t\treturn Array.isArray(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before another given date.\n\t *\n\t **/\n\tisBefore(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before or equal to another given date.\n\t *\n\t **/\n\tisBeforeOrEqual(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a boolean.\n\t *\n\t **/\n\tisBoolean(value)\n\t{\n\t\treturn [true, false].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a date object.\n\t *\n\t **/\n\tisDate(value)\n\t{\n\t\treturn value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is different to another given value.\n\t *\n\t **/\n\tisDifferent(value, different)\n\t{\n\t\treturn value !== different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value ends with another given value.\n\t *\n\t **/\n\tisEndingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.endsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid email address.\n\t *\n\t **/\n\tisEmail(value)\n\t{\n\t\treturn new RegExp('^\\\\S+@\\\\S+[\\\\.][0-9a-z]+$').test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is falsy.\n\t *\n\t **/\n\tisFalsy(value)\n\t{\n\t\treturn [0, '0', false, 'false'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is within the given array of options.\n\t *\n\t **/\n\tisIn(value, options)\n\t{\n\t\toptions = typeof options === 'string'\n\t\t\t\t? options.split(\",\")\n\t\t\t\t: options;\n\n\t\treturn options.includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an integer.\n\t *\n\t **/\n\tisInteger(value)\n\t{\n\t\treturn Number.isInteger(value) && parseInt(value).toString() === value.toString();\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a JSON string.\n\t *\n\t **/\n\tisJson(value)\n\t{\n\t\ttry {\n \treturn typeof JSON.parse(value) === 'object';\n\t\t} catch (e) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given maximum limit.\n\t *\n\t **/\n\tisMaximum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) <= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given minimum limit.\n\t *\n\t **/\n\tisMinimum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) >= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is not within the given array of options.\n\t *\n\t **/\n\tisNotIn(value, options)\n\t{\n\t\treturn ! this.isIn(value, options);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is numeric (an integer or a float).\n\t *\n\t **/\n\tisNumeric(value)\n\t{\n\t\treturn ! isNaN(parseFloat(value)) && isFinite(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is optional.\n\t *\n\t **/\n\tisOptional(value)\n\t{\n\t\treturn [null, undefined, ''].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value satisifies the given regular expression.\n\t *\n\t **/\n\tisRegexMatch(value, expression)\n\t{\n\t\treturn new RegExp(expression).test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is present.\n\t *\n\t **/\n\tisRequired(value)\n\t{\n\t\treturn ! this.isOptional(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is the same as another given value.\n\t *\n\t **/\n\tisSame(value, different)\n\t{\n\t\treturn value === different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value starts with another given value.\n\t *\n\t **/\n\tisStartingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.startsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a string.\n\t *\n\t **/\n\tisString(value)\n\t{\n\t\treturn typeof value === 'string';\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is truthy.\n\t *\n\t **/\n\tisTruthy(value)\n\t{\n\t\treturn [1, '1', true, 'true'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid URL.\n\t *\n\t **/\n\tisUrl(value)\n\t{\n\t\treturn 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_]*)?$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid UUID.\n\t *\n\t **/\n\tisUuid(value)\n\t{\n\t\treturn 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}$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine whether the given value meets the given rules.\n\t *\n\t **/\n\tis(value, rules = [])\n\t{\n\t\t// Check if no rules were specified\n\t\tif (rules.length === 0) return true;\n\n\t\t// Check for an optional value\n\t\tif (rules[0] === 'optional' && this.isOptional(value)) return true;\n\n\t\t// Iterate through the rules\n\t\tfor (let index = 0; index < rules.length; index++) {\n\n\t\t\t// Ignore optional rules\n\t\t\tif (rules[index] === 'optional') continue;\n\n\t\t\t// Determine the method to use\n\t\t\tlet rule = rules[index].split(':')[0][0].toUpperCase()\n\t\t\t\t\t + rules[index].split(':')[0].slice(1);\n\n\t\t\t// Validate the value against the method\n\t\t\tlet result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]);\n\n\t\t\t// Check if the value failed validation\n\t\t\tif (! result) return rules[index];\n\n\t\t}\n\n\t\t// Otherwise, the value is valid\n\t\treturn true;\n\t}\n\n\n\n\t/**\n\t * Replace the default error messages with a new set.\n\t *\n\t **/\n\tsetErrorMessages(messages)\n\t{\n\t\tthis.messages = messages;\n\t}\n\n}\n\n\n\n/**\n * Create an instance of the library.\n *\n **/\nwindow.Iodine = new Iodine();"],"names":["Iodine","constructor","messages","this","_defaultMessages","_dateCompare","first","second","type","equals","isDate","isInteger","getTime","after","afterOrEqual","array","before","beforeOrEqual","boolean","date","different","endingWith","email","falsy","in","integer","json","maximum","minimum","notIn","numeric","optional","regexMatch","required","same","startingWith","string","truthy","url","uuid","getErrorMessage","rule","arg","key","split","param","includes","Date","parseInt","toLocaleTimeString","undefined","year","month","day","hour","minute","replace","isAfter","value","isAfterOrEqual","isArray","Array","isBefore","isBeforeOrEqual","isBoolean","Object","prototype","toString","call","isNaN","isDifferent","isEndingWith","sub","isString","endsWith","isEmail","RegExp","test","String","toLowerCase","isFalsy","isIn","options","Number","isJson","JSON","parse","e","isMaximum","limit","length","parseFloat","isMinimum","isNotIn","isNumeric","isFinite","isOptional","isRegexMatch","expression","isRequired","isSame","isStartingWith","startsWith","isTruthy","isUrl","isUuid","is","rules","let","index","toUpperCase","slice","apply","setErrorMessages","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ASrC,gBAS/CsC,oBAAWtC,SAEH,CAAC,UAAMR,EAAW,IAAIJ,SAASY,gBASvCuC,sBAAavC,EAAOwC,UAEZ,IAAItB,OAAOsB,GAAYrB,KAAKC,OAAOpB,GAAOqB,4BASlDoB,oBAAWzC,UAEDvD,KAAK6F,WAAWtC,gBAS1B0C,gBAAO1C,EAAOtC,UAENsC,IAAUtC,eASlBiF,wBAAe3C,EAAOc,UAEdrE,KAAKsE,SAASf,IAAUA,EAAM4C,WAAW9B,gBASjDC,kBAASf,SAEgB,iBAAVA,eASf6C,kBAAS7C,SAED,CAAC,EAAG,KAAK,EAAM,QAAQZ,SAASY,gBASxC8C,eAAM9C,UAEE,IAAIkB,OAAO,yKACfC,KAAKC,OAAOpB,GAAOqB,4BASvB0B,gBAAO/C,UAEC,IAAIkB,OAAO,6EACfC,KAAKC,OAAOpB,GAAOqB,4BASvB2B,YAAGhD,EAAOiD,qBAAQ,IAGI,IAAjBA,EAAMjB,OAAc,OAAO,KAGd,aAAbiB,EAAM,IAAqBxG,KAAK6F,WAAWtC,GAAQ,OAAO,MAGzDkD,IAAIC,EAAQ,EAAGA,EAAQF,EAAMjB,OAAQmB,OAGpB,aAAjBF,EAAME,KAOG1G,WAJFwG,EAAME,GAAOjE,MAAM,KAAK,GAAG,GAAGkE,cACpCH,EAAME,GAAOjE,MAAM,KAAK,GAAGmE,MAAM,KAGPC,MAAM7G,KAAM,CAACuD,EAAOiD,EAAME,GAAOjE,MAAM,KAAK,KAG7D,OAAO+D,EAAME,UAKrB,eASRI,0BAAiB/G,QAEXA,SAAWA,GAWlBgH,OAAOlH,OAAS,IAAIA"} \ No newline at end of file +{"version":3,"file":"iodine.min.umd.js","sources":["../src/iodine.js"],"sourcesContent":["/*\n|--------------------------------------------------------------------------\n| Iodine - JavaScript Library\n|--------------------------------------------------------------------------\n|\n| This library contains a collection of useful validation rules that can\n| be used to quickly verify whether items meet certain conditions.\n|\n*/\nexport default class Iodine\n{\n\n\t/**\n\t * Constructor.\n\t *\n\t **/\n\tconstructor()\n\t{\n\t\tthis.messages = this._defaultMessages();\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_dateCompare(first, second, type, equals = false)\n\t{\n\t\tif (! this.isDate(first)) return false;\n\n\t\tif (! this.isDate(second) && ! this.isInteger(second)) return false;\n\n\t\tsecond = typeof second === 'number' ? second : second.getTime();\n\n\t\tif (type === 'less' && equals) return first.getTime() <= second;\n\t\tif (type === 'less' && ! equals) return first.getTime() < second;\n\t\tif (type === 'more' && equals) return first.getTime() >= second;\n\t\tif (type === 'more' && ! equals) return first.getTime() > second;\n\t}\n\n\n\n\t/**\n\t * @internal.\n\t *\n\t **/\n\t_defaultMessages()\n\t{\n\t\treturn {\n\t\t\tafter : `The date must be after: '[PARAM]'`,\n\t\t\tafterOrEqual : `The date must be after or equal to: '[PARAM]'`,\n\t\t\tarray : `Value must be an array`,\n\t\t\tbefore : `The date must be before: '[PARAM]'`,\n\t\t\tbeforeOrEqual : `The date must be before or equal to: '[PARAM]'`,\n\t\t\tboolean : `Value must be true or false`,\n\t\t\tdate : `Value must be a date`,\n\t\t\tdifferent : `Value must be different to '[PARAM]'`,\n\t\t\tendingWith : `Value must end with '[PARAM]'`,\n\t\t\temail : `Value must be a valid email address`,\n\t\t\tfalsy : `Value must be a falsy value (false, 'false', 0 or '0')`,\n\t\t\tin \t\t : `Value must be one of the following options: [PARAM]`,\n\t\t\tinteger : `Value must be an integer`,\n\t\t\tjson : `Value must be a parsable JSON object string`,\n\t\t\tmaximum : `Value must not be greater than '[PARAM]' in size or character length`,\n\t\t\tminimum : `Value must not be less than '[PARAM]' in size or character length`,\n\t\t\tnotIn : `Value must not be one of the following options: [PARAM]`,\n\t\t\tnumeric : `Value must be numeric`,\n\t\t\toptional : `Value is optional`,\n\t\t\tregexMatch : `Value must satisify the regular expression: [PARAM]`,\n\t\t\trequired : `Value must be present`,\n\t\t\tsame : `Value must be '[PARAM]'`,\n\t\t\tstartingWith : `Value must start with '[PARAM]'`,\n\t\t\tstring : `Value must be a string`,\n\t\t\ttruthy : `Value must be a truthy value (true, 'true', 1 or '1')`,\n\t\t\turl : `Value must be a valid url`,\n\t\t\tuuid : `Value must be a valid UUID`,\n\t\t};\n\t}\n\n\n\n\t/**\n\t * Attach a custom validation rule to the library.\n\t *\n\t **/\n\taddRule(name, closure)\n\t{\n\t\tIodine.prototype[`is${name[0].toUpperCase()}${name.slice(1)}`] = closure;\n\t}\n\n\n\n\t/**\n\t * Retrieve an error message for the given rule.\n\t *\n\t **/\n\tgetErrorMessage(rule, arg = undefined)\n\t{\n\t\tlet key = rule.split(':')[0];\n\t\tlet param = arg || rule.split(':')[1];\n\n\t\tif (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) {\n\t\t\tparam = new Date(parseInt(param)).toLocaleTimeString(undefined, {\n\t\t\t\tyear: 'numeric', month: 'short', day: 'numeric', hour: '2-digit', minute: 'numeric'\n\t\t\t});\n\t\t}\n\n\t\treturn [null, undefined].includes(param)\n\t\t\t ? this.messages[key]\n\t\t\t : this.messages[key].replace(\"[PARAM]\", param);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after another given date.\n\t *\n\t **/\n\tisAfter(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is after or equal to another given date.\n\t *\n\t **/\n\tisAfterOrEqual(value, after)\n\t{\n\t\treturn this._dateCompare(value, after, 'more', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an array.\n\t *\n\t **/\n\tisArray(value)\n\t{\n\t\treturn Array.isArray(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before another given date.\n\t *\n\t **/\n\tisBefore(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', false);\n\t}\n\n\n\n\t/**\n\t * Determine if the given date is before or equal to another given date.\n\t *\n\t **/\n\tisBeforeOrEqual(value, before)\n\t{\n\t\treturn this._dateCompare(value, before, 'less', true);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a boolean.\n\t *\n\t **/\n\tisBoolean(value)\n\t{\n\t\treturn [true, false].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a date object.\n\t *\n\t **/\n\tisDate(value)\n\t{\n\t\treturn value && Object.prototype.toString.call(value) === '[object Date]' && ! isNaN(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is different to another given value.\n\t *\n\t **/\n\tisDifferent(value, different)\n\t{\n\t\treturn value != different;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value ends with another given value.\n\t *\n\t **/\n\tisEndingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.endsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid email address.\n\t *\n\t **/\n\tisEmail(value)\n\t{\n\t\treturn new RegExp('^\\\\S+@\\\\S+[\\\\.][0-9a-z]+$').test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is falsy.\n\t *\n\t **/\n\tisFalsy(value)\n\t{\n\t\treturn [0, '0', false, 'false'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is within the given array of options.\n\t *\n\t **/\n\tisIn(value, options)\n\t{\n\t\toptions = typeof options === 'string'\n\t\t\t\t? options.split(\",\")\n\t\t\t\t: options;\n\n\t\treturn options.includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is an integer.\n\t *\n\t **/\n\tisInteger(value)\n\t{\n\t\treturn Number.isInteger(value) && parseInt(value).toString() === value.toString();\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a JSON string.\n\t *\n\t **/\n\tisJson(value)\n\t{\n\t\ttry {\n \treturn typeof JSON.parse(value) === 'object';\n\t\t} catch (e) {\n\t\t\treturn false;\n\t\t}\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given maximum limit.\n\t *\n\t **/\n\tisMaximum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) <= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value meets the given minimum limit.\n\t *\n\t **/\n\tisMinimum(value, limit)\n\t{\n\t\tvalue = typeof value === 'string' ? value.length : value;\n\n\t\treturn parseFloat(value) >= limit;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is not within the given array of options.\n\t *\n\t **/\n\tisNotIn(value, options)\n\t{\n\t\treturn ! this.isIn(value, options);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is numeric (an integer or a float).\n\t *\n\t **/\n\tisNumeric(value)\n\t{\n\t\treturn ! isNaN(parseFloat(value)) && isFinite(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is optional.\n\t *\n\t **/\n\tisOptional(value)\n\t{\n\t\treturn [null, undefined, ''].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value satisifies the given regular expression.\n\t *\n\t **/\n\tisRegexMatch(value, expression)\n\t{\n\t\treturn new RegExp(expression).test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is present.\n\t *\n\t **/\n\tisRequired(value)\n\t{\n\t\treturn ! this.isOptional(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is the same as another given value.\n\t *\n\t **/\n\tisSame(value, same)\n\t{\n\t\treturn value == same;\n\t}\n\n\n\n\t/**\n\t * Determine if the given value starts with another given value.\n\t *\n\t **/\n\tisStartingWith(value, sub)\n\t{\n\t\treturn this.isString(value) && value.startsWith(sub);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a string.\n\t *\n\t **/\n\tisString(value)\n\t{\n\t\treturn typeof value === 'string';\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is truthy.\n\t *\n\t **/\n\tisTruthy(value)\n\t{\n\t\treturn [1, '1', true, 'true'].includes(value);\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid URL.\n\t *\n\t **/\n\tisUrl(value)\n\t{\n\t\treturn 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_]*)?$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine if the given value is a valid UUID.\n\t *\n\t **/\n\tisUuid(value)\n\t{\n\t\treturn 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}$')\n\t\t\t .test(String(value).toLowerCase());\n\t}\n\n\n\n\t/**\n\t * Determine whether the given value meets the given rules.\n\t *\n\t **/\n\tis(value, rules = [])\n\t{\n\t\t// Check if no rules were specified\n\t\tif (rules.length === 0) return true;\n\n\t\t// Check for an optional value\n\t\tif (rules[0] === 'optional' && this.isOptional(value)) return true;\n\n\t\t// Iterate through the rules\n\t\tfor (let index = 0; index < rules.length; index++) {\n\n\t\t\t// Ignore optional rules\n\t\t\tif (rules[index] === 'optional') continue;\n\n\t\t\t// Determine the method to use\n\t\t\tlet rule = rules[index].split(':')[0][0].toUpperCase()\n\t\t\t\t\t + rules[index].split(':')[0].slice(1);\n\n\t\t\t// Validate the value against the method\n\t\t\tlet result = this[`is${rule}`].apply(this, [value, rules[index].split(':')[1]]);\n\n\t\t\t// Check if the value failed validation\n\t\t\tif (! result) return rules[index];\n\n\t\t}\n\n\t\t// Otherwise, the value is valid\n\t\treturn true;\n\t}\n\n\n\n\t/**\n\t * Replace the default error messages with a new set.\n\t *\n\t **/\n\tsetErrorMessages(messages)\n\t{\n\t\tthis.messages = messages;\n\t}\n\n}\n\n\n\n/**\n * Create an instance of the library.\n *\n **/\nwindow.Iodine = new Iodine();"],"names":["Iodine","constructor","messages","this","_defaultMessages","_dateCompare","first","second","type","equals","isDate","isInteger","getTime","after","afterOrEqual","array","before","beforeOrEqual","boolean","date","different","endingWith","email","falsy","in","integer","json","maximum","minimum","notIn","numeric","optional","regexMatch","required","same","startingWith","string","truthy","url","uuid","addRule","name","closure","prototype","toUpperCase","slice","getErrorMessage","rule","arg","undefined","key","split","param","includes","Date","parseInt","toLocaleTimeString","year","month","day","hour","minute","replace","isAfter","value","isAfterOrEqual","isArray","Array","isBefore","isBeforeOrEqual","isBoolean","Object","toString","call","isNaN","isDifferent","isEndingWith","sub","isString","endsWith","isEmail","RegExp","test","String","toLowerCase","isFalsy","isIn","options","Number","isJson","JSON","parse","e","isMaximum","limit","length","parseFloat","isMinimum","isNotIn","isNumeric","isFinite","isOptional","isRegexMatch","expression","isRequired","isSame","isStartingWith","startsWith","isTruthy","isUrl","isUuid","is","rules","let","index","apply","setErrorMessages","window"],"mappings":"qKASe,IAAMA,EAOpBC,gBAEMC,SAAWC,KAAKC,uCAStBC,sBAAaC,EAAOC,EAAQC,EAAMC,0BAAS,KAEpCN,KAAKO,OAAOJ,OAEZH,KAAKO,OAAOH,KAAaJ,KAAKQ,UAAUJ,MAE9CA,EAA2B,iBAAXA,EAAsBA,EAASA,EAAOK,UAEzC,SAATJ,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,EACZ,SAATD,GAAmBC,EAAeH,EAAMM,WAAaL,EAC5C,SAATC,GAAqBC,SAAeH,EAAMM,UAAYL,EAFlBD,EAAMM,UAAYL,gBAW3DH,kCAEQ,CACNS,MAAiB,oCACjBC,aAAiB,gDACjBC,MAAiB,yBACjBC,OAAiB,qCACjBC,cAAiB,iDACjBC,QAAiB,8BACjBC,KAAiB,uBACjBC,UAAiB,uCACjBC,WAAiB,gCACjBC,MAAiB,sCACjBC,MAAiB,yDACjBC,GAAc,sDACdC,QAAiB,2BACjBC,KAAiB,8CACjBC,QAAiB,uEACjBC,QAAiB,oEACjBC,MAAiB,0DACjBC,QAAiB,wBACjBC,SAAiB,oBACjBC,WAAiB,sDACjBC,SAAiB,wBACjB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AAM,IAAqB7G,KAAKkG,WAAWrC,GAAQ,OAAO,MAGzDiD,IAAIC,EAAQ,EAAGA,EAAQF,EAAMjB,OAAQmB,OAGpB,aAAjBF,EAAME,KAOG/G,WAJF6G,EAAME,GAAO/D,MAAM,KAAK,GAAG,GAAGP,cACpCoE,EAAME,GAAO/D,MAAM,KAAK,GAAGN,MAAM,KAGPsE,MAAMhH,KAAM,CAAC6D,EAAOgD,EAAME,GAAO/D,MAAM,KAAK,KAG7D,OAAO6D,EAAME,UAKrB,eASRE,0BAAiBlH,QAEXA,SAAWA,GAWlBmH,OAAOrH,OAAS,IAAIA"} \ No newline at end of file diff --git a/package.json b/package.json index 3a4c085..594b7f6 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kingshott/iodine", - "version": "2.0.3", + "version": "3.0.0", "description": "A micro client-side validation library", "repository": { "type": "git", diff --git a/resources/version.svg b/resources/version.svg index 405867b..f473e34 100644 --- a/resources/version.svg +++ b/resources/version.svg @@ -14,7 +14,7 @@ stable stable - v2.0.3 - v2.0.3 + v3.0.0 + v3.0.0 \ No newline at end of file diff --git a/src/iodine.js b/src/iodine.js index c635e75..12c45e0 100644 --- a/src/iodine.js +++ b/src/iodine.js @@ -50,36 +50,47 @@ export default class Iodine return { after : `The date must be after: '[PARAM]'`, afterOrEqual : `The date must be after or equal to: '[PARAM]'`, - array : `Field must be an array`, + array : `Value must be an array`, before : `The date must be before: '[PARAM]'`, beforeOrEqual : `The date must be before or equal to: '[PARAM]'`, - boolean : `Field must be true or false`, - date : `Field must be a date`, - different : `Field must be different to '[PARAM]'`, - endingWith : `Field must end with '[PARAM]'`, - email : `Field must be a valid email address`, - falsy : `Field must be a falsy value (false, 'false', 0 or '0')`, - in : `Field must be one of the following options: [PARAM]`, - integer : `Field must be an integer`, - json : `Field must be a parsable JSON object string`, - maximum : `Field must not be greater than '[PARAM]' in size or character length`, - minimum : `Field must not be less than '[PARAM]' in size or character length`, - notIn : `Field must not be one of the following options: [PARAM]`, - numeric : `Field must be numeric`, - optional : `Field is optional`, - regexMatch : `Field must satisify the regular expression: [PARAM]`, - required : `Field must be present`, - same : `Field must be '[PARAM]'`, - startingWith : `Field must start with '[PARAM]'`, - string : `Field must be a string`, - truthy : `Field must be a truthy value (true, 'true', 1 or '1')`, - url : `Field must be a valid url`, - uuid : `Field must be a valid UUID`, + 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; + } + + + /** * Retrieve an error message for the given rule. * @@ -185,7 +196,7 @@ export default class Iodine **/ isDifferent(value, different) { - return value !== different; + return value != different; } @@ -349,9 +360,9 @@ export default class Iodine * Determine if the given value is the same as another given value. * **/ - isSame(value, different) + isSame(value, same) { - return value === different; + return value == same; } diff --git a/tests/test.js b/tests/test.js index a0c73b9..4fba8ef 100644 --- a/tests/test.js +++ b/tests/test.js @@ -399,10 +399,10 @@ test('it validates values against multiple rules', () => { **/ test('it retrieves formatted error messages for rules', () => { let time = Date.UTC(2020, 4, 2, 3, 17, 0); - expect(Iodine.getErrorMessage('array')).toBe('Field must be an array'); - expect(Iodine.getErrorMessage('endingWith')).toBe(`Field must end with '[PARAM]'`); - expect(Iodine.getErrorMessage('endingWith:world')).toBe(`Field must end with 'world'`); - expect(Iodine.getErrorMessage('endingWith', 'world')).toBe(`Field must end with 'world'`); + expect(Iodine.getErrorMessage('array')).toBe('Value must be an array'); + expect(Iodine.getErrorMessage('endingWith')).toBe(`Value must end with '[PARAM]'`); + expect(Iodine.getErrorMessage('endingWith:world')).toBe(`Value must end with 'world'`); + expect(Iodine.getErrorMessage('endingWith', 'world')).toBe(`Value must end with 'world'`); expect(Iodine.getErrorMessage(`after:${time}`)).toBe(`The date must be after: '2 May 2020, 04:17'`); expect(Iodine.getErrorMessage(`after`, time)).toBe(`The date must be after: '2 May 2020, 04:17'`); }); @@ -414,8 +414,45 @@ test('it retrieves formatted error messages for rules', () => { * **/ 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'); +}); + + + +/** + * Confirm that the 'addRule' method works correctly for simple rules. + * + **/ +test('it can add simple custom rules', () => { + 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.getErrorMessage('lowerCase')).toBe('Value must be in lower case'); +}); + + + +/** + * Confirm that the 'addRule' method works correctly for advanced rules. + * + **/ +test('it can add advanced custom rules', () => { + 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.getErrorMessage('equals:2')).toBe(`Value must be equal to '2'`); + expect(Iodine.getErrorMessage('equals', 2)).toBe(`Value must be equal to '2'`); }); \ No newline at end of file