This document serves as a comprehensive guide for developers working on the Validation Engine. It explains how to use, extend, and maintain the library as part of the software ecosystem. Additionally, it provides an in-depth explanation of how field validation and the validation controller work.
- Development Guide for Validation Engine
- Node.js: v16 or later.
- Web Browser: Modern browsers (e.g., Chrome, Firefox, Edge) for testing.
Clone the project directory to your local environment:
cp -r /shared/-repo/ValidationEngine .
cd ValidationEngineValidationEngine/
„¥„Ÿ„Ÿ src/ # Source files
„ „¥„Ÿ„Ÿ models/ # Validation models (e.g., ValidationError, ValidationResult)
„ „¥„Ÿ„Ÿ service/ # Core services (e.g., FieldValidator, ValidationController)
„ „¥„Ÿ„Ÿ utils/ # Utility functions and helper classes
„¥„Ÿ„Ÿ tests/ # Unit tests
„¥„Ÿ„Ÿ dist/ # Bundled and minified files for browsers
„¥„Ÿ„Ÿ webpack.config.js # Webpack configuration for bundling
„¤„Ÿ„Ÿ README.md # User documentation
Field validation is the process of applying one or more validation conditions to a specific form field. It ensures the field meets the requirements defined in the validation rules.
-
Validation Rules: Each field has associated rules defining conditions like "Required," "Length Check," "Regex Match," etc.
-
FieldValidator: The
FieldValidatorservice contains methods for validating individual conditions. -
Example Flow:
- A rule is defined for the
emailfield:{ fieldId: "email", conditions: [ { type: "REQUIRED", errorMessage: "Email is required" }, { type: "REGEX", regex: "^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$", errorMessage: "Invalid email format" } ] }
- During validation, each condition is checked using
FieldValidatormethods:const field = { fieldId: "email", value: "john.doe@example.com" }; const condition = { type: "REQUIRED" }; const result = FieldValidator.validateRequired(field, condition); if (result) { logger.error("Validation error:", result.message); }
- A rule is defined for the
validateRequired(field, condition): Ensures the field is not empty.validateLength(field, condition): Checks if the field value length is within a specified range.validateRegex(field, condition): Validates the field value against a regex pattern.validateDependency(field, condition, formData): Ensures the field meets conditions based on the values of other fields.
The ValidationController orchestrates the validation process. It applies the rules defined for each field and aggregates the results.
-
Load Rules: The controller takes the form data and validation rules as input.
-
Apply Field Validation: It uses
FieldValidatorto validate each field against its conditions. -
Aggregate Results: Errors are collected into a
ValidationResultobject, which can be returned to the caller.
Here�fs how the ValidationController validates a form:
const formData = {
email: "john.doe@example",
password: "1234"
};
const rules = [
{
fieldId: "email",
conditions: [
{ type: "REQUIRED", errorMessage: "Email is required" },
{ type: "REGEX", regex: "^[^\\s@]+@[^\\s@]+\\.[^\\s@]+$", errorMessage: "Invalid email format" }
]
},
{
fieldId: "password",
conditions: [
{ type: "REQUIRED", errorMessage: "Password is required" },
{ type: "LENGTH_CHECK", minLength: 8, errorMessage: "Password must be at least 8 characters long" }
]
}
];
const controller = new ValidationController();
controller.validateForm(formData, rules)
.then(result => {
if (result.hasErrors) {
logger.error("Validation errors:", result.details);
} else {
logger.log("Validation passed!");
}
});validateForm(formData, rules): Validates the entire form based on the provided rules.applyRule(field, rule): Validates a single field against a single rule.
Use the following commands during development:
npm testnpm run build-
Identify the Module:
- For field-specific logic, update
FieldValidator. - For overall rule application, update
ValidationController.
- For field-specific logic, update
-
Write the Feature:
- Add the new functionality in the appropriate file.
- Follow the existing modular structure.
-
Test the Feature:
- Add test cases in
/testsfor the new functionality.
- Add test cases in
-
Create a test file in
/tests:touch tests/new-feature.test.js
-
Write test cases:
describe("New Feature", () => { test("should validate custom logic", () => { const result = newFeatureFunction(); expect(result).toBe(expectedValue); }); });
-
Run tests:
npm test
Use Webpack to bundle the library:
npm run buildThe output will be generated in /dist/validation-engine.min.js.