Valid8r is a lightweight and efficient npm package designed to handle all your input and form validation needs with ease and precision.
Documentation
Codesandbox ( React + TS )
Valid8r is a node.js library, which means any application that runs on node.js can use valid8r.
Start by installing 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.
Install Valid8r with npm / yarn / bun
npm install @c4code/valid8r // For npm
yarn add @c4code/valid8r // For Yarn
bun add @c4code/valid8r // For Bun
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. 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 null as the second parameter. If the validation fails however it will throw an Error.
if you don't want it to throw error and instead return all the errors, use the { safe: true }
flag
import valid8r from '@c4code/valid8r';
const input2: string = "M4rk Black";
const [valid, errors2] = valid8r.name(input, { safe: true });
if (!valid) {
console.log(errors2);
// [{ "noSpChars": "Name should not contain any special characters." }]
}
Use ES6 Module Import to import valid8r
import valid8r from '@c4code/valid8r';
Valid8r provides a way to default all your validation configuration globally for your project in on go, you just need to provide all the configuration for all the fields in one place.
Once you’ve configured globally for all the fields, every validation in your project will by default follow that configuration if not provided explicitly.
Here's how you can achieve that, In any of your file in the global context:
import valid8r from '@c4code/valid8r';
Call defaults function in the valid8r and pass an object of all the fields as the first argument that you want to configure:
valid8r.defaults({
name: {
fullNameWithMiddle: true,
allowNumbers: true,
},
email: {
allowedDomains: ['gmail.com', 'outlook.com'],
allowDisposables: false,
customDisposables: ['xyz.com', 'dispmail.com'],
}
}); // defaults to all the validation in the project
Then import the config file to any of the main files for this to work.
import './config.ts';
That's it! Now all the validation functions used in the project will use this as their default configuration, until they are specifically provided with their own configuration. Such as:
import valid8r from '@c4code/valid8r';
// Your input's value
const input: string = "Jonathan Wilson2";
const [isValid, errors] = valid8r.name(input);
console.log(isValid); // true, as allowNumbers is set to true.
const input2: string = "D4nny Wils0n";
valid8r.name(input, { allowNumbers: false });
// throws NameValidationFailed error
The configuration is flexible and can be customized according to the projects need.
If you don't like the default error messages, and want to configure the error messages according to you valid8r provides a simple way to configure every error message efficiently.
import valid8r from '@c4code/valid8r';
Call defaults function in the valid8r and pass an object that of all the field's error messages your want to configure as the second argument.
valid8r.defaults({
/* Field configurations */
}, {
name: {
onlyLast: "Only last name is allowed!",
noSpChars: "Please dont input any special characters!",
},
}); // defaults to all the validation in the project
However, some fields accept dynamic values that get converts to their relative values when the error is thrown,
import valid8r from '@c4code/valid8r';
valid8r.defaults({
/* Field configurations */
}, {
name: {
minLen: "Name should atleast be {minLen} char long!"
},
});
// Your input's value
const input: string = "J";
const [isValid, errors] = valid8r.name(input, {
minLen: 4,
safe: true
});
if (!isValid) {
console.log(errors);
// [{ minLen: "Name should atleast be 4 char long!" }]
}
You can configure all the field's error message to be used as default in any of the validator
If any validation function requires a specific error you can easily set the error message as the third parameter in any validation function
import valid8r from '@c4code/valid8r';
// Your input's value
const input: string = "Jay Carlo$";
const [isValid, errors] = valid8r.name(input, { safe: true }, {
noSpChar: "Special character cannot be included!"
});
if (!isValid) {
console.log(errors);
// [{ noSpChar: "Special character cannot be included!" }]
}
MIT License
Copyright (c) 2024 Kushal Shah
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.