customFieldValueRules.ts 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. /**
  2. * What a custom field's value may be, for its type.
  3. *
  4. * One set of rules for the form and the server. The form uses them to say
  5. * what is wrong before the save leaves the browser; the server uses them
  6. * because a request does not have to come from the form.
  7. *
  8. * Values are stored as text, whatever the type: a number as typed, a date as
  9. * a calendar day (YYYY-MM-DD), a checkbox as 'true' or 'false', a choice as
  10. * the option's own text. An empty string is "no value", and is fine for every
  11. * type unless the field is required.
  12. */
  13. /** Why a value was refused. The form turns each into a sentence. */
  14. export type CustomFieldIssue = 'required' | 'number' | 'date' | 'option' | 'checkbox' | 'tooLong'
  15. /** Longer than any text a field is for, and short enough to keep a row small. */
  16. export const CUSTOM_FIELD_VALUE_MAX_LENGTH = 10000
  17. export interface CustomFieldRuleInput {
  18. fieldType: string
  19. options: string | null
  20. required: boolean
  21. }
  22. /** A select field's choices. They are kept as one comma-separated string. */
  23. export function customFieldOptions(options: string | null | undefined): string[] {
  24. return (options || '')
  25. .split(',')
  26. .map((option) => option.trim())
  27. .filter(Boolean)
  28. }
  29. const DATE_ONLY = /^(\d{4})-(\d{2})-(\d{2})$/
  30. /** A real calendar day written YYYY-MM-DD: 2026-02-30 is not one. */
  31. function isCalendarDay(value: string): boolean {
  32. const match = DATE_ONLY.exec(value)
  33. if (!match) return false
  34. const [year, month, day] = [Number(match[1]), Number(match[2]), Number(match[3])]
  35. const date = new Date(Date.UTC(year, month - 1, day))
  36. return (
  37. date.getUTCFullYear() === year && date.getUTCMonth() === month - 1 && date.getUTCDate() === day
  38. )
  39. }
  40. /**
  41. * Whether a required field has been left empty. A checkbox never is: left
  42. * unticked is an answer.
  43. */
  44. export function isMissingRequiredValue(field: CustomFieldRuleInput, value: string): boolean {
  45. return field.required && field.fieldType !== 'checkbox' && value.trim() === ''
  46. }
  47. /**
  48. * Whether the value suits the field's type. Says nothing about "required".
  49. * A type this file does not know is treated as text, so a field type added
  50. * later is never refused by an older rule.
  51. */
  52. export function customFieldTypeIssue(
  53. field: CustomFieldRuleInput,
  54. value: string
  55. ): CustomFieldIssue | null {
  56. if (value.length > CUSTOM_FIELD_VALUE_MAX_LENGTH) return 'tooLong'
  57. if (value === '') return null
  58. switch (field.fieldType) {
  59. case 'number':
  60. // The same test a number field's default value has to pass.
  61. return value.trim() !== '' && Number.isFinite(Number(value)) ? null : 'number'
  62. case 'date':
  63. return isCalendarDay(value) ? null : 'date'
  64. case 'checkbox':
  65. return value === 'true' || value === 'false' ? null : 'checkbox'
  66. case 'select':
  67. return customFieldOptions(field.options).includes(value) ? null : 'option'
  68. default:
  69. return null
  70. }
  71. }
  72. /** The first thing wrong with a value, or null when it may be saved. */
  73. export function customFieldValueIssue(
  74. field: CustomFieldRuleInput,
  75. value: string
  76. ): CustomFieldIssue | null {
  77. if (isMissingRequiredValue(field, value)) return 'required'
  78. return customFieldTypeIssue(field, value)
  79. }