Lightweight and Powerful Data Validation Library – Iodine.js

Category: Javascript | August 25, 2023
Author:caneara
Views Total:109 views
Official Page:Go to website
Last Update:August 25, 2023
License:MIT

Preview:

Lightweight and Powerful Data Validation Library – Iodine.js

Description:

Iodine is a tiny validation JavaScript library that lets you easily validate data in the browser with a simple API and no dependencies. It supports chainable validation rules so you can verify data meets multiple criteria.

Out of the box, it includes rules for common needs like checking for valid emails, dates, numbers, and more. You can also create custom rules.

How to use it:

1. Install and import the Iodine.

# NPM
$ npm i @caneara/iodine
// browser
<script src="/path/to/dist/iodine.min.umd.js" defer></script>
// ES Module
import Iodine from '@caneara/iodine';

2. Initiate a new instance of the Iodine:

const myValidator = new Iodine();

3. Here are the available validation rules in Iodine. Note that all of Iodine’s rules start with assert.

RuleDescription
assertAfter(date/integer)Verify that the item is a Date after a given Date or timestamp
assertAfterOrEqual(date/integer)Verify that the item is a Date after or equal to a given Date or timestamp
assertArrayVerify that the item is an array
assertBefore(date/integer)Verify that the item is a Date before a given Date or timestamp
assertBeforeOrEqual(date/integer)Verify that the item is a Date before or equal to a given Date or timestamp
assertBooleanVerify that the item is either true or false
assertDateVerify that the item is a Date object
assertDifferent(value)Verify that the item is different to the supplied value (uses loose compare)
assertEnds(value)Verify that the item ends with the given value
assertEmailVerify that the item is a valid email address
assertFalsyVerify that the item is either false, 'false', 0 or '0'
assertIn(array)Verify that the item is within the given array
assertIntegerVerify that the item is an integer
assertJsonVerify that the item is a parsable JSON object string
assertMaxLength(limit)Verify that the item’s character length does not exceed the given limit
assertMinLength(limit)Verify that the item’s character length is not under the given limit
assertMax(limit)Verify that the item’s numerical value does not exceed the given limit
assertMin(limit)Verify that the item’s numerical value is not under the given limit
assertNotIn(array)Verify that the item is not within the given array
assertNumericVerify that the item is number or a numeric string
assertOptionalAllow for optional values (only for use with multiple checks)
assertRegexMatch(exp)Verify that the item satisfies the given regular expression
assertRequiredVerify that the item is not null, undefined or an empty string
assertSame(value)Verify that the item is the same as the supplied value (uses loose compare)
assertStartsWith(value)Verify that the item starts with the given value
assertStringVerify that the item is a string
assertTruthyVerify that the item is either true, 'true', 1 or '1'
assertUrlVerify that the item is a valid URL
assertUuidVerify that the item is a UUID
// true
myValidator.assertInteger(7); 
// false
myValidator.assertInteger('cssscript.com');

4. Apply multiple rules to your data.

// true
myValidator.assertInteger(7, ['required', 'integer']);
// false
myValidator.assertInteger('cssscript.com', ['required', 'integer']);
// multiple items
const items = {
      name: CSSScript,
      email: '[email protected]',
      password: 'abcdefgh',
};
const rules = {
      name: ['required', 'string'],
      email: ['required', 'email'],
      password: ['required'],
};
myValidator.assert(items, rules);
// additional parameters
myValidator.assert(7, ['required', 'integer', 'min:5']);
// Optional values
myValidator.assert(7, ['optional', 'integer']);

5. Replace default error messages:

// Custom Errors
myValidator.setErrorMessages({ 
  same : `[FIELD] must be '[PARAM]'` 
});
// Single Errors
myValidator.setErrorMessage('passwordConfirmation', 'Does not match password');
// Custom Errors by Field
myValidator.assert(value, ['required'], { 'required' : 'The "Label" must be present.' });

6. Add custom rules using the rule function:

// Simple
myValidator.rule('newRule', (value) => ());
// With parameters
myValidator.rule('newRule', (value, param) => ());

You Might Be Interested In:


Leave a Reply