| FazBrowse GitHub Viewer | Trending | | Home |
| Tools: [Download Repo ZIP] [Original HTTPS Page] |
| Name | Name | Last commit date | ||
|---|---|---|---|---|
JS Validator is a robust JavaScript library designed for validating data inputs across applications. It offers a versatile and effective approach to ensuring data integrity, making it suitable for various scenarios, from form validation to complex rule-based data checks.
You can easily integrate JS Validator into your project by installing it through npm:
npm install @vmgware/js-validatorThis example demonstrates how to validate a simple user registration form:
const Validator = require("@vmgware/js-validator");
// Define validation rules for user data
const rules = {
username: { type: "string", required: true, min: 3 },
email: { type: "string", required: true, validate: "email" },
age: { type: "number", min: 18 },
};
// Define custom error messages for validation failures
const messages = {
username: {
required: "Username is required",
min: "Username must be at least 3 characters long",
},
email: {
required: "Email is required",
validate: "Please enter a valid email address",
},
age: {
min: "You must be at least 18 years old",
},
};
// Initialize the validator with the defined rules and messages
const validator = new Validator(rules, messages);
// Sample user data to be validated
const userData = {
username: "johndoe",
email: "john.doe@example.com",
age: 25,
};
// Validate the user data
validator.validate(userData).then(isValid => {
if (isValid) {
console.log("User data is valid");
} else {
console.error("Validation errors:", validator.getErrors());
}
});In this TypeScript example, we validate a user profile form, leveraging TypeScript's type safety:
import Validator from "@vmgware/js-validator";
interface UserProfile {
username: string;
email: string;
birthdate: Date;
}
// Validation rules according to the UserProfile interface
const rules = {
username: { type: "string", required: true, min: 3 },
email: { type: "string", required: true, validate: "email" },
birthdate: { type: "date", required: true },
};
// Custom error messages for validation failures
const messages = {
username: {
required: "Username is required",
min: "Username must be at least 3 characters long",
},
email: {
required: "Email is required",
validate: "Please enter a valid email address",
},
birthdate: {
required: "Birthdate is required",
},
};
// Initialize the validator with rules and messages tailored for UserProfile
const validator = new Validator<UserProfile>(rules, messages);
// Sample user profile data to be validated
const userProfile: UserProfile = {
username: "janedoe",
email: "jane.doe@example.com",
birthdate: new Date("1990-04-15"),
};
// Validate the user profile
validator.validate(userProfile).then(isValid => {
if (isValid) {
console.log("User profile is valid");
} else {
console.error("Validation errors:", validator.getErrors());
}
});Custom error messages provide clear, user-friendly feedback, helping users correct their inputs effectively.
Asynchronous validation is particularly useful for checks that require external data, such as verifying if a username is available:
// Asynchronous function to check if a username is available
async function isUsernameAvailable(username) {
// Simulate an API call to check username availability
// Returns true if available
}
// Add asynchronous custom validation to the username field
rules.username.custom = async username => await isUsernameAvailable(username);
// Define a custom error message for the username availability check
messages.username.custom = "This username is already taken";
// Validation process remains the same as shown in previous examplesStrict mode ensures that the input strictly adheres to the defined rules, rejecting any extraneous fields:
// Enable strict mode in the validator options
const options = { strictMode: true };
// Initialize the validator with rules, messages, and options
const validator = new Validator(rules, messages, options);
// With strict mode, any fields not defined in the rules will result in a validation errorContributions are welcome! Please follow the Conventional Commits standard for commit messages.
JS Validator is open-sourced under the MIT License. See the LICENSE file for more details.
JS Validator is designed for developers seeking a straightforward yet versatile validation library for their JavaScript or TypeScript projects. Whether you're validating simple forms or complex data structures, JS Validator provides the tools you need to ensure data integrity with minimal effort.
| Back | FazBrowse Home | New Git URL |