Files
faunosteo/packages/supabase/src/field-limits.ts
T
bchanot b54c8e5bd8 feat(supabase): animals and relations M3 with RPCs, history and caps
Species rows (8 + other), animals with the D6b birth model, relations with
composite workspace keys, current/ended/voided states and a partial unique
index; definer RPCs for animals and relations (one lock order, shared write
budget, caps), invoker reads (search_animals, list_client_animals,
client_archive_preview) and search_clients with current_animal_count.
pgTAP 014 (254), parity test, sandbox suite, types from the isolated stack.
2026-10-26 00:52:22 +01:00

60 lines
1.9 KiB
TypeScript

/**
* Bounds and patterns of the V1-1 record fields, shared by the forms (Zod
* schemas, D-R7) and proven equal to the SQL CHECK constraints by
* tests/db/field-limits.test.ts (behaviour on the migrated schema, never by
* parsing SQL). Contract: docs/development/V1-1_OWNERS_ANIMALS.md §5, §12.2.
*
* Lengths count code points, as PostgreSQL's char_length does: use
* codePointLength, never String.length (UTF-16 units) or Zod's .max() alone.
*/
export const CLIENT_FIELD_LIMITS = {
displayName: 120,
email: 254,
addressLine: 120,
postalCode: 16,
city: 80,
} as const
export const ANIMAL_FIELD_LIMITS = {
name: 80,
breed: 80,
speciesDetail: 80,
} as const
/**
* Lower bounds of the SQL CHECKs on birth years and on every record date
* (birth, relation start and end). The upper bound is "not in the future":
* the form checks the device's local date, the RPCs tomorrow's UTC date.
*/
export const BIRTH_YEAR_MIN = 1900
export const RECORD_DATE_MIN = '1900-01-01'
/** U+0000 to U+001F, U+007F to U+009F, U+2028, U+2029 (the SQL literal starts at U+0001: text cannot hold U+0000). */
export const CONTROL_CHARACTERS = /[\u0000-\u001F\u007F-\u009F\u2028\u2029]/u
/** Applied to the lower-cased value. JavaScript's \s is wider than SQL's, so the form refuses at least what SQL refuses. */
export const EMAIL_PATTERN = /^[^@\s]+@[^@\s]+\.[^@\s]+$/u
export const PHONE_E164_PATTERN = /^\+[1-9][0-9]{6,14}$/
export const COUNTRY_CODE_PATTERN = /^[A-Z]{2}$/
export function codePointLength(value: string): number {
return [...value].length
}
/**
* The text rule of contract §5: 1 to max code points, no leading or trailing
* space (U+0020, as SQL btrim checks), no control character.
*/
export function isCleanText(value: string, max: number): boolean {
const length = codePointLength(value)
return (
length >= 1 &&
length <= max &&
value === value.replace(/^ +| +$/g, '') &&
!CONTROL_CHARACTERS.test(value)
)
}