diff --git a/README.md b/README.md index 20fee54..971b6eb 100755 --- a/README.md +++ b/README.md @@ -48,6 +48,8 @@ Iodine.isInteger(item_1); // true Iodine.isInteger(item_2); // false ``` +Single checks return a `true` or `false` value, indicating whether the item passed validation. + ## Multiple checks If you want to verify whether an item passes a set of rules, you should use the main `is` method. This method accepts two parameters. The first, is the item you want to check. The second, is an array of rules that should be run in sequence e.g. @@ -57,30 +59,40 @@ let item_1 = 7; let item_2 = 'string'; Iodine.is(item_1, ['required', 'integer']); // true -Iodine.is(item_2, ['required', 'integer']); // false +Iodine.is(item_2, ['required', 'integer']); // string - 'integer' ``` The `is` method will return `true` if the item passes every rule. -Alternatively, the name of the first rule that failed will be returned e.g. `'integer'`. +If the item fails to validate, the first rule that it failed to satisfy will be returned e.g. `'integer'`. > Version 1 of Iodine only returned the rule name e.g. 'minimum'. Version 2+ returns the rule name and any supplied parameter e.g. 'minimum:7'. ## Additional parameters -Some rules require extra parameters. You can supply them by adding them to the rule with a semicolon separator e.g. +Some rules require extra parameters e.g. ```js let item_1 = 7; -let item_2 = 'string'; +let item_2 = 4; + +Iodine.isMinimum(item_1, 5); // true +Iodine.isMinimum(item_2, 5); // false +``` + +For multiple checks, you can supply the parameters by appending them to the rule with a semicolon separator e.g. + +```js +let item_1 = 7; +let item_2 = 4; -Iodine.is(item_1, ['required', 'integer', 'minimum:5', 'maximum:10']); // true -Iodine.is(item_2, ['required', 'integer', 'minimum:5', 'maximum:10']); // false +Iodine.is(item_1, ['required', 'integer', 'minimum:5']); // true +Iodine.is(item_2, ['required', 'integer', 'minimum:5']); // string - 'minimum:5' ``` ## Optional values -Sometimes, you may wish to allow for optional values. Iodine supports this with the `optional` rule: +When performing multiple checks, you may wish to allow for optional values. Iodine supports this with the `optional` rule: ```js let item_1 = 7; @@ -89,7 +101,7 @@ let item_3 = 'string'; Iodine.is(item_1, ['optional', 'integer']); // true Iodine.is(item_2, ['optional', 'integer']); // true -Iodine.is(item_3, ['optional', 'integer']); // false +Iodine.is(item_3, ['optional', 'integer']); // string - 'integer' ``` **IMPORTANT**: If you wish to allow for optional values, then you must supply `'optional'` as the first rule in the list. @@ -102,10 +114,10 @@ Iodine includes a default set of error messages for the English language. To ret Iodine.getErrorMessage('array'); // string ``` -When dealing with parameters, the `getErrorMessage` method allows you to supply the rule either as a single `string` or as two arguments (the rule and parameter) e.g. +When dealing with rules that have parameters, the `getErrorMessage` method allows you to supply the rule either as a combined `string` or as two arguments (the rule and parameter) e.g. ```js -Iodine.getErrorMessage('minimum:7'); // string +Iodine.getErrorMessage('minimum:7'); // string Iodine.getErrorMessage('minimum', 7); // string ``` diff --git a/package.json b/package.json index a828af0..43c4ab5 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "@kingshott/iodine", - "version": "2.0.1", + "version": "2.0.2", "description": "A micro client-side validation library", "repository": { "type": "git", diff --git a/resources/version.svg b/resources/version.svg index 593c008..289526b 100644 --- a/resources/version.svg +++ b/resources/version.svg @@ -14,7 +14,7 @@ stable stable - v2.0.1 - v2.0.1 + v2.0.2 + v2.0.2 \ No newline at end of file diff --git a/src/iodine.js b/src/iodine.js index ab50e17..c635e75 100644 --- a/src/iodine.js +++ b/src/iodine.js @@ -84,7 +84,7 @@ export default class Iodine * Retrieve an error message for the given rule. * **/ - getErrorMessage(rule, arg = null) + getErrorMessage(rule, arg = undefined) { let key = rule.split(':')[0]; let param = arg || rule.split(':')[1]; @@ -95,7 +95,7 @@ export default class Iodine }); } - return param === undefined + return [null, undefined].includes(param) ? this.messages[key] : this.messages[key].replace("[PARAM]", param); } diff --git a/tests/test.js b/tests/test.js index 0e0654e..a0c73b9 100644 --- a/tests/test.js +++ b/tests/test.js @@ -400,6 +400,7 @@ 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(`after:${time}`)).toBe(`The date must be after: '2 May 2020, 04:17'`);