epic-validator
v3.2.0
Published
A simple but powerful Data Validator
Downloads
183
Readme
Epic Validator
A simple but powerful Data Validator Library. This library will allow you to validate data with less code and more power. You can use this library with any framework.
Usage
Epic Validator is very easy to use library. View the documentation below to start working with Epic Validator quickly!
Validate a value
Follow the below method to validate a value
import { Validation } from "epic-validator";
// Create a Validator Instance.
// You can also pass an object containing your custom validators
const Validator = new Validation({
// Your custom IP Address validator
isIP: (_) =>
_.matches(
/^((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$/,
"Please provide a valid IP Address!"
),
// Your custom IBAN validator
isIBAN: (_) =>
_.matches(
/^([A-Z]{2}[ -]?[0-9]{2})(?=(?:[ -]?[A-Z0-9]){9,30}$)((?:[ -]?[A-Z0-9]{3,5}){2,7})([ -]?[A-Z0-9]{1,3})?$/,
"Invalid IBAN Number has been provided!"
),
});
(async () => {
// Example 1:
const Username = "john";
// Validate Value
await Validator.validate(Username)
.isAlphanumeric(
{
allowSpaces: false, // Set to True if spaces allowed.
strict: true, // Set to False if symbols allowed.
},
// Provide a Validation message
"Please provide a valid Username!"
)
/**
* Use exec() method to run the validator and throw the errors if any.
* You can also use run() method but if there are any errors, you will need to throw errors manually.
* You can use Validator.throw() method to throw any errors manually.
*/
.exec();
// Example 2:
const Email = "[email protected]";
// Validate Email
await Validator.validate(Email)
.isEmail("Please provide a valid Email!")
.run();
// Throw Errors if any
Validator.throw();
// Example 3:
const Contact = 092384758399;
// Validate Contact using custom() method
await Validator.validate(Contact)
.custom((value, chain) => {
if (typeof value !== "number")
throw "Please provide a valid Contact Number!";
})
.exec();
// Example 4:
const IP = "192.168.2.1";
// Validate IP Address using your custom validator
await Validator.validate(IP).use("isIP").exec();
})();
Validate an Array of Values
You can easily validate an Array of Values using the method below.
(async () => {
const Emails = ["[email protected]", "abc", "@mail.com"];
// Validate All Emails in the Array
await Validator.validate(Emails)
.each((_) => _.isEmail("Please provide a valid Email!"))
.exec();
})();
Validate a Schema
It is recommended to validate your data using the schema() method. Follow the method below to validate data schema.
(async () => {
// Dummy Data To Validate
const Data = {
username: "john",
password: "john123",
confirmPassword: "john123",
contact: 03056762589,
agreement: true,
};
// Validate Schema
await Validator.validate(Data).schema({
username: _ => _.isAlphanumeric({}, "Please provide a valid Username!"),
password: _ => _.isString("Please provide a valid Password!").not().isEmpty({}, "Password cannot be empty!"),
confirmPassword: _ => _.isString("Please provide a valid Confirmation Password!").matches(Data.password, "The Confirmation Password doesn't matches your Password!"),
contact: _ => _.isNumeric({
sanitize: true, // Set to True for converting "123" to 123 or "100.2" to 100.2
}, "Please provide a valid Contact Number!"),
agreement: _ => _.likeBoolean({
sanitize: true,
isTrue: true
}, "You need to agree our terms!")
}).exec();
})();
Available Validators
The followings are the available Validators.
- isString
- isNumber
- isBoolean
- isObject
- isArray
- isFunction
- isBigInt
- isUndefined
- isSymbol
- isJson
- likeObject
- likeArray
- isEmpty
- notEmpty
- isIn
- matches
- isAlpha
- isAlphanumeric
- isNumeric
- isEmail
- isIpv4
- isIpv6
- isLength
- isAmount
- isURL
- likeBoolean
Available Utility Methods
The followings are the available Utility Methods.
- custom
- optional
- required
- not
- skip
- schema
- each
- use
- error
We have tried our best to make the documentation as simple as possible. Good Luck!