npm i indicative
# yarn
yarn add indicative
Just like any other Javascript package, Indicative is published on npm and can be installed using any npm client.
npm i indicative
# yarn
yarn add indicative
Indicative comes with bunch of validation
and sanitization
rules, which can make the build size larger than expected.
To keep indicative lightweight, we make it possible to bundle what you use. However, when using Indicative with Node.js, it is recommended to use the full build and avoid un-neccessary setup process.
No matter how you import modules from indicative, the usage API remains the same. |
// full build
const indicative = require('indicative')
indicative.validate(data, rules)
indicative.validateAll(data, rules)
// raw validator
indicative.is.email(emailAddress)
// add your own rules
indicative.extend('exists', existsFn)
// sanitize data
indicative.sanitize(data, rules)
// raw sanitizor
indicative.sanitizor.normalizeEmail(emailAddress)
When building apps for the browser, it is important to save every byte. Indicative has modularized builds, written specifically for smaller output.
Configuring modules of indicative together can be a bit tedious, but it is only one time process and can result in significantly smaller build size.
Let’s do it step by step.
Import the core validator (weighs 1.6KB)
.
import Validator from 'indicative/builds/validator'
Cherry pick validations you want to make use of. It is fine to pull all the validations your entire app will be using.
import {
email,
required,
min,
max
} from 'indicative/builds/validations'
Grab the errors formatter you want to make use of.
import { Vanilla } from 'indicative/builds/formatters'
Instantiate validator by passing all the imported validations. You can re-use the same instance to run multiple validations, so it makes sense to save it’s reference.
const validatorInstance = Validator({ email, required, min, max }, Vanilla)
// vuejs example
Vue.use(function (__Vue__) {
__Vue__.prototype.validator = validatorInstance
})
Finally, use the validatorInstance
to run validations as you would do with the full build.
const rules = {
username: 'required',
password: 'required|min:4|max:40'
}
validatorInstance
.validate(data, rules)
.then(() => {})
.catch((errors) => {})
The same process is followed for importing sanitzations and self creating a sanitizor instance.
import Sanitizor from 'indicative/builds/sanitizor'
import {
normalizeEmail,
stripTags,
toNull
} from 'indicative/builds/sanitizations'
const sanitizorInstance = Sanitizor({ normalizeEmail, stripTags, toNull })
Here’s the list of build size for each build.
Build | Minified | Gzip |
---|---|---|
Full builds/main.js |
51KB |
17KB |
Validator builds/validator.js |
3.6KB |
1.6KB |
Sanitizor builds/sanitizor.js |
4.7KB |
1.97KB |
Validations builds/validations.js |
21KB |
5.95KB |
Sanitizations builds/sanitizations.js |
21KB |
8.16 kB |
Raw validations builds/raw.js |
17KB |
5.5KB |