fix(contracts): 守卫收紧必填字段类型与对象形状;修复包级 test 脚本;补齐守卫测试与 REQUIRED 类型锁
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -20,17 +20,22 @@ export interface UniversalRow {
|
||||
rawLocation: string
|
||||
}
|
||||
|
||||
const REQUIRED: (keyof UniversalRow)[] = [
|
||||
type RequiredKeys<T> = { [K in keyof T]-?: {} extends Pick<T, K> ? never : K }[keyof T]
|
||||
|
||||
// 契约冻结:REQUIRED = 接口必填字段全集;接口新增必填字段而列表漏改 → 编译报错
|
||||
const REQUIRED = [
|
||||
'sourceType', 'sourceId', 'status', 'locale',
|
||||
'taxonomyRefs', 'attachmentRefs', 'seo', 'knownFields', 'extras', 'rawLocation',
|
||||
]
|
||||
] as const
|
||||
|
||||
type _RequiredKeysMatch = [RequiredKeys<UniversalRow>] extends [typeof REQUIRED[number]] ? true : never
|
||||
const _requiredKeysComplete: _RequiredKeysMatch = true
|
||||
|
||||
export function isUniversalRow(v: unknown): v is UniversalRow {
|
||||
if (typeof v !== 'object' || v === null) return false
|
||||
const row = v as Record<string, unknown>
|
||||
return REQUIRED.every((k) => k in row)
|
||||
&& Array.isArray(row.taxonomyRefs) && Array.isArray(row.attachmentRefs)
|
||||
&& row.seo !== null && typeof row.seo === 'object'
|
||||
&& row.knownFields !== null && typeof row.knownFields === 'object'
|
||||
&& row.extras !== null && typeof row.extras === 'object'
|
||||
&& ['sourceType','sourceId','status','locale','rawLocation'].every((k) => typeof row[k] === 'string')
|
||||
&& [row.seo, row.knownFields, row.extras].every((x) => typeof x === 'object' && x !== null && !Array.isArray(x))
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user