diff --git a/.github/workflows/style.yml b/.github/workflows/style.yml deleted file mode 100644 index 1f7fda0..0000000 --- a/.github/workflows/style.yml +++ /dev/null @@ -1,32 +0,0 @@ -name: styling - -on: [push] - -jobs: - style: - runs-on: ubuntu-latest - - steps: - - name: Checkout code - uses: actions/checkout@v2 - - - name: Install dependencies - run: npm install - env: - CI: true - - - name: Run prettier - run: npm run prettier - - - name: Extract branch name - shell: bash - run: echo "##[set-output name=branch;]$(echo ${GITHUB_REF#refs/heads/})" - id: extract_branch - - - name: Commit changes - uses: stefanzweifel/git-auto-commit-action@v2.3.0 - with: - commit_message: Fix styling - branch: ${{ steps.extract_branch.outputs.branch }} - env: - GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} \ No newline at end of file diff --git a/src/iodine.js b/src/iodine.js index dfac684..f8705b6 100644 --- a/src/iodine.js +++ b/src/iodine.js @@ -17,7 +17,7 @@ export class Iodine { this.messages = this._defaultMessages(); - this.defaultFieldName = "Value"; + this.defaultFieldName = 'Value'; } /** @@ -29,12 +29,12 @@ export class Iodine { 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; } /** @@ -43,35 +43,35 @@ export class Iodine { */ _defaultMessages() { return { - after: `The date must be after: '[PARAM]'`, - afterOrEqual: `The date must be after or equal to: '[PARAM]'`, - array: `[FIELD] 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`, - max: `[FIELD] must be less than or equal to [PARAM]`, - min: `[FIELD] must be greater than or equal to [PARAM]`, - maxLength: `[FIELD] must not be greater than '[PARAM]' in character length`, - minLength: `[FIELD] must not be less than '[PARAM]' 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`, + after : `The date must be after: '[PARAM]'`, + afterOrEqual : `The date must be after or equal to: '[PARAM]'`, + array : `[FIELD] 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`, + max : `[FIELD] must be less than or equal to [PARAM]`, + min : `[FIELD] must be greater than or equal to [PARAM]`, + maxLength : `[FIELD] must not be greater than '[PARAM]' in character length`, + minLength : `[FIELD] must not be less than '[PARAM]' 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`, }; } @@ -82,15 +82,10 @@ export class Iodine { _prepare(value, rules = []) { if (!rules.length) return []; - if (rules[0] === "optional" && this.isOptional(value)) return []; + if (rules[0] === 'optional' && this.isOptional(value)) return []; - return rules - .filter((rule) => rule !== "optional") - .map((rule) => [ - rule, - this._titleCase(rule.split(":").shift()), - rule.split(":").slice(1), - ]); + return rules.filter(rule => rule !== 'optional') + .map(rule => [rule, this._titleCase(rule.split(':').shift()), rule.split(':').slice(1)]); } /** @@ -114,13 +109,8 @@ export class Iodine { * */ async asyncIs(value, rules = []) { - for (let index in (rules = this._prepare(value, rules))) { - if ( - !(await this[`is${rules[index][1]}`].apply(this, [ - value, - rules[index][2].join(":"), - ])) - ) { + for (let index in rules = this._prepare(value, rules)) { + if (!await this[`is${rules[index][1]}`].apply(this, [value, rules[index][2].join(':')])) { return await rules[index][0]; } } @@ -133,7 +123,7 @@ export class Iodine { * */ async asyncIsValid(value, rules = []) { - return (await this.asyncIs(value, rules)) === true; + return await this.asyncIs(value, rules) === true; } /** @@ -141,33 +131,32 @@ export class Iodine { * */ getErrorMessage(rule, args = undefined) { - let { param, field } = - typeof args === "object" ? args : { param: args, field: undefined }; + let { param, field } = typeof args === 'object' ? args : { param: args, field: undefined }; - const chunks = rule.split(":"); + const chunks = rule.split(':'); let key = chunks.shift(); - param = param || chunks.join(":"); + param = param || chunks.join(':'); - if (["after", "afterOrEqual", "before", "beforeOrEqual"].includes(key)) { + if (['after', 'afterOrEqual', 'before', 'beforeOrEqual'].includes(key)) { param = new Date(parseInt(param)).toLocaleTimeString(this.locale, { - year: "numeric", - month: "short", - day: "numeric", - hour: "2-digit", - minute: "numeric", - hour12: false, + year : 'numeric', + month : 'short', + day : 'numeric', + hour : '2-digit', + minute : 'numeric', + hour12 : false, }); } - let message = [null, undefined, ""].includes(param) + let message = [null, undefined, ''].includes(param) ? this.messages[key] - : this.messages[key].replace("[PARAM]", param); + : this.messages[key].replace('[PARAM]', param); - return [null, undefined, ""].includes(field) - ? message.replace("[FIELD]", this.defaultFieldName) - : message.replace("[FIELD]", field); + return [null, undefined, ''].includes(field) + ? message.replace('[FIELD]', this.defaultFieldName) + : message.replace('[FIELD]', field); } /** @@ -175,7 +164,7 @@ export class Iodine { * */ isAfter(value, after) { - return this._dateCompare(value, after, "more", false); + return this._dateCompare(value, after, 'more', false); } /** @@ -183,7 +172,7 @@ export class Iodine { * */ isAfterOrEqual(value, after) { - return this._dateCompare(value, after, "more", true); + return this._dateCompare(value, after, 'more', true); } /** @@ -199,7 +188,7 @@ export class Iodine { * */ isBefore(value, before) { - return this._dateCompare(value, before, "less", false); + return this._dateCompare(value, before, 'less', false); } /** @@ -207,7 +196,7 @@ export class Iodine { * */ isBeforeOrEqual(value, before) { - return this._dateCompare(value, before, "less", true); + return this._dateCompare(value, before, 'less', true); } /** @@ -223,11 +212,7 @@ export class Iodine { * */ isDate(value) { - return ( - value && - Object.prototype.toString.call(value) === "[object Date]" && - !isNaN(value) - ); + return (value && Object.prototype.toString.call(value) === '[object Date]' && !isNaN(value)); } /** @@ -251,9 +236,7 @@ export class Iodine { * */ isEmail(value) { - return new RegExp("^\\S+@\\S+[\\.][0-9a-z]+$").test( - String(value).toLowerCase() - ); + return new RegExp("^\\S+@\\S+[\\.][0-9a-z]+$").test(String(value).toLowerCase()); } /** @@ -261,7 +244,7 @@ export class Iodine { * */ isFalsy(value) { - return [0, "0", false, "false"].includes(value); + return [0, '0', false, 'false'].includes(value); } /** @@ -269,9 +252,7 @@ export class Iodine { * */ isIn(value, options) { - return ( - typeof options === "string" ? options.split(",") : options - ).includes(value); + return (typeof options === 'string' ? options.split(',') : options).includes(value); } /** @@ -279,9 +260,7 @@ export class Iodine { * */ isInteger(value) { - return ( - Number.isInteger(value) && parseInt(value).toString() === value.toString() - ); + return Number.isInteger(value) && parseInt(value).toString() === value.toString(); } /** @@ -290,7 +269,7 @@ export class Iodine { */ isJson(value) { try { - return typeof JSON.parse(value) === "object"; + return typeof JSON.parse(value) === 'object'; } catch (e) { return false; } @@ -317,7 +296,7 @@ export class Iodine { * */ isMaxLength(value, limit) { - return typeof value === "string" ? value.length <= limit : false; + return typeof value === 'string' ? value.length <= limit : false; } /** @@ -325,7 +304,7 @@ export class Iodine { * */ isMinLength(value, limit) { - return typeof value === "string" ? value.length >= limit : false; + return typeof value === 'string' ? value.length >= limit : false; } /** @@ -349,7 +328,7 @@ export class Iodine { * */ isOptional(value) { - return [null, undefined, ""].includes(value); + return [null, undefined, ''].includes(value); } /** @@ -389,7 +368,7 @@ export class Iodine { * */ isString(value) { - return typeof value === "string"; + return typeof value === 'string'; } /** @@ -397,7 +376,7 @@ export class Iodine { * */ isTruthy(value) { - return [1, "1", true, "true"].includes(value); + return [1, '1', true, 'true'].includes(value); } /** @@ -405,8 +384,7 @@ export class Iodine { * */ isUrl(value) { - let regex = - "^(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_]*)?$"; + let regex = "^(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_]*)?$"; return new RegExp(regex).test(String(value).toLowerCase()); } @@ -416,8 +394,7 @@ export class Iodine { * */ isUuid(value) { - let regex = - "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"; + let regex = "^[0-9a-f]{8}-[0-9a-f]{4}-[1-5][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$"; return new RegExp(regex).test(String(value).toLowerCase()); } @@ -427,13 +404,8 @@ export class Iodine { * */ is(value, rules = []) { - for (let index in (rules = this._prepare(value, rules))) { - if ( - !this[`is${rules[index][1]}`].apply(this, [ - value, - rules[index][2].join(":"), - ]) - ) { + for (let index in rules = this._prepare(value, rules)) { + if (!this[`is${rules[index][1]}`].apply(this, [value, rules[index][2].join(':')])) { return rules[index][0]; } } diff --git a/tests/test.js b/tests/test.js index 3890471..d636dbc 100644 --- a/tests/test.js +++ b/tests/test.js @@ -1,4 +1,4 @@ -import { Iodine as Library } from "../src/iodine"; +import { Iodine as Library } from '../src/iodine'; window.Iodine = new Library(); @@ -18,216 +18,176 @@ afterEach(() => { * Confirm that the 'isAfter' method works correctly. * */ -test("after date values", () => { +test('after date values', () => { let year = new Date().getFullYear(); - expect( - Iodine.isAfter(new Date(year + 1, 12, 18), new Date(year, 12, 18)) - ).toBe(true); + expect(Iodine.isAfter(new Date(year + 1, 12, 18), new Date(year, 12, 18))).toBe(true); expect(Iodine.isAfter(new Date(year + 1, 12, 17), Date.now())).toBe(true); - expect( - Iodine.isAfter( - new Date(`December 18, ${year + 1} 03:24:00`), - new Date(year, 12, 18) - ) - ).toBe(true); + expect(Iodine.isAfter(new Date(`December 18, ${year + 1} 03:24:00`), new Date(year, 12, 18))).toBe(true); expect(Iodine.isAfter(new Date(year - 1, 12, 17), Date.now())).toBe(false); expect(Iodine.isAfter(new Date(year - 1, 12, 17), "now")).toBe(false); - expect(Iodine.isAfter("now", new Date(year - 1, 12, 17))).toBe(false); - expect(Iodine.isAfter("now", "now")).toBe(false); + expect(Iodine.isAfter('now', new Date(year - 1, 12, 17))).toBe(false); + expect(Iodine.isAfter('now', 'now')).toBe(false); }); /** * Confirm that the 'isAfterOrEqual' method works correctly. * */ -test("after or equal date values", () => { +test('after or equal date values', () => { let year = new Date().getFullYear(); - expect( - Iodine.isAfterOrEqual(new Date(year + 1, 12, 18), new Date(year, 12, 18)) - ).toBe(true); - expect(Iodine.isAfterOrEqual(new Date(year + 1, 12, 17), Date.now())).toBe( - true - ); - expect( - Iodine.isAfterOrEqual( - new Date(`December 18, ${year + 1} 03:24:00`), - new Date(year, 12, 18) - ) - ).toBe(true); - expect( - Iodine.isAfterOrEqual(new Date(year, 12, 18), new Date(year, 12, 18)) - ).toBe(true); - expect(Iodine.isAfterOrEqual(new Date(year - 1, 12, 17), Date.now())).toBe( - false - ); - expect(Iodine.isAfterOrEqual(new Date(year - 1, 12, 17), "now")).toBe(false); - expect(Iodine.isAfterOrEqual("now", new Date(year - 1, 12, 17))).toBe(false); - expect(Iodine.isAfterOrEqual("now", "now")).toBe(false); + expect(Iodine.isAfterOrEqual(new Date(year + 1, 12, 18), new Date(year, 12, 18))).toBe(true); + expect(Iodine.isAfterOrEqual(new Date(year + 1, 12, 17), Date.now())).toBe(true); + expect(Iodine.isAfterOrEqual(new Date(`December 18, ${year + 1} 03:24:00`), new Date(year, 12, 18))).toBe(true); + expect(Iodine.isAfterOrEqual(new Date(year, 12, 18), new Date(year, 12, 18))).toBe(true); + expect(Iodine.isAfterOrEqual(new Date(year - 1, 12, 17), Date.now())).toBe(false); + expect(Iodine.isAfterOrEqual(new Date(year - 1, 12, 17), 'now')).toBe(false); + expect(Iodine.isAfterOrEqual('now', new Date(year - 1, 12, 17))).toBe(false); + expect(Iodine.isAfterOrEqual('now', 'now')).toBe(false); }); /** * Confirm that the 'isBefore' method works correctly. * */ -test("before date values", () => { +test('before date values', () => { let year = new Date().getFullYear(); - expect( - Iodine.isBefore(new Date(year - 1, 12, 18), new Date(year, 12, 18)) - ).toBe(true); + expect(Iodine.isBefore(new Date(year - 1, 12, 18), new Date(year, 12, 18))).toBe(true); expect(Iodine.isBefore(new Date(year - 1, 12, 17), Date.now())).toBe(true); - expect( - Iodine.isBefore( - new Date(`December 18, ${year - 1} 03:24:00`), - new Date(year, 12, 18) - ) - ).toBe(true); + expect(Iodine.isBefore(new Date(`December 18, ${year - 1} 03:24:00`), new Date(year, 12, 18))).toBe(true); expect(Iodine.isBefore(new Date(year + 1, 12, 17), Date.now())).toBe(false); - expect(Iodine.isBefore(new Date(year + 1, 12, 17), "now")).toBe(false); - expect(Iodine.isBefore("now", new Date(year + 1, 12, 17))).toBe(false); - expect(Iodine.isBefore("now", "now")).toBe(false); + expect(Iodine.isBefore(new Date(year + 1, 12, 17), 'now')).toBe(false); + expect(Iodine.isBefore('now', new Date(year + 1, 12, 17))).toBe(false); + expect(Iodine.isBefore('now', 'now')).toBe(false); }); /** * Confirm that the 'isBeforeOrEqual' method works correctly. * */ -test("before or equal date values", () => { +test('before or equal date values', () => { let year = new Date().getFullYear(); - expect( - Iodine.isBeforeOrEqual(new Date(year - 1, 12, 18), new Date(year, 12, 18)) - ).toBe(true); - expect(Iodine.isBeforeOrEqual(new Date(year - 1, 12, 17), Date.now())).toBe( - true - ); - expect( - Iodine.isBeforeOrEqual( - new Date(`December 18, ${year - 1} 03:24:00`), - new Date(year, 12, 18) - ) - ).toBe(true); - expect( - Iodine.isBeforeOrEqual(new Date(year, 12, 18), new Date(year, 12, 18)) - ).toBe(true); - expect(Iodine.isBeforeOrEqual(new Date(year + 1, 12, 17), Date.now())).toBe( - false - ); - expect(Iodine.isBeforeOrEqual(new Date(year + 1, 12, 17), "now")).toBe(false); - expect(Iodine.isBeforeOrEqual("now", new Date(year + 1, 12, 17))).toBe(false); - expect(Iodine.isBeforeOrEqual("now", "now")).toBe(false); + expect(Iodine.isBeforeOrEqual(new Date(year - 1, 12, 18), new Date(year, 12, 18))).toBe(true); + expect(Iodine.isBeforeOrEqual(new Date(year - 1, 12, 17), Date.now())).toBe(true); + expect(Iodine.isBeforeOrEqual(new Date(`December 18, ${year - 1} 03:24:00`), new Date(year, 12, 18))).toBe(true); + expect(Iodine.isBeforeOrEqual(new Date(year, 12, 18), new Date(year, 12, 18))).toBe(true); + expect(Iodine.isBeforeOrEqual(new Date(year + 1, 12, 17), Date.now())).toBe(false); + expect(Iodine.isBeforeOrEqual(new Date(year + 1, 12, 17), 'now')).toBe(false); + expect(Iodine.isBeforeOrEqual('now', new Date(year + 1, 12, 17))).toBe(false); + expect(Iodine.isBeforeOrEqual('now', 'now')).toBe(false); }); /** * Confirm that the 'isArray' method works correctly. * */ -test("array values", () => { +test('array values', () => { expect(Iodine.isArray([1, 2, 3])).toBe(true); - expect(Iodine.isArray(["1", "2", "3"])).toBe(true); + expect(Iodine.isArray(['1', '2', '3'])).toBe(true); expect(Iodine.isArray(1)).toBe(false); - expect(Iodine.isArray("1")).toBe(false); + expect(Iodine.isArray('1')).toBe(false); }); /** * Confirm that the 'isBoolean' method works correctly. * */ -test("boolean values", () => { +test('boolean values', () => { expect(Iodine.isBoolean(true)).toBe(true); expect(Iodine.isBoolean(false)).toBe(true); expect(Iodine.isBoolean(1)).toBe(false); - expect(Iodine.isBoolean("1")).toBe(false); + expect(Iodine.isBoolean('1')).toBe(false); }); /** * Confirm that the 'isDate' method works correctly. * */ -test("date values", () => { +test('date values', () => { expect(Iodine.isDate(new Date(1995, 12, 17))).toBe(true); - expect(Iodine.isDate(new Date("December 17, 1995 03:24:00"))).toBe(true); + expect(Iodine.isDate(new Date('December 17, 1995 03:24:00'))).toBe(true); expect(Iodine.isDate(1)).toBe(false); - expect(Iodine.isDate("1")).toBe(false); + expect(Iodine.isDate('1')).toBe(false); }); /** * Confirm that the 'isDifferent' method works correctly. * */ -test("different values", () => { +test('different values', () => { expect(Iodine.isDifferent(1, 2)).toBe(true); - expect(Iodine.isDifferent("x", "y")).toBe(true); + expect(Iodine.isDifferent('x', 'y')).toBe(true); expect(Iodine.isDifferent(1, 1)).toBe(false); - expect(Iodine.isDifferent("x", "x")).toBe(false); + expect(Iodine.isDifferent('x', 'x')).toBe(false); }); /** * Confirm that the 'isEndingWith' method works correctly. * */ -test("a value ends with another value", () => { - expect(Iodine.isEndingWith("hello world", "world")).toBe(true); - expect(Iodine.isEndingWith("hello universe", "world")).toBe(false); +test('a value ends with another value', () => { + expect(Iodine.isEndingWith('hello world', 'world')).toBe(true); + expect(Iodine.isEndingWith('hello universe', 'world')).toBe(false); }); /** * Confirm that the 'isEmail' method works correctly. * */ -test("email values", () => { - expect(Iodine.isEmail("john@example.com")).toBe(true); - expect(Iodine.isEmail("m@i.com")).toBe(true); - expect(Iodine.isEmail("😃@i.com")).toBe(true); - expect(Iodine.isEmail("")).toBe(false); - expect(Iodine.isEmail("45454.com")).toBe(false); - expect(Iodine.isEmail("sdfsf@")).toBe(false); +test('email values', () => { + expect(Iodine.isEmail('john@example.com')).toBe(true); + expect(Iodine.isEmail('m@i.com')).toBe(true); + expect(Iodine.isEmail('😃@i.com')).toBe(true); + expect(Iodine.isEmail('')).toBe(false); + expect(Iodine.isEmail('45454.com')).toBe(false); + expect(Iodine.isEmail('sdfsf@')).toBe(false); }); /** * Confirm that the 'isFalsy' method works correctly. * */ -test("falsy values", () => { +test('falsy values', () => { expect(Iodine.isFalsy(false)).toBe(true); - expect(Iodine.isFalsy("false")).toBe(true); + expect(Iodine.isFalsy('false')).toBe(true); expect(Iodine.isFalsy(0)).toBe(true); - expect(Iodine.isFalsy("0")).toBe(true); + expect(Iodine.isFalsy('0')).toBe(true); expect(Iodine.isFalsy(true)).toBe(false); - expect(Iodine.isFalsy("true")).toBe(false); + expect(Iodine.isFalsy('true')).toBe(false); expect(Iodine.isFalsy(1)).toBe(false); - expect(Iodine.isFalsy("1")).toBe(false); + expect(Iodine.isFalsy('1')).toBe(false); }); /** * Confirm that the 'isIn' method works correctly. * */ -test("in list values", () => { - expect(Iodine.isIn("a", "a,b,c")).toBe(true); - expect(Iodine.isIn("a", ["a", "b", "c"])).toBe(true); - expect(Iodine.isIn("d", "a,b,c")).toBe(false); - expect(Iodine.isIn("d", ["a", "b", "c"])).toBe(false); +test('in list values', () => { + expect(Iodine.isIn('a', 'a,b,c')).toBe(true); + expect(Iodine.isIn('a', ['a', 'b', 'c'])).toBe(true); + expect(Iodine.isIn('d', 'a,b,c')).toBe(false); + expect(Iodine.isIn('d', ['a', 'b', 'c'])).toBe(false); }); /** * Confirm that the 'isInteger' method works correctly. * */ -test("integer values", () => { +test('integer values', () => { expect(Iodine.isInteger(1)).toBe(true); expect(Iodine.isInteger(1.5)).toBe(false); - expect(Iodine.isInteger("1")).toBe(false); + expect(Iodine.isInteger('1')).toBe(false); }); /** * Confirm that the 'isJson' method works correctly. * */ -test("json values", () => { - expect(Iodine.isJson("{}")).toBe(true); +test('json values', () => { + expect(Iodine.isJson('{}')).toBe(true); expect(Iodine.isJson('{"a" : 3}')).toBe(true); expect(Iodine.isJson("1")).toBe(false); expect(Iodine.isJson("")).toBe(false); @@ -237,87 +197,87 @@ test("json values", () => { * Confirm that the 'isMax' method works correctly. * */ -test("maximum numeric values", () => { +test('maximum numeric values', () => { expect(Iodine.isMax(1, 5)).toBe(true); expect(Iodine.isMax(5, 5)).toBe(true); expect(Iodine.isMax(6, 5)).toBe(false); - expect(Iodine.isMax("1", 5)).toBe(true); - expect(Iodine.isMax("5", 5)).toBe(true); - expect(Iodine.isMax("6", 5)).toBe(false); + expect(Iodine.isMax('1', 5)).toBe(true); + expect(Iodine.isMax('5', 5)).toBe(true); + expect(Iodine.isMax('6', 5)).toBe(false); }); /** * Confirm that the 'isMin' method works correctly. * */ -test("minimum numeric values", () => { +test('minimum numeric values', () => { expect(Iodine.isMin(6, 5)).toBe(true); expect(Iodine.isMin(5, 5)).toBe(true); expect(Iodine.isMin(4, 5)).toBe(false); - expect(Iodine.isMin("6", 5)).toBe(true); - expect(Iodine.isMin("5", 5)).toBe(true); - expect(Iodine.isMin("4", 5)).toBe(false); + expect(Iodine.isMin('6', 5)).toBe(true); + expect(Iodine.isMin('5', 5)).toBe(true); + expect(Iodine.isMin('4', 5)).toBe(false); }); /** * Confirm that the 'isMaxLength' method works correctly. * */ -test("maximum string length", () => { +test('maximum string length', () => { expect(Iodine.isMaxLength(1, 5)).toBe(false); expect(Iodine.isMaxLength(5, 5)).toBe(false); expect(Iodine.isMaxLength(6, 5)).toBe(false); - expect(Iodine.isMaxLength("1", 5)).toBe(true); - expect(Iodine.isMaxLength("12345", 5)).toBe(true); - expect(Iodine.isMaxLength("123456", 5)).toBe(false); + expect(Iodine.isMaxLength('1', 5)).toBe(true); + expect(Iodine.isMaxLength('12345', 5)).toBe(true); + expect(Iodine.isMaxLength('123456', 5)).toBe(false); }); /** * Confirm that the 'isMinLength' method works correctly. * */ -test("minimum string length", () => { +test('minimum string length', () => { expect(Iodine.isMinLength(6, 5)).toBe(false); expect(Iodine.isMinLength(5, 5)).toBe(false); expect(Iodine.isMinLength(4, 5)).toBe(false); - expect(Iodine.isMinLength("123456", 5)).toBe(true); - expect(Iodine.isMinLength("12345", 5)).toBe(true); - expect(Iodine.isMinLength("1234", 5)).toBe(false); + expect(Iodine.isMinLength('123456', 5)).toBe(true); + expect(Iodine.isMinLength('12345', 5)).toBe(true); + expect(Iodine.isMinLength('1234', 5)).toBe(false); }); /** * Confirm that the 'isNotIn' method works correctly. * */ -test("not in list values", () => { - expect(Iodine.isNotIn("d", "a,b,c")).toBe(true); - expect(Iodine.isNotIn("d", ["a", "b", "c"])).toBe(true); - expect(Iodine.isNotIn("a", "a,b,c")).toBe(false); - expect(Iodine.isNotIn("a", ["a", "b", "c"])).toBe(false); +test('not in list values', () => { + expect(Iodine.isNotIn('d', 'a,b,c')).toBe(true); + expect(Iodine.isNotIn('d', ['a', 'b', 'c'])).toBe(true); + expect(Iodine.isNotIn('a', 'a,b,c')).toBe(false); + expect(Iodine.isNotIn('a', ['a', 'b', 'c'])).toBe(false); }); /** * Confirm that the 'isNumeric' method works correctly. * */ -test("numeric values", () => { +test('numeric values', () => { expect(Iodine.isNumeric(1)).toBe(true); expect(Iodine.isNumeric(2.5)).toBe(true); expect(Iodine.isNumeric(3.45)).toBe(true); - expect(Iodine.isNumeric("13")).toBe(true); - expect(Iodine.isNumeric("14.55")).toBe(true); - expect(Iodine.isNumeric("17.0")).toBe(true); - expect(Iodine.isNumeric("abc")).toBe(false); + expect(Iodine.isNumeric('13')).toBe(true); + expect(Iodine.isNumeric('14.55')).toBe(true); + expect(Iodine.isNumeric('17.0')).toBe(true); + expect(Iodine.isNumeric('abc')).toBe(false); }); /** * Confirm that the 'isOptional' method works correctly. * */ -test("optional values", () => { +test('optional values', () => { expect(Iodine.isOptional(1)).toBe(false); - expect(Iodine.isOptional("1")).toBe(false); - expect(Iodine.isOptional("")).toBe(true); + expect(Iodine.isOptional('1')).toBe(false); + expect(Iodine.isOptional('')).toBe(true); expect(Iodine.isOptional(null)).toBe(true); expect(Iodine.isOptional(undefined)).toBe(true); }); @@ -326,22 +286,20 @@ test("optional values", () => { * Confirm that the 'isRegexMatch' method works correctly. * */ -test("regular expression values", () => { - expect(Iodine.isRegexMatch("P54655465", "^P\\d{3,}$")).toBe(true); - expect(Iodine.isRegexMatch("1234", "^\\S+@\\S+[\\.][0-9a-z]+$")).toBe(false); - expect( - Iodine.isRegexMatch("john@example.com", "^\\S+@\\S+[\\.][0-9a-z]+$") - ).toBe(true); +test('regular expression values', () => { + expect(Iodine.isRegexMatch('P54655465', "^P\\d{3,}$")).toBe(true); + expect(Iodine.isRegexMatch('1234', "^\\S+@\\S+[\\.][0-9a-z]+$")).toBe(false); + expect(Iodine.isRegexMatch('john@example.com', "^\\S+@\\S+[\\.][0-9a-z]+$")).toBe(true); }); /** * Confirm that the 'isRequired' method works correctly. * */ -test("required values", () => { +test('required values', () => { expect(Iodine.isRequired(1)).toBe(true); - expect(Iodine.isRequired("1")).toBe(true); - expect(Iodine.isRequired("")).toBe(false); + expect(Iodine.isRequired('1')).toBe(true); + expect(Iodine.isRequired('')).toBe(false); expect(Iodine.isRequired(null)).toBe(false); expect(Iodine.isRequired(undefined)).toBe(false); }); @@ -350,108 +308,88 @@ test("required values", () => { * Confirm that the 'isSame' method works correctly. * */ -test("same values", () => { +test('same values', () => { expect(Iodine.isSame(1, 1)).toBe(true); - expect(Iodine.isSame("x", "x")).toBe(true); + expect(Iodine.isSame('x', 'x')).toBe(true); expect(Iodine.isSame(1, 2)).toBe(false); - expect(Iodine.isSame("x", "y")).toBe(false); + expect(Iodine.isSame('x', 'y')).toBe(false); }); /** * Confirm that the 'isStartingWith' method works correctly. * */ -test("a value starts with another value", () => { - expect(Iodine.isStartingWith("bye world", "hello")).toBe(false); - expect(Iodine.isStartingWith("hello world", "hello")).toBe(true); +test('a value starts with another value', () => { + expect(Iodine.isStartingWith('bye world', 'hello')).toBe(false); + expect(Iodine.isStartingWith('hello world', 'hello')).toBe(true); }); /** * Confirm that the 'isString' method works correctly. * */ -test("string values", () => { +test('string values', () => { expect(Iodine.isString(1)).toBe(false); - expect(Iodine.isString("1")).toBe(true); + expect(Iodine.isString('1')).toBe(true); }); /** * Confirm that the 'isTruthy' method works correctly. * */ -test("truthy values", () => { +test('truthy values', () => { expect(Iodine.isTruthy(true)).toBe(true); - expect(Iodine.isTruthy("true")).toBe(true); + expect(Iodine.isTruthy('true')).toBe(true); expect(Iodine.isTruthy(1)).toBe(true); - expect(Iodine.isTruthy("1")).toBe(true); + expect(Iodine.isTruthy('1')).toBe(true); expect(Iodine.isTruthy(false)).toBe(false); - expect(Iodine.isTruthy("false")).toBe(false); + expect(Iodine.isTruthy('false')).toBe(false); expect(Iodine.isTruthy(0)).toBe(false); - expect(Iodine.isTruthy("0")).toBe(false); + expect(Iodine.isTruthy('0')).toBe(false); }); /** * Confirm that the 'isUrl' method works correctly. * */ -test("url values", () => { - expect(Iodine.isUrl("1234")).toBe(false); - expect(Iodine.isUrl("http://www.google.com")).toBe(true); +test('url values', () => { + expect(Iodine.isUrl('1234')).toBe(false); + expect(Iodine.isUrl('http://www.google.com')).toBe(true); }); /** * Confirm that the 'isUuid' method works correctly. * */ -test("UUID values", () => { - expect(Iodine.isUuid("9034dfa4-49d9-4e3f-9c6d-bc6a0e2233d1")).toBe(true); - expect(Iodine.isUuid("XAZLYYZeNu75xkicYcPoBWhAW0AX2HRlbqbK")).toBe(false); +test('UUID values', () => { + expect(Iodine.isUuid('9034dfa4-49d9-4e3f-9c6d-bc6a0e2233d1')).toBe(true); + expect(Iodine.isUuid('XAZLYYZeNu75xkicYcPoBWhAW0AX2HRlbqbK')).toBe(false); }); /** * Confirm that the 'is' method works correctly. * */ -test("return true/failed rule", () => { - expect(Iodine.is("5", ["required", "string", "min:1", "max:5"])).toBe(true); - expect(Iodine.is(5, ["required", "integer", "min:7", "max:10"])).toBe( - "min:7" - ); - expect(Iodine.is(5, ["optional", "integer", "min:7", "max:10"])).toBe( - "min:7" - ); - expect(Iodine.is("", ["optional", "integer", "min:7", "max:10"])).toBe(true); - expect(Iodine.is(null, ["optional", "integer", "min:7", "max:10"])).toBe( - true - ); - expect(Iodine.is(undefined, ["optional", "integer", "min:7", "max:10"])).toBe( - true - ); +test('return true/failed rule', () => { + expect(Iodine.is('5', ['required', 'string', 'min:1', 'max:5'])).toBe(true); + expect(Iodine.is(5, ['required', 'integer', 'min:7', 'max:10'])).toBe('min:7'); + expect(Iodine.is(5, ['optional', 'integer', 'min:7', 'max:10'])).toBe('min:7'); + expect(Iodine.is('', ['optional', 'integer', 'min:7', 'max:10'])).toBe(true); + expect(Iodine.is(null, ['optional', 'integer', 'min:7', 'max:10'])).toBe(true); + expect(Iodine.is(undefined, ['optional', 'integer', 'min:7', 'max:10'])).toBe(true); }); /** * Confirm that the 'isValid' method returns the right value against multiple rules. * */ -test("return true/false", () => { - expect(Iodine.isValid("5", ["required", "string", "min:1", "max:5"])).toBe( - true - ); - expect(Iodine.isValid(5, ["required", "integer", "min:7", "max:10"])).toBe( - false - ); - expect(Iodine.isValid(5, ["optional", "integer", "min:7", "max:10"])).toBe( - false - ); - expect(Iodine.isValid("", ["optional", "integer", "min:7", "max:10"])).toBe( - true - ); - expect(Iodine.isValid(null, ["optional", "integer", "min:7", "max:10"])).toBe( - true - ); - expect( - Iodine.isValid(undefined, ["optional", "integer", "min:7", "max:10"]) - ).toBe(true); +test('return true/false', () => { + expect(Iodine.isValid('5', ['required', 'string', 'min:1', 'max:5'])).toBe(true); + expect(Iodine.isValid(5, ['required', 'integer', 'min:7', 'max:10'])).toBe(false); + expect(Iodine.isValid(5, ['optional', 'integer', 'min:7', 'max:10'])).toBe(false); + expect(Iodine.isValid('', ['optional', 'integer', 'min:7', 'max:10'])).toBe(true); + expect(Iodine.isValid(null, ['optional', 'integer', 'min:7', 'max:10'])).toBe(true); + expect(Iodine.isValid(undefined, ['optional', 'integer', 'min:7', 'max:10'])).toBe(true); }); /** @@ -459,140 +397,90 @@ test("return true/false", () => { * */ test('parameter that contains semicolon(":")', () => { - expect(Iodine.is(":b", ["required", "regexMatch:^:\\w$"])).toBe(true); - expect(Iodine.is("a:b", ["required", "regexMatch:^:\\w$"])).not.toBe(true); - expect(Iodine.is(":b", ["required", "regexMatch:^:\\w$"])).toBe(true); - expect(Iodine.is("a:b", ["required", "regexMatch:^:\\w$"])).not.toBe(true); + expect(Iodine.is(':b', ['required', "regexMatch:^:\\w$"])).toBe(true); + expect(Iodine.is('a:b', ['required', "regexMatch:^:\\w$"])).not.toBe(true); + expect(Iodine.is(':b', ['required', "regexMatch:^:\\w$"])).toBe(true); + expect(Iodine.is('a:b', ['required', "regexMatch:^:\\w$"])).not.toBe(true); }); /** * Confirm that the 'getErrorMessage' method works correctly. * */ -test("it retrieves formatted error messages for rules", () => { +test('it retrieves formatted error messages for rules', () => { let time = Date.UTC(2020, 4, 2, 10, 17, 0); - Iodine.setLocale("en-US"); + Iodine.setLocale('en-US'); let hour = new Date(parseInt(time)).getHours(); - 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("endingWith", { field: "Song title" })).toBe( - `Song title must end with '[PARAM]'` - ); - expect( - Iodine.getErrorMessage("endingWith:world", { field: "Song title" }) - ).toBe(`Song title must end with 'world'`); - expect( - Iodine.getErrorMessage("endingWith", { - field: "Song title", - param: "world", - }) - ).toBe(`Song title must end with 'world'`); - expect(Iodine.getErrorMessage("endingWith", { param: "world" })).toBe( - `Value must end with 'world'` - ); - expect(Iodine.getErrorMessage(`after:${time}`)).toBe( - `The date must be after: 'May 2, 2020, ${hour}:17'` - ); - expect(Iodine.getErrorMessage(`after`, time)).toBe( - `The date must be after: 'May 2, 2020, ${hour}:17'` - ); + 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('endingWith', { field : 'Song title' })).toBe(`Song title must end with '[PARAM]'`); + expect(Iodine.getErrorMessage('endingWith:world', { field : 'Song title' })).toBe(`Song title must end with 'world'`); + expect(Iodine.getErrorMessage('endingWith', { field : 'Song title', param : 'world'})).toBe(`Song title must end with 'world'`); + expect(Iodine.getErrorMessage('endingWith', { param : 'world' })).toBe(`Value must end with 'world'`); + expect(Iodine.getErrorMessage(`after:${time}`)).toBe(`The date must be after: 'May 2, 2020, ${hour}:17'`); + expect(Iodine.getErrorMessage(`after`, time)).toBe(`The date must be after: 'May 2, 2020, ${hour}:17'`); }); /** * Confirm that the default error messages can be replaced. * */ -test("it can replace the default error messages", () => { - Iodine.setErrorMessages({ - array: "Hello world", - endingWith: "Hello, [PARAM]", - startingWith: "[FIELD]: [PARAM] says, 'hello'", - }); - - 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", { param: "John" })).toBe( - "Hello, John" - ); - expect(Iodine.getErrorMessage("endingWith", "John")).toBe("Hello, John"); - expect(Iodine.getErrorMessage("startingWith:Paul")).toBe( - `Value: Paul says, 'hello'` - ); - expect(Iodine.getErrorMessage("startingWith", "Paul")).toBe( - `Value: Paul says, 'hello'` - ); - expect(Iodine.getErrorMessage("startingWith", { param: "Paul" })).toBe( - `Value: Paul says, 'hello'` - ); - expect(Iodine.getErrorMessage("startingWith:Paul", { field: "Name" })).toBe( - `Name: Paul says, 'hello'` - ); - expect( - Iodine.getErrorMessage("startingWith", { field: "Name", param: "Paul" }) - ).toBe(`Name: Paul says, 'hello'`); +test('it can replace the default error messages', () => { + Iodine.setErrorMessages({ array : 'Hello world', endingWith : 'Hello, [PARAM]', startingWith : "[FIELD]: [PARAM] says, 'hello'" }); + + 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', { param : 'John' })).toBe('Hello, John'); + expect(Iodine.getErrorMessage('endingWith', 'John')).toBe('Hello, John'); + expect(Iodine.getErrorMessage('startingWith:Paul')).toBe(`Value: Paul says, 'hello'`); + expect(Iodine.getErrorMessage('startingWith', 'Paul')).toBe(`Value: Paul says, 'hello'`); + expect(Iodine.getErrorMessage('startingWith', { param : 'Paul' })).toBe(`Value: Paul says, 'hello'`); + expect(Iodine.getErrorMessage('startingWith:Paul', { field : 'Name' })).toBe(`Name: Paul says, 'hello'`); + expect(Iodine.getErrorMessage('startingWith', { field : 'Name', param : 'Paul' })).toBe(`Name: Paul says, 'hello'`); }); /** * Confirm the defualt field name can be replaced. */ -test("it can replace the default field name", () => { - Iodine.setDefaultFieldName("Input"); +test('it can replace the default field name', () => { + Iodine.setDefaultFieldName('Input'); - expect(Iodine.getErrorMessage("array")).toBe("Input must be an array"); - expect(Iodine.getErrorMessage("endingWith")).toBe( - `Input must end with '[PARAM]'` - ); - expect(Iodine.getErrorMessage("endingWith:world")).toBe( - `Input must end with 'world'` - ); - expect(Iodine.getErrorMessage("endingWith", { param: "world" })).toBe( - `Input must end with 'world'` - ); + expect(Iodine.getErrorMessage('array')).toBe('Input must be an array'); + expect(Iodine.getErrorMessage('endingWith')).toBe(`Input must end with '[PARAM]'`); + expect(Iodine.getErrorMessage('endingWith:world')).toBe(`Input must end with 'world'`); + expect(Iodine.getErrorMessage('endingWith', { param : 'world' })).toBe(`Input must end with 'world'`); }); /** * Confirm that a single error message can be replaced. * */ -test("it can replace a default error message", () => { +test('it can replace a default error message', () => { const messagesCount = Object.keys(Iodine.messages).length; - Iodine.setErrorMessage("email", "Does not look like a valid email"); + Iodine.setErrorMessage('email', 'Does not look like a valid email'); - expect(Iodine.getErrorMessage("email")).toBe( - "Does not look like a valid email" - ); + expect(Iodine.getErrorMessage('email')).toBe('Does not look like a valid email'); expect(Object.keys(Iodine.messages).length).toEqual(messagesCount); - expect(Iodine.getErrorMessage("date")).toBe("Value must be a date"); + expect(Iodine.getErrorMessage('date')).toBe('Value must be a date'); }); /** * Confirm that a single error message can be added to the set. * */ -test("it can add an error message to the set", () => { +test('it can add an error message to the set', () => { const messagesCount = Object.keys(Iodine.messages).length; - Iodine.setErrorMessage( - "passwordConfirmation", - "Password confirmation needs to match" - ); + Iodine.setErrorMessage('passwordConfirmation', 'Password confirmation needs to match'); - expect(Iodine.getErrorMessage("passwordConfirmation")).toBe( - "Password confirmation needs to match" - ); + expect(Iodine.getErrorMessage('passwordConfirmation')).toBe('Password confirmation needs to match'); expect(Object.keys(Iodine.messages).length).toEqual(messagesCount + 1); }); @@ -600,68 +488,52 @@ test("it can add an error message to the set", () => { * Confirm that the 'addRule' method works correctly for simple rules. * */ -test("it can add simple custom rules", () => { - Iodine.addRule("lowerCase", (value) => value === value.toLowerCase()); +test('it can add simple custom rules', () => { + Iodine.addRule('lowerCase', (value) => value === value.toLowerCase()); - Iodine.setErrorMessages({ lowerCase: "Value must be in lower case" }); + 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" - ); + 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); +test('it can add advanced custom rules', () => { + Iodine.addRule('equals', (value, param) => value == param); - Iodine.setErrorMessages({ equals: `Value must be equal to '[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'` - ); + 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'`); }); /** * Confirm that the 'addRule' method works correctly for async rules. * */ -test("it can add async custom rules", async () => { - Iodine.addRule( - "timeoutEquals", - (value, param) => - new Promise((resolve) => setTimeout(resolve(value == param), 10)) - ); +test('it can add async custom rules', async () => { + Iodine.addRule('timeoutEquals', (value, param) => new Promise(resolve => setTimeout(resolve(value == param), 10))); - Iodine.setErrorMessages({ - timeoutEquals: `Value must be equal to '[PARAM]' after 10ms`, - }); + Iodine.setErrorMessages({ timeoutEquals : `Value must be equal to '[PARAM]' after 10ms` }); expect(await Iodine.isTimeoutEquals(1, 1)).toBe(true); expect(await Iodine.isTimeoutEquals(1, 2)).toBe(false); - expect(await Iodine.asyncIs(1, ["required", "timeoutEquals:1"])).toBe(true); - expect(await Iodine.asyncIs(1, ["required", "timeoutEquals:2"])).toBe( - "timeoutEquals:2" - ); - expect( - await Iodine.asyncIsValid(1, ["required", "integer", "timeoutEquals:1"]) - ).toBe(true); - expect( - await Iodine.asyncIsValid(1, ["required", "integer", "timeoutEquals:2"]) - ).toBe(false); -}); + expect(await Iodine.asyncIs(1, ['required', 'timeoutEquals:1'])).toBe(true); + expect(await Iodine.asyncIs(1, ['required', 'timeoutEquals:2'])).toBe('timeoutEquals:2'); + expect(await Iodine.asyncIsValid(1, ['required', 'integer', 'timeoutEquals:1'])).toBe(true); + expect(await Iodine.asyncIsValid(1, ['required', 'integer', 'timeoutEquals:2'])).toBe(false); +}); \ No newline at end of file