Installation
Valid8r is a node.js library, which means any application that runs on node.js can use valid8r.
npm install @c4code/valid8r
Installing Valid8r is quick and simple. Just run npm install valid8r
to get started, and you're ready to validate your fields with minimal setup.
Import
To use Valid8r in your project, simply import it with the following line
import valid8r from '@c4code/valid8r';
That it! Once imported, you can start using its validation functions right away.
Basic Usage
Here's how to use Valid8r for basic field validation
import valid8r from '@c4code/valid8r';
// Your input's value
const input: string = "Jay Carlos";
const [isValid, errors] = valid8r.name(input);
console.log(isValid); // true
const input2: string = "M4rk Black";
valid8r.name(input); // throws NameValidationFailed error
The functions return true
if the input is valid, and false
if it is not. If the function returns false
, the errors Array
will contain all the errors that was given by the input.
Safe Handling
Here's how to use Valid8r for basic field validation using Safe error throwing
import valid8r from '@c4code/valid8r';
// Your input's value
const input: string = "Jay Carlos";
const [isValid, errors] = valid8r.name(input, { safe: true });
console.log(isValid); // if { safe: false }, this will always return true
const input2: string = "M4rk Black";
const [valid, errors2] = valid8r.name(input);
if (!valid) {
console.log(errors2);
// [{ "noSpChars": "Name should not contain any special characters." }]
}
{ safe: true }
will ensure that all the errors will be returned instead of throwing them, if set to false it will throw the first error by default if not configured otherwise.
Last updated