In the previous chapter, we explored Application Statistics, which provide tools for analyzing and optimizing cache performance in the xconfui application. In this chapter, we will focus on Validation Services, a feature that ensures data integrity and correctness across the application by validating rules, formulas, and other entities.
In systems with dynamic configurations and extensive user input, ensuring data correctness is critical. Invalid or inconsistent data can lead to system failures, incorrect configurations, or suboptimal performance. Validation Services provide a centralized mechanism to verify that data meets expected criteria before it is processed or saved.
Consider a scenario where an administrator is creating a new formula to manage device configurations. Before saving the formula:
- The name of the formula must be unique and non-empty.
- Percentage values in the formula should be between 0 and 100.
- Conditions in the formula must be properly defined.
The Validation Services feature provides reusable methods to validate these requirements, ensuring that only correct and complete data is accepted by the system. This prevents errors and simplifies debugging by catching issues early.
Validation Services are built around reusable validation functions that can be applied to various entities. These functions include:
- Name Validation: Ensures names are non-empty and unique.
- Percentage Validation: Checks that percentage values are within valid ranges.
- Condition Validation: Verifies that conditions are properly defined.
Name Validation ensures that entity names are non-empty and do not conflict with existing names.
The validateName function checks whether a name is valid by ensuring it is non-empty and unique.
function validateName(name, usedNames) {
if (utilsService.isNullOrUndefinedOrEmptyOrWhiteSpaceString(name)) {
return { isValid: false, message: 'Name must not be empty' };
}
if (usedNames.indexOf(name) >= 0) {
return { isValid: false, message: 'Such name already exists' };
}
return { isValid: true };
}Explanation:
- If the name is empty or contains only whitespace, an error message is returned.
- If the name already exists in the provided
usedNamesarray, an error message is returned. - If both checks pass, the function returns
isValid: true.
Example Input:
validateName('Formula1', ['Formula2', 'Formula3']);Output:
{
"isValid": true
}Percentage Validation ensures that percentage values are numeric and between 0 and 100.
The validatePercentage function checks whether a percentage is valid.
function validatePercentage(percentage) {
if (!utilsService.isInt(percentage) || percentage < 0 || percentage > 100) {
return { isValid: false, message: 'Percentage must be a number from 0 to 100' };
}
return { isValid: true };
}Explanation:
- If the percentage is not an integer or is outside the range [0, 100], an error message is returned.
- If the percentage is valid, the function returns
isValid: true.
Example Input:
validatePercentage(50);Output:
{
"isValid": true
}Condition Validation ensures that conditions are properly defined before saving rules or formulas.
The validateCondition function checks whether a condition is non-empty and correctly structured.
function validateCondition(rule) {
if (utilsService.isNullOrUndefinedOrEmpty(rule.compoundParts) && !rule.condition) {
return { isValid: false, message: 'Please fill condition' };
}
return { isValid: true };
}Explanation:
- If the
compoundPartsattribute is empty andconditionis not defined, an error message is returned. - If the condition is valid, the function returns
isValid: true.
Example Input:
validateCondition({ condition: 'environment == "Production"' });Output:
{
"isValid": true
}Validation Services are applied to entities like formulas, rules, and filters to ensure data correctness before processing or saving. Here’s a sequence diagram to illustrate the process:
sequenceDiagram
participant User as User
participant Controller as ValidationController
participant Service as ValidationService
participant Utils as UtilsService
User->>Controller: Submit formula data
Controller->>Service: Call validateName, validatePercentage, validateCondition
Service->>Utils: Use helper functions for validation
Utils-->>Service: Return validation results
Service-->>Controller: Provide validation status
Controller-->>User: Display validation errors (if any)
Explanation:
- The user submits data (e.g., a formula) through the controller.
- The controller calls validation functions from the service (e.g.,
validateName). - The service uses helper functions from
utilsServiceto perform specific checks. - Validation results are returned to the controller.
- If errors are detected, the controller displays them to the user.
Let’s dive deeper into the code for Validation Services.
The formulaValidationService is implemented in formula.validation.service.js:
function validateAll(rule, usedNames) {
return validateName(rule.name, usedNames).isValid &&
validatePercentage(rule.percentage).isValid &&
validateCondition(rule).isValid;
}Explanation:
- Combines multiple validation checks (e.g., name, percentage, condition) into a single method.
- Returns
trueonly if all checks pass.
The percentFilterValidationService is implemented in percentfilter.validation.service.js:
function validatePercentFilter(filter) {
return isValidPercentage(filter.percentage);
}Explanation:
- Validates the percentage attribute of a filter using the
isValidPercentagefunction.
The featureRuleValidationService is implemented in featurerule.validation.service.js:
function validateNumberOfFeatures(features, allowedNumberOfFeatures) {
if (features.length === 0 || features.length > allowedNumberOfFeatures) {
return false;
}
return true;
}Explanation:
- Ensures that the number of features in a rule does not exceed the allowed limit.
In this chapter, we explored Validation Services, which ensure data integrity and correctness across the xconfui application. We covered:
- Name Validation: Ensures names are non-empty and unique.
- Percentage Validation: Checks that percentage values are within valid ranges.
- Condition Validation: Verifies conditions are properly defined.
These services provide a robust framework for validating entities like formulas, rules, and filters, preventing errors and ensuring data consistency.
Next, we will explore Change Management, which focuses on tracking and managing changes to application configurations.
Generated by AI Codebase Knowledge Builder