Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@kingshott/iodine

Package Overview
Dependencies
Maintainers
1
Versions
27
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@kingshott/iodine

A micro client-side validation library

  • 3.0.0
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
1.5K
decreased by-61.28%
Maintainers
1
Weekly downloads
 
Created
Source

Code example

Size Build Coverage Version License

Iodine

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.

Installation

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';

Usage

Iodine is automatically added to the window namespace, making it available anywhere on the page / within your application.

Single checks

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.

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.

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'.

Additional parameters

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'

Optional values

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.

Error messages

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

Custom messages (localisation)

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

Available rules

The following validation rules are available:

RuleDescription
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
isArrayVerify 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
isBooleanVerify that the item is either true or false
isDateVerify 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
isEmailVerify that the item is a valid email address
isFalsyVerify that the item is either false, 'false', 0 or '0'
isIn(array)Verify that the item is within the given array
isIntegerVerify that the item is an integer
isJsonVerify 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
isNumericVerify that the item is number or a numeric string
isOptionalAllow for optional values (only for use with multiple checks)
isRegexMatch(exp)Verify that the item satisifies the given regular expression
isRequiredVerify 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
isStringVerify that the item is a string
isTruthyVerify that the item is either true, 'true', 1 or '1'
isUrlVerify that the item is a valid URL
isUuidVerify that the item is a UUID

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.

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]'` });

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.

Support the project

If you'd like to support the development of Iodine, then please consider sponsoring me. Thanks so much!

License

The MIT License (MIT). Please see License File for more information.

FAQs

Package last updated on 03 Apr 2020

Did you know?

Socket

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.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc