import PayloadTypes from '../types/PayloadTypes.js'; export default class PayloadValidator { static validate(schema, payload, path = '') { for (const field in schema) { const rule = schema[field]; const value = payload[field]; const p = path ? `${path}.${field}` : field; if (rule.is_mandatory_field && value === undefined) { return { valid:false, error:`Missing field ${p}` }; } if (value === undefined) continue; switch (rule.type) { case PayloadTypes.STRING: case PayloadTypes.NUMBER: if (typeof value !== rule.type) return { valid:false, error:`Invalid type ${p}` }; break; case PayloadTypes.ARRAY: case PayloadTypes.JSON_ARRAY: if (!Array.isArray(value)) return { valid:false, error:`Invalid array ${p}` }; if (rule.array_dtype) { for (const v of value) { if (typeof v !== rule.array_dtype) return { valid:false, error:`Invalid array dtype ${p}` }; } } break; case PayloadTypes.JSON_OBJECT: if (typeof value !== 'object' || Array.isArray(value)) return { valid:false, error:`Invalid object ${p}` }; if (rule.json_dtype) { const nested = this.validate(rule.json_dtype, value, p); if (!nested.valid) return nested; } break; } } return { valid:true }; } }