A flexible and enterprise-ready validation framework designed to handle complex form validations. It supports robust validation types, dependency checks, and customizable workflows.
- Extensive Validation Types: Includes
REQUIRED,COMPARISON,DEPENDENCY,REGEX,TYPE_CHECK, and more. - Dependency Validation: Ensures fields are validated based on related fields.
- Asynchronous Validation: Supports async operations and webhooks for dynamic validations.
- Custom Operators: Allows you to define your own operators for specific use cases.
- Rule Inheritance: Enables reusable and extendable rule templates.
- Multi-Language Support: Fully supports English and Japanese.
- Performance Metrics: Tracks and optimizes validation performance.
- Customizable Logging: Provides detailed logs with adjustable output formats.
- Schema Validation: Validates JSON and YAML rule definitions.
- Standalone or NPM Module: Usable in both environments seamlessly.
npm install @shanewas/form-validationInclude the compiled JavaScript file in your project:
<script src="validation-engine.min.js"></script>Validation rules are defined in JSON format:
{
"rules": [
{
"ruleId": "rule-1",
"groupId": "group-1",
"ruleName": "Age Validation",
"priority": 1,
"conditions": [
{
"pdfId": "form-1",
"fieldId": "age",
"fieldType": "number",
"type": "COMPARISON",
"operator": "GREATER_THAN",
"value": 18,
"errorMessage": "Age must be greater than 18"
}
]
}
]
}import { ValidationController } from "@shanewas/form-validation";
const formData = { age: 16 };
const rules = [ /* JSON Rules */ ];
const controller = new ValidationController();
controller
.validateForm(formData, rules)
.then((result) => logger.log(result))
.catch((error) => logger.error(error));<script>
const formData = { age: 16 };
const rules = [ /* JSON Rules */ ];
const controller = new ValidationController();
controller.validateForm(formData, rules)
.then((result) => logger.log(result))
.catch((error) => logger.error(error));
</script>Dependency validation ensures that a field is validated based on the value of another field. For example, if a guardian’s name is required when the applicant is a minor.
Define a dependency rule:
{
"rules": [
{
"ruleId": "dependency-rule-1",
"groupId": "group-1",
"ruleName": "Guardian Dependency Rule",
"priority": 1,
"conditions": [
{
"pdfId": "form-1",
"fieldId": "guardianName",
"fieldType": "text",
"type": "DEPENDENCY",
"dependentFieldId": "applicantAge",
"dependentOperator": "LESS_THAN",
"dependentValue": 18,
"errorMessage": "Guardian name is required for minors."
}
]
}
]
}const formData = {
applicantAge: 16, // Dependent field
guardianName: "" // Target field
};
const rules = [ /* Dependency rule defined above */ ];
const controller = new ValidationController();
controller
.validateForm(formData, rules)
.then((result) => {
if (result.hasErrors) {
logger.error("Validation failed:", result.details);
} else {
logger.log("Validation succeeded!");
}
})
.catch((error) => logger.error("System error:", error));Add custom operators using ValidationUtils:
ValidationUtils.addCustomOperator("INCLUDES", (value, comparisonValue) => {
return Array.isArray(comparisonValue) && comparisonValue.includes(value);
});You can validate asynchronously with external services:
{
"type": "CUSTOM",
"validate": async (field, context) => {
const response = await fetch("https://api.example.com/validate", {
method: "POST",
body: JSON.stringify({ field })
});
return response.ok ? null : "Validation failed";
}
}Define reusable templates:
{
"template": "ageValidation",
"fields": ["age"],
"conditions": [
{
"operator": "GREATER_THAN",
"value": 18,
"errorMessage": "Age must be greater than 18"
}
]
}Provide errors in multiple languages:
{
"errorMessage": {
"en": "Age must be greater than 18",
"jp": "年齢は18歳以上でなければなりません"
}
}Configure logging output:
import { ValidationLogger } from "@shanewas/form-validation";
ValidationLogger.configure({
level: "debug", // "info", "warn", "error"
output: "file", // "console", "file"
filePath: "/logs/validation.log"
});validateForm(formData, rules): Validates form data against rules.reset(): Resets internal state for reuse.
| Type | Description |
|---|---|
REQUIRED |
Ensures the field is filled. |
COMPARISON |
Validates using operators. |
DEPENDENCY |
Validates based on related field values. |
TYPE_CHECK |
Ensures the value matches a type. |
REGEX |
Validates using regular expressions. |
Here’s the complete list of operators with their descriptions, as defined in your system:
| Operator | Description |
|---|---|
EQUALS |
Checks if the field value is equal to the comparison value. |
NOT_EQUALS |
Checks if the field value is not equal to the comparison value. |
GREATER_THAN |
Checks if the field value is greater than the comparison value. |
LESS_THAN |
Checks if the field value is less than the comparison value. |
GREATER_THAN_OR_EQUAL |
Checks if the field value is greater than or equal to the comparison value. |
LESS_THAN_OR_EQUAL |
Checks if the field value is less than or equal to the comparison value. |
CONTAINS |
Checks if the field value contains the comparison value as a substring. |
STARTS_WITH |
Checks if the field value starts with the comparison value. |
ENDS_WITH |
Checks if the field value ends with the comparison value. |
BETWEEN |
Checks if the field value lies within the range of two values (inclusive). |
BEFORE |
Checks if the field value is before the comparison date (for date values). |
AFTER |
Checks if the field value is after the comparison date (for date values). |
EMPTY |
Checks if the field value is empty (null, undefined, or ""). |
NOT_EMPTY |
Checks if the field value is not empty. |
Track performance with detailed insights:
ValidationEngine.enableMetrics();
const metrics = ValidationEngine.getMetrics();
logger.log(metrics);Run tests for your validation rules:
npm test- Node.js: Version 16 or later.
- Browser Support: ES Modules and modern browsers.
We welcome contributions! Please see our Contributing Guidelines.
This project is licensed under the GNU General Public License v3.0. See the LICENSE file for details.