phase 6 เบื้องต้น
This commit is contained in:
parent
1bb850c6b8
commit
93570d38d8
20 changed files with 1715 additions and 34 deletions
41
web/app/utils/fieldValidation.ts
Normal file
41
web/app/utils/fieldValidation.ts
Normal file
|
|
@ -0,0 +1,41 @@
|
|||
/**
|
||||
* Validation ฝั่ง client ของ dynamic form สร้างเอกสาร (FR-10) — pure function ให้ unit test ได้
|
||||
* เงื่อนไขตรงกับ backend (DocumentsController.Generate): required ห้ามว่าง,
|
||||
* number ต้อง parse เป็นเลขได้, date ต้องเป็นวันที่ ISO ที่ parse ได้ — client เช็คก่อนเพื่อ feedback ทันที
|
||||
*/
|
||||
|
||||
export interface FieldDefLike {
|
||||
key: string
|
||||
label: string
|
||||
type: string
|
||||
isRequired: boolean
|
||||
}
|
||||
|
||||
export interface FieldIssue {
|
||||
key: string
|
||||
message: string
|
||||
}
|
||||
|
||||
/** ตัวเลข: จำนวนเต็ม/ทศนิยม ติดลบได้ มี comma คั่นหลักได้ (ตรง NumberStyles.Number ของ backend) */
|
||||
const NUMBER_RE = /^[+-]?\d+(?:\.\d+)?$/
|
||||
|
||||
export function validateFieldValues(
|
||||
defs: FieldDefLike[],
|
||||
values: Record<string, string>,
|
||||
): FieldIssue[] {
|
||||
const issues: FieldIssue[] = []
|
||||
for (const def of defs) {
|
||||
const raw = (values[def.key] ?? '').trim()
|
||||
if (raw === '') {
|
||||
if (def.isRequired) issues.push({ key: def.key, message: `“${def.label}” เป็นช่องที่ต้องกรอก` })
|
||||
continue
|
||||
}
|
||||
if (def.type === 'number' && !NUMBER_RE.test(raw.replace(/,/g, ''))) {
|
||||
issues.push({ key: def.key, message: `“${def.label}” ต้องเป็นตัวเลข` })
|
||||
}
|
||||
if (def.type === 'date' && Number.isNaN(Date.parse(raw))) {
|
||||
issues.push({ key: def.key, message: `“${def.label}” ต้องเป็นวันที่ (YYYY-MM-DD)` })
|
||||
}
|
||||
}
|
||||
return issues
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue