UNPKG

epic-validator

Version:
180 lines (144 loc) 4.4 kB
# 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 ```ts 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 = "john@gmail.com"; // 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. ```ts (async () => { const Emails = ["john@gmail.com", "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. ```ts (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. 1. isString 2. isNumber 3. isBoolean 4. isObject 5. isArray 6. isFunction 7. isBigInt 8. isUndefined 9. isSymbol 10. isJson 11. likeObject 12. likeArray 13. isEmpty 14. notEmpty 15. isIn 16. matches 17. isAlpha 18. isAlphanumeric 19. isNumeric 20. isEmail 21. isIpv4 22. isIpv6 23. isLength 24. isAmount 25. isURL 26. likeBoolean #### Available Utility Methods The followings are the available Utility Methods. 1. custom 2. optional 3. required 4. not 5. skip 6. schema 7. each 8. use 9. error We have tried our best to make the documentation as simple as possible. Good Luck!