Press n or j to go to the next uncovered block, b, p or k for the previous block.
| 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 | 50x 8x 42x 2x 40x 16x 2x 14x 1x 13x 1x 12x 4x 4x 2x 10x 8x 8x 1x 7x 3x 4x 1x 3x 6x 4x 3x 3x 1x 1x 4x 4x 2x 2x 6x 1x 5x 1x 4x 1x 3x 22x 3x 3x 2x 20x 49x 49x 49x 50x 50x 50x 28x 28x 28x 49x 7x 7x 4x 2x 2x 2x 2x | /**
* Validation logic
* Core logic - runtime agnostic
*/
import { ValidationError } from '../../shared/errors.js';
import type { ValidationSchema, ValidationResult, ValidationRule } from './types.js';
function validateField(
value: unknown,
fieldName: string,
rule: ValidationRule
): string | null {
// Check required
if (rule.required && (value === undefined || value === null || value === '')) {
return `${fieldName} is required`;
}
// If not required and value is empty, skip other validations
if (!rule.required && (value === undefined || value === null || value === '')) {
return null;
}
// Type validation
switch (rule.type) {
case 'string':
if (typeof value !== 'string') {
return `${fieldName} must be a string`;
}
if (rule.min !== undefined && value.length < rule.min) {
return `${fieldName} must be at least ${rule.min} characters`;
}
if (rule.max !== undefined && value.length > rule.max) {
return `${fieldName} must be at most ${rule.max} characters`;
}
if (rule.pattern) {
const regex = typeof rule.pattern === 'string' ? new RegExp(rule.pattern) : rule.pattern;
if (!regex.test(value)) {
return `${fieldName} does not match the required pattern`;
}
}
break;
case 'number':
const numValue = typeof value === 'string' ? Number(value) : value;
if (typeof numValue !== 'number' || isNaN(numValue)) {
return `${fieldName} must be a number`;
}
if (rule.min !== undefined && numValue < rule.min) {
return `${fieldName} must be at least ${rule.min}`;
}
if (rule.max !== undefined && numValue > rule.max) {
return `${fieldName} must be at most ${rule.max}`;
}
break;
case 'boolean':
if (typeof value !== 'boolean') {
// Try to convert string to boolean
if (typeof value === 'string') {
const lower = value.toLowerCase();
if (lower !== 'true' && lower !== 'false') {
return `${fieldName} must be a boolean`;
}
} else {
return `${fieldName} must be a boolean`;
}
}
break;
case 'object':
if (typeof value !== 'object' || value === null || Array.isArray(value)) {
return `${fieldName} must be an object`;
}
break;
case 'array':
if (!Array.isArray(value)) {
return `${fieldName} must be an array`;
}
if (rule.min !== undefined && value.length < rule.min) {
return `${fieldName} must have at least ${rule.min} items`;
}
if (rule.max !== undefined && value.length > rule.max) {
return `${fieldName} must have at most ${rule.max} items`;
}
break;
}
// Custom validation
if (rule.custom) {
const customResult = rule.custom(value);
if (customResult !== true) {
return typeof customResult === 'string' ? customResult : `${fieldName} is invalid`;
}
}
return null;
}
export function validate(
data: unknown,
schema: ValidationSchema
): ValidationResult {
const errors: Record<string, string[]> = {};
const obj = data !== null && typeof data === 'object' && !Array.isArray(data)
? (data as Record<string, unknown>)
: {};
for (const [fieldName, rule] of Object.entries(schema)) {
const value = obj[fieldName];
const error = validateField(value, fieldName, rule);
if (error) {
Eif (!errors[fieldName]) {
errors[fieldName] = [];
}
errors[fieldName].push(error);
}
}
return {
valid: Object.keys(errors).length === 0,
errors: Object.keys(errors).length > 0 ? errors : undefined,
};
}
export function validateOrThrow(data: unknown, schema: ValidationSchema): void {
const result = validate(data, schema);
if (!result.valid) {
throw new ValidationError('Validation failed', result.errors);
}
}
export function validateBody<T>(body: unknown, schema: ValidationSchema): T {
validateOrThrow(body, schema);
return body as T;
}
export function validateQuery<T>(query: unknown, schema: ValidationSchema): T {
validateOrThrow(query, schema);
return query as T;
}
|