feat(supabase): clients M2 with RPCs, search and per-workspace budget
V1-1.2 (contract V1-1_OWNERS_ANIMALS.md §5, §9, §10). public.clients (person or organization, display name only, optional contact, folded search keys), create/update/set_archived as definer RPCs (identical not_found, idempotent create with divergent-replay refusal, revisions, archive read-only), search_clients invoker with keyset pages and phone search, write budget per workspace in UTC hour/day windows under a row lock, 20 000-client cap, ids-only audit hidden from platform staff (N9), nothing for anon or service_role. pgTAP 013 (130), field-limits parity test, sandbox suite (concurrency, budget races, 20 000-client search), types regenerated.
This commit is contained in:
1 parent
2c0531d638
commit
4adbfcb608
9 files changed
+1916
-2
No files matched your search
@@ -0,0 +1,45 @@
|
||||
/**
|
||||
* 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
|
||||
|
||||
/** 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)
|
||||
)
|
||||
}
|
||||
Reference in new issue
Block a user