|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
export class SchemaValidator { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static validate(schema: Record<string, unknown>, data: unknown): boolean { |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (schema.required && Array.isArray(schema.required)) { |
|
|
const required = schema.required as string[]; |
|
|
const dataObj = data as Record<string, unknown>; |
|
|
|
|
|
for (const field of required) { |
|
|
if (dataObj[field] === undefined) { |
|
|
console.error(`Missing required field: ${field}`); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
if (schema.properties && typeof schema.properties === 'object') { |
|
|
const properties = schema.properties as Record<string, { type?: string }>; |
|
|
const dataObj = data as Record<string, unknown>; |
|
|
|
|
|
for (const [key, prop] of Object.entries(properties)) { |
|
|
if (dataObj[key] !== undefined && prop.type) { |
|
|
const expectedType = prop.type; |
|
|
const actualType = Array.isArray(dataObj[key]) |
|
|
? 'array' |
|
|
: typeof dataObj[key]; |
|
|
|
|
|
if (expectedType !== actualType) { |
|
|
console.error( |
|
|
`Type mismatch for property "${key}": expected ${expectedType}, got ${actualType}`, |
|
|
); |
|
|
return false; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
return true; |
|
|
} |
|
|
} |
|
|
|