express-routes-validator
v1.0.20
Published
A package for request validation middleware and validators
Downloads
153
Readme
Powerful Validators for Node.js
A comprehensive set of reusable and customizable validation functions for your Node.js applications.
Ensure clean, safe, and validated input data with ease.
Features
- Easy-to-use and customizable validators
- Supports validation for strings, numbers, arrays, emails, dates, and more
- Designed for use with Express.js middleware
- Extendable with custom validation logic
Installation
Install the package via npm:
npm install your-package-name
Available Validators
Here is a complete list of the validators available in this package:
1. isString
Validates that a value is a string.
isString(value, key);
2. isNumber
Validates that a value is a number.
isNumber(value, key);
3. isRequired
Validates that the value is neither null
nor undefined
.
isRequired(value, key);
4. maxLength
Validates that a string does not exceed a maximum length.
maxLength(max)(value, key);
5. minLength
Validates that a string is at least a minimum length.
minLength(min)(value, key);
6. isEmail
Validates that the value is a valid email address.
isEmail(value, key);
7. minValue
Validates that a numeric value is greater than or equal to a specified minimum value.
minValue(min)(value, key);
8. maxValue
Validates that a numeric value is less than or equal to a specified maximum value.
maxValue(max)(value, key);
9. matchesPattern
Validates that the value matches a given regular expression.
matchesPattern(pattern)(value, key);
10. customValidator
Allows you to define a custom validation function.
customValidator(fn)(value, key);
11. isArray
Validates that the value is an array.
isArray(value, key);
12. trimString
Trims whitespace from the beginning and end of a string.
trimString(value, key);
13. arrayElementsValidator
Validates the elements inside an array using provided validators.
arrayElementsValidator(elementValidators)(value, key);
14. objectKeysValidator
Validates the keys of an object using provided key validators.
objectKeysValidator(keyValidators)(value, key);
15. isBoolean
Validates that the value is a boolean.
isBoolean(value, key);
16. isObject
Validates that the value is an object.
isObject(value, key);
17. arrayMaxLength
Validates that an array does not exceed a maximum length.
arrayMaxLength(max)(value, key);
18. arrayMinLength
Validates that an array contains at least a minimum number of elements.
arrayMinLength(min)(value, key);
19. isKeyInObject
Validates that a specific key exists in an object.
isKeyInObject(requiredKey)(value, key);
20. isAlpha
Validates that the value only contains alphabetic characters (A-Z, a-z).
isAlpha(value, key);
21. isAlphanumeric
Validates that the value only contains alphanumeric characters (A-Z, a-z, 0-9).
isAlphanumeric(value, key);
22. isLowercase
Validates that the value is in lowercase.
isLowercase(value, key);
23. isUppercase
Validates that the value is in uppercase.
isUppercase(value, key);
24. isLength
Validates that the length of a string is within a specified range.
isLength(min, max)(value, key);
25. isInteger
Validates that the value is an integer.
isInteger(value, key);
26. isPositive
Validates that the value is a positive number.
isPositive(value, key);
27. isNegative
Validates that the value is a negative number.
isNegative(value, key);
28. isDate
Validates that the value is a valid date.
isDate(value, key);
29. isFutureDate
Validates that the date is in the future.
isFutureDate(value, key);
30. isPastDate
Validates that the date is in the past.
isPastDate(value, key);
31. isBeforeDate
Validates that the date is before a given date.
isBeforeDate(date)(value, key);
32. isEmailUnique
Validates that the email is unique in a given list of emails (asynchronous).
isEmailUnique(key, value, existingEmails);
33. isDateInRange
Validates that the date is within a specified range.
isDateInRange(startDate, endDate)(value, key);
34. isCountryCode
Validates that the value is a valid two-letter country code.
isCountryCode(value, key);
35. isMongoId
Validates that the value is a valid MongoDB ObjectId.
isMongoId(value, key);
36. capitalizeString
Capitalizes the first letter of each word in a string.
capitalizeString(value, key);
Example Usage
Here’s a simple example of how to use these validators:
import express from "express";
import { validateRequest } from "express-routes-validator";
const app = express();
const options = {
"/register": {
username: [isRequired, minLength(3), maxLength(20)],
email: [isRequired, isEmail, capitalizeString],
},
"/update-profile": {
age: [isRequired, isNumber, minValue(18)],
},
"/search": {
query: [isRequired, isString, maxLength(50)],
},
};
app.use(validateRequest(options));
app.post("/register", (req, res) => {
res.send("Registered successfully!");
});
app.put("/update-profile", (req, res) => {
res.send("Profile updated successfully!");
});
app.get("/search", (req, res) => {
res.send("Search results");
});
app.listen(3000, () => {
console.log("Server running on port 3000");
});
Contributing
Feel free to fork and submit pull requests. All contributions are welcome! If you find any issues or would like to suggest a feature, please create an issue on GitHub.
License
This package is licensed under the MIT License. See the LICENSE file for more details.