Skip to main content

กฎ Validation กลาง (Form Builder Rule)

ภาพรวม

ชุด "กฎกลาง" ที่แอดมินสร้างไว้ใน CMS แล้วนำไปผูกกับคำถามในหลายฟอร์มได้ เช่น รูปแบบเบอร์มือถือไทย เลขบัตรประชาชน หรือรหัสพนักงาน แต่ละกฎเก็บ pattern (regex), minLength, maxLength, errorMessage และ errorMessageTh ไว้ในคอลัมน์ properties

feature นี้มี endpoint เพียงตัวเดียวซึ่งชื่อในโค้ดคือ test (สืบทอดมาจาก source เดิม) แต่บทบาทจริงสำคัญกว่านั้นมาก เพราะเป็น service ที่ โหลดโครงฟอร์ม เรียกใช้เพื่อนำ rule ไป merge ลงในคำถาม และเป็นตัวกำหนดว่า การตรวจความถูกต้องของคำตอบ จะใช้ pattern ใด

Business Flow

GET /api/form-builder-rule คืน array ของ rule ที่มี status='active' โดยอ่านผ่าน Redis cache

การสร้าง cache key ถูกเลียนแบบจาก source แบบตัวต่อตัวเพื่อให้ Go และ Node ชนกันที่ key เดียวกัน ทำให้รันคู่ขนานได้

  1. optionsJSON คือสตริง {"where":{"status":"active"}} ซึ่งเป็นสำเนาของ JSON.stringify ของ FindManyOptions ที่ controller เดิมส่งเข้ามาแบบเป๊ะ ๆ
  2. suffix คือ sha256(optionsJSON) ที่ตัดเอา 10 ตัวแรก ผ่านฟังก์ชัน util.GenerateShortHash
  3. key มีรูปแบบ FORM_BUILDER_RULE:findAll:{suffix} ตามแม่แบบ ${MODULE_NAME}:${key}
  4. หาก cache hit และ unmarshal สำเร็จจะคืนค่าทันที แต่ถ้า cache entry พังรูปจะ fall through ไปอ่าน DB แทน
  5. เมื่ออ่าน DB สำเร็จจะเขียน cache กลับด้วย default TTL จาก REDIS_TTL
  6. error ทุกชนิดถูกกลืน แล้วคืน array ว่าง เพื่อ parity กับ source ที่ไม่ปล่อยให้ endpoint นี้ตอบ 500

มีทางเข้า 2 แบบที่ต่างกันโดยเจตนา

  • FindAllWithCache ใช้โดย endpoint มี cache และกลืน error
  • FindAllActive ไม่มี cache ใช้โดย form-builder ในขั้นตอน transformPattern เพราะ source เรียก findAll ไม่ใช่ findAllWithCache ที่จุดนั้น หากใช้ cache แอดมินที่แก้ rule แล้วฟอร์มจะยังคงใช้ค่าเก่าอยู่

หากไม่มี Redis client ที่ชื่อ redis service จะ degrade ไปเป็นการอ่านสดโดยไม่ panic

ไฟล์และฟังก์ชันหลัก

รายการค่า
RouteGET /api/form-builder-rule
Registerinternal/formbuilderrule/register.goRegister(r, deps)
Handlerinternal/formbuilderrule/handler.go(*Handler).Test
Serviceinternal/formbuilderrule/service.goFindAllWithCache, FindAllActive, getRedisKey
Repositoryinternal/formbuilderrule/repository.goFindAllActive
Entityinternal/formbuilderrule/entity.goFormBuilderRule ซึ่งมี Properties เป็น raw JSON
ค่าคงที่moduleName = "FORM_BUILDER_RULE", findAllKey = "findAll" และ optionsJSON

จุดเชื่อมต่อกับ Service อื่น

  • ตาราง form_builder_rule (คอลัมน์ status และ jsonb properties)
  • internal/cache ซึ่งเป็น namespaced Redis wrapper ทำงานร่วมกับ Redis client ชื่อ redis โดย namespace และ TTL มาจาก deps.Config.RedisCache
  • internal/util/hash.goGenerateShortHash
  • ถูกใช้โดย โหลดโครงฟอร์ม ในขั้น transformPattern และโดย ส่งคำตอบฟอร์ม ผ่าน formbuilder.Service ที่ประกอบซ้ำใน register ของตัวเอง
  • ฝั่ง client-web ที่เกี่ยวข้องคือ feature form-fill ซึ่งใช้ commonRule ที่ merge มาแล้วเพื่อ validate ฝั่ง client ก่อนส่ง