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

Commit

Permalink
added custom rules, changed error messages, fixed same and different …
Browse files Browse the repository at this point in the history
…rules
  • Loading branch information
mattkingshott committed Apr 3, 2020
1 parent 3c1d209 commit 15e8a8e
Show file tree
Hide file tree
Showing 11 changed files with 119 additions and 43 deletions.
34 changes: 31 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
<script src="https://cdn.jsdelivr.net/gh/mattkingshott/iodine@2/dist/iodine.min.js" defer></script>
<script src="https://cdn.jsdelivr.net/gh/mattkingshott/iodine@3/dist/iodine.min.js" defer></script>
```

You can also pull Iodine into your project via NPM:
Expand Down Expand Up @@ -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'`
Expand All @@ -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'`
Expand All @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion dist/iodine.min.js

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 15e8a8e

Please sign in to comment.