Security News
GitHub Removes Malicious Pull Requests Targeting Open Source Repositories
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
@kingshott/iodine
Advanced tools
Iodine.js is a micro client-side validation library. It has no dependencies and can be used in isolation or as part of a framework. Iodine also supports chainable rules, allowing you to verify that a piece of data satisifies multiple criteria.
The easiest way to pull Iodine into your project is via a CDN:
<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:
npm i @kingshott/iodine
import Iodine from '@kingshott/iodine';
Iodine is automatically added to the window
namespace, making it available anywhere on the page / within your application.
Iodine's rules are prefixed with the is
keyword. So, to check if an item is an integer
, you'd use the following code:
let item_1 = 7;
let item_2 = 'string';
Iodine.isInteger(item_1); // true
Iodine.isInteger(item_2); // false
Single checks return a true
or false
value, indicating whether the item passed validation.
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.
let item_1 = 7;
let item_2 = 'string';
Iodine.is(item_1, ['required', 'integer']); // true
Iodine.is(item_2, ['required', 'integer']); // string - 'integer'
The is
method will return true
if the item passes every rule.
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'.
Some rules require extra parameters e.g.
let item_1 = 7;
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.
let item_1 = 7;
let item_2 = 4;
Iodine.is(item_1, ['required', 'integer', 'minimum:5']); // true
Iodine.is(item_2, ['required', 'integer', 'minimum:5']); // string - 'minimum:5'
When performing multiple checks, you may wish to allow for optional values. Iodine supports this with the optional
rule:
let item_1 = 7;
let item_2 = null;
let item_3 = 'string';
Iodine.is(item_1, ['optional', 'integer']); // true
Iodine.is(item_2, ['optional', 'integer']); // true
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.
Iodine includes a default set of error messages for the English language. To retrieve an error message for a rule, use the getErrorMessage
method:
Iodine.getErrorMessage('array'); // string
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.
Iodine.getErrorMessage('minimum:7'); // string
Iodine.getErrorMessage('minimum', 7); // string
You can easily replace the default error messages with your own via the setErrorMessages
method. This method requires a single parameter, which is an object
containing the messages. See the _defaultMessages method for an example of this.
Iodine will automatically swap [PARAM]
placeholders with the parameters supplied in the getErrorMessage
method. As such, you should insert this placeholder at the appropriate position in your new error message e.g.
Iodine.setErrorMessages({ same: `Field must be '[PARAM]'` }); // English
Iodine.setErrorMessages({ same: `Champ doit être '[PARAM]'` }); // French
The following validation rules are available:
Rule | Description |
---|---|
isAfter(date/integer) | Verify that the item is a Date after a given Date or timestamp |
isAfterOrEqual(date/integer) | Verify that the item is a Date after or equal to a given Date or timestamp |
isArray | Verify that the item is an array |
isBefore(date/integer) | Verify that the item is a Date before a given Date or timestamp |
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 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' |
isIn(array) | Verify that the item is within the given array |
isInteger | Verify that the item is an integer |
isJson | Verify that the item is a parsable JSON object string |
isMaximum(limit) | Verify that the item does not exceed the given limit (number or char length) |
isMinimum(limit) | Verify that the item is not under the given limit (number or char length) |
isNotIn(array) | Verify that the item is not within the given array |
isNumeric | Verify that the item is number or a numeric string |
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 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' |
isUrl | Verify that the item is a valid URL |
isUuid | Verify that the item is a UUID |
Examine the tests for examples of how to use each rule.
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.
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.
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.
Iodine.addRule('equals', (value, param) => value == param);
You can also add error messages for your custom rules e.g.
Iodine.addRule('equals', (value, param) => value == param);
Iodine.setErrorMessages({ equals : `Value must be equal to '[PARAM]'` });
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.
If you'd like to support the development of Iodine, then please consider sponsoring me. Thanks so much!
The MIT License (MIT). Please see License File for more information.
FAQs
A micro client-side validation library
The npm package @kingshott/iodine receives a total of 1,420 weekly downloads. As such, @kingshott/iodine popularity was classified as popular.
We found that @kingshott/iodine demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
GitHub removed 27 malicious pull requests attempting to inject harmful code across multiple open source repositories, in another round of low-effort attacks.
Security News
RubyGems.org has added a new "maintainer" role that allows for publishing new versions of gems. This new permission type is aimed at improving security for gem owners and the service overall.
Security News
Node.js will be enforcing stricter semver-major PR policies a month before major releases to enhance stability and ensure reliable release candidates.