6368ee376f
新增 shop-manage-field-validators.ts:分组/店铺名/商城/账号/密码逐字段校验 与汇总,错误文案与后端 NotBlank/Size 规则一致。 TDD: task-96.test.ts 8 用例先 RED 后 GREEN。
55 lines
2.2 KiB
TypeScript
55 lines
2.2 KiB
TypeScript
/** 店铺中心逐字段校验(任务 96):账号/密码等店铺字段可独立校验,错误文案与后端 NotBlank/Size 一致;纯逻辑。 */
|
|
import type { ShopManageFormErrors, ShopManageFormValues } from './shop-manage-form-model.ts'
|
|
|
|
export const SHOP_MANAGE_ZN_USERNAME_MAX_LEN = 128
|
|
|
|
function requiredText(value: string): string {
|
|
return (value || '').trim()
|
|
}
|
|
|
|
export function validateGroupId(value: number | null): string | undefined {
|
|
return typeof value === 'number' && Number.isFinite(value) && value >= 1 ? undefined : '分组不能为空'
|
|
}
|
|
|
|
export function validateShopNameValue(value: string): string | undefined {
|
|
return requiredText(value) ? undefined : '店铺名称不能为空'
|
|
}
|
|
|
|
export function validateMallNameValue(value: string): string | undefined {
|
|
return requiredText(value) ? undefined : '商城名称不能为空'
|
|
}
|
|
|
|
export function validateAccount(value: string): string | undefined {
|
|
return requiredText(value) ? undefined : '账号不能为空'
|
|
}
|
|
|
|
export function validatePassword(value: string): string | undefined {
|
|
return requiredText(value) ? undefined : '密码不能为空'
|
|
}
|
|
|
|
export function validateZnUsernameLength(value: string): string | undefined {
|
|
const trimmed = (value || '').trim()
|
|
if (trimmed.length > SHOP_MANAGE_ZN_USERNAME_MAX_LEN) {
|
|
return `自动化账号长度不能超过${SHOP_MANAGE_ZN_USERNAME_MAX_LEN}个字符`
|
|
}
|
|
return undefined
|
|
}
|
|
|
|
/** 汇总逐字段错误(供编辑/新增表单即时反馈)。 */
|
|
export function collectShopManageFieldErrors(form: ShopManageFormValues): ShopManageFormErrors {
|
|
const errors: ShopManageFormErrors = {}
|
|
const groupError = validateGroupId(form.groupId)
|
|
if (groupError) errors.groupId = groupError
|
|
const nameError = validateShopNameValue(form.shopName)
|
|
if (nameError) errors.shopName = nameError
|
|
const mallError = validateMallNameValue(form.mallName)
|
|
if (mallError) errors.mallName = mallError
|
|
const accountError = validateAccount(form.account)
|
|
if (accountError) errors.account = accountError
|
|
const passwordError = validatePassword(form.password)
|
|
if (passwordError) errors.password = passwordError
|
|
const znError = validateZnUsernameLength(form.znUsername)
|
|
if (znError) errors.znUsername = znError
|
|
return errors
|
|
}
|