import { checkAssetFieldsExist } from './rewrite.ts'; // zod strict 校验 + 资源路径安全(穿越/缺失)检查 export function validateEntry(input) { const { schema, data, entryDir, cmsRoot } = input; const errors = []; const warnings = []; for (const [locale, value] of Object.entries(data)) { validateWithZod(schema, value, locale, errors); checkAssetFieldsExist(value, entryDir, cmsRoot, errors, locale); } return { errors, warnings }; } // 直接交给 zod 处理(含 ZodArray 逐元素校验 + 下标 path), // 不再手动判断 schema 类型或手写逐元素循环。 function validateWithZod(schema, data, scope, errors) { const result = schema.safeParse(data); if (result.success) return; for (const issue of result.error.issues) { // 顶层"预期数组但数据不是数组",保留旧文案与旧格式(无 # 分隔符) if (issue.code === 'invalid_type' && issue.expected === 'array' && issue.path.length === 0) { errors.push(`[${scope}] 预期数组形态,但数据不是数组`); continue; } errors.push(`[${scope}#${formatPath(issue.path)}] ${issue.message}`); } } // 数值下标 → [i],字符串键 → .key,拼接格式与旧实现保持一致 function formatPath(pathSegments) { let out = ''; for (const seg of pathSegments) { out += typeof seg === 'number' ? `[${seg}]` : `.${seg}`; } return out; }