Skip to content
This repository has been archived by the owner on Apr 1, 2024. It is now read-only.

Commit

Permalink
improved getErrorMessage method, fleshed out the readme
Browse files Browse the repository at this point in the history
  • Loading branch information
mattkingshott committed Apr 3, 2020
1 parent ca18530 commit 14839fc
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 15 deletions.
32 changes: 22 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
Expand All @@ -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.
Expand All @@ -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
```

Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
4 changes: 2 additions & 2 deletions resources/version.svg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
4 changes: 2 additions & 2 deletions src/iodine.js
Original file line number Diff line number Diff line change
Expand Up @@ -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];
Expand All @@ -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);
}
Expand Down
1 change: 1 addition & 0 deletions tests/test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'`);
Expand Down

0 comments on commit 14839fc

Please sign in to comment.