Junior
In your opinion, what does validation mean?
sobes.tech AI
Answer from AI
Checking data compliance with predefined rules, standards, or formats. The goal is to ensure correctness, completeness, and integrity of input data, preventing system errors related to incorrect or invalid data. Validation can be performed at various levels: Frontend, Backend, and Database.
Main aspects of validation:
- Format: Checking if data matches the expected format (e.g., email, phone number, date).
- Range: Checking if numerical values fall within an acceptable range.
- Data type: Ensuring data has the expected type (string, number, boolean, etc.).
- Mandatory fields: Checking for the presence of required fields.
- Related constraints: Validating data based on their relationship with other data (e.g., end date should not be earlier than start date).
- Uniqueness: Ensuring the field value is unique within a certain dataset.
Example of backend validation (Node.js + Express + Express-validator):
const { body, validationResult } = require('express-validator');
// Middleware for validating email and password
const validateUser = [
body('email')
.isEmail().withMessage('Invalid email format') // Email format check
.normalizeEmail(), // Email normalization
body('password')
.isLength({ min: 6 }).withMessage('Password must be at least 6 characters long') // Minimum length check
.matches(/\d/).withMessage('Password must contain a digit') // Digit presence check
.matches(/[A-Z]/).withMessage('Password must contain an uppercase letter'), // Uppercase letter check
(req, res, next) => {
const errors = validationResult(req);
if (!errors.isEmpty()) {
return res.status(400).json({ errors: errors.array() }); // Return validation errors
}
next(); // Proceed if validation passes
}
];
// Use middleware in route
// app.post('/users', validateUser, (req, res) => { /* User creation logic */ });
Difference between validation and verification:
| Aspect | Validation | Verification |
|---|---|---|
| Purpose | Checking compliance with rules and formats. | Checking adherence to requirements and specifications. |
| Focus | Input data correctness. | Overall product/system correctness. |
| When | Usually early in data processing. | Throughout the entire development lifecycle. |
| How | Format, type, range checks. | Functionality, performance testing. |
| Question | "Does the data comply with rules?" | "Is the product made correctly?" |
Validation is an important part of QA, as it helps identify errors early, reducing time and costs for fixing them.