gongxiangp2/lib/task-detail-privacy-fields.ts

57 lines
1.7 KiB
TypeScript

export const TASK_DETAIL_DEMAND_TYPE_OPTIONS = ['新品', '优化'] as const
export type TaskDetailDemandType = (typeof TASK_DETAIL_DEMAND_TYPE_OPTIONS)[number]
export interface TaskDetailPrivacyFieldInput {
taskDetailDemandType?: unknown
taskDetailPrivate?: unknown
}
export interface NormalizedTaskDetailPrivacyFields {
taskDetailDemandType: TaskDetailDemandType
taskDetailPrivate: boolean
}
export function isTaskDetailDemandType(value: unknown): value is TaskDetailDemandType {
return TASK_DETAIL_DEMAND_TYPE_OPTIONS.includes(value as TaskDetailDemandType)
}
export function normalizeTaskDetailPrivacyFields(
input: TaskDetailPrivacyFieldInput,
): NormalizedTaskDetailPrivacyFields {
const rawDemandType = input?.taskDetailDemandType
const rawPrivate = input?.taskDetailPrivate
let taskDetailDemandType: TaskDetailDemandType = '新品'
if (rawDemandType != null) {
if (typeof rawDemandType !== 'string') {
throw new Error('需求类型只能是“新品”或“优化”')
}
const normalizedDemandType = rawDemandType.trim()
if (!normalizedDemandType || !isTaskDetailDemandType(normalizedDemandType)) {
throw new Error('需求类型只能是“新品”或“优化”')
}
taskDetailDemandType = normalizedDemandType
}
let taskDetailPrivate = false
if (rawPrivate != null) {
if (typeof rawPrivate !== 'boolean') {
throw new Error('私密标记只能传 true 或 false')
}
taskDetailPrivate = rawPrivate
}
if (taskDetailPrivate && taskDetailDemandType !== '优化') {
throw new Error('仅优化需求支持私密上传')
}
return {
taskDetailDemandType,
taskDetailPrivate,
}
}