Const
// Valid UUID parameter validation
const validUuid = { id: '123e4567-e89b-12d3-a456-426614174000' };
const result = UuidParamSchema.parse(validUuid);
console.log(result.id); // Valid UUID string
// Route parameter validation for UUID-based resources
router.get('/users/:id', validate({ params: UuidParamSchema }), (req, res) => {
const { id } = req.params; // Guaranteed to be valid UUID
const user = await userService.findById(id);
if (!user) {
return res.status(404).json({ error: 'User not found' });
}
res.json({ user });
});
UUID parameter validation schema for strict identifier validation
Strict identifier validation schema that enforces UUID format compliance for route parameters. This schema is essential for endpoints that use UUID-based identifiers, providing strong validation to ensure only properly formatted UUIDs are accepted and preventing malformed identifier attacks.
UUID validation provides several security and reliability benefits including prevention of SQL injection attacks through strict format enforcement, elimination of malformed identifier errors that could cause application crashes, and consistent identifier format that supports distributed systems and database replication scenarios.
The schema validates against the standard UUID format (8-4-4-4-12 hexadecimal digits) and rejects any input that doesn't conform to this pattern. This strict validation is particularly important for APIs that expose database primary keys or need to maintain referential integrity across distributed services.