@dbs-portal/core-api
Version:
HTTP client and API utilities for DBS Portal
232 lines • 6.38 kB
JavaScript
/**
* Validation utilities for mock handlers
*/
/**
* Validate required field
*/
export function validateRequired(value, fieldName) {
if (value === undefined || value === null || value === '') {
return `${fieldName} is required`;
}
return null;
}
/**
* Validate email format
*/
export function validateEmail(value, fieldName = 'Email') {
if (!value)
return null; // Use required validator for required check
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(value)) {
return `${fieldName} must be a valid email address`;
}
return null;
}
/**
* Validate minimum length
*/
export function validateMinLength(value, minLength, fieldName) {
if (!value)
return null; // Use required validator for required check
if (value.length < minLength) {
return `${fieldName} must be at least ${minLength} characters long`;
}
return null;
}
/**
* Validate maximum length
*/
export function validateMaxLength(value, maxLength, fieldName) {
if (!value)
return null; // Use required validator for required check
if (value.length > maxLength) {
return `${fieldName} must be no more than ${maxLength} characters long`;
}
return null;
}
/**
* Validate pattern
*/
export function validatePattern(value, pattern, fieldName, message) {
if (!value)
return null; // Use required validator for required check
if (!pattern.test(value)) {
return message || `${fieldName} format is invalid`;
}
return null;
}
/**
* Validate object against rules
*/
export function validateObject(data, rules) {
const errors = [];
for (const rule of rules) {
const value = data[rule.field];
for (const validation of rule.rules) {
let error = null;
switch (validation.type) {
case 'required':
error = validateRequired(value, rule.field);
break;
case 'email':
error = validateEmail(value, rule.field);
break;
case 'minLength':
error = validateMinLength(value, validation.value, rule.field);
break;
case 'maxLength':
error = validateMaxLength(value, validation.value, rule.field);
break;
case 'pattern':
error = validatePattern(value, validation.value, rule.field, validation.message);
break;
case 'custom':
if (validation.validator && !validation.validator(value)) {
error = validation.message || `${rule.field} is invalid`;
}
break;
}
if (error) {
errors.push(error);
}
}
}
return {
isValid: errors.length === 0,
errors,
};
}
/**
* Common validation rule sets
*/
export const commonValidationRules = {
/**
* User registration validation
*/
userRegistration: [
{
field: 'email',
rules: [
{ type: 'required' },
{ type: 'email' },
],
},
{
field: 'password',
rules: [
{ type: 'required' },
{ type: 'minLength', value: 8, message: 'Password must be at least 8 characters long' },
],
},
{
field: 'firstName',
rules: [
{ type: 'required' },
{ type: 'maxLength', value: 50 },
],
},
{
field: 'lastName',
rules: [
{ type: 'required' },
{ type: 'maxLength', value: 50 },
],
},
],
/**
* Login validation
*/
login: [
{
field: 'email',
rules: [
{ type: 'required' },
{ type: 'email' },
],
},
{
field: 'password',
rules: [
{ type: 'required' },
],
},
],
/**
* Profile update validation
*/
profileUpdate: [
{
field: 'firstName',
rules: [
{ type: 'maxLength', value: 50 },
],
},
{
field: 'lastName',
rules: [
{ type: 'maxLength', value: 50 },
],
},
{
field: 'email',
rules: [
{ type: 'email' },
],
},
],
};
/**
* Create a validator function from rules
*/
export function createValidator(rules) {
return (data) => {
const result = validateObject(data, rules);
return result.errors;
};
}
/**
* Validate file upload
*/
export function validateFileUpload(file, options = {}) {
const errors = [];
const { maxSize, allowedTypes, allowedExtensions } = options;
// Check file size
if (maxSize && file.size > maxSize) {
errors.push(`File size must be less than ${Math.round(maxSize / 1024 / 1024)}MB`);
}
// Check file type
if (allowedTypes && !allowedTypes.includes(file.type)) {
errors.push(`File type ${file.type} is not allowed`);
}
// Check file extension
if (allowedExtensions) {
const extension = file.name.split('.').pop()?.toLowerCase();
if (!extension || !allowedExtensions.includes(extension)) {
errors.push(`File extension .${extension} is not allowed`);
}
}
return {
isValid: errors.length === 0,
errors,
};
}
/**
* Sanitize input data
*/
export function sanitizeInput(data) {
const sanitized = {};
for (const [key, value] of Object.entries(data)) {
if (typeof value === 'string') {
// Basic HTML sanitization (remove script tags, etc.)
sanitized[key] = value
.replace(/<script\b[^<]*(?:(?!<\/script>)<[^<]*)*<\/script>/gi, '')
.replace(/<iframe\b[^<]*(?:(?!<\/iframe>)<[^<]*)*<\/iframe>/gi, '')
.trim();
}
else {
sanitized[key] = value;
}
}
return sanitized;
}
//# sourceMappingURL=validation.js.map