Files
zeekayandHanzo Dev 64c558cdc4 fix(settings): validate the default date format as a date format
The document preferences form validated its date format with
ZDocumentMetaTimezoneSchema, a bare string, while its type and the settings
routes it submits to say ZDocumentMetaDateFormatSchema, the list of formats.
@hookform/resolvers 3 never compared the two; 5 types the submit handler by
what the schema returns, and a string is not a date format.

The field now validates with the date format schema. The select only offers
DATE_FORMATS and the server has rejected anything else since the routes
were written, so a value the form sends is never newly refused; what
changes is that a bad value stops in the form instead of at the server.
date-formats.test.ts holds that: every format the select offers and the
stored default are in the list the schema is made of, and a time zone is
not.

Co-authored-by: Hanzo Dev <[email protected]>
2026-09-19 04:23:43 -07:00

40 lines
1.4 KiB
TypeScript

/**
* The date format a team or organisation keeps.
*
* The preferences form offers DATE_FORMATS in a select and validates the
* choice with ZDocumentMetaDateFormatSchema, the schema the settings routes
* check it against. That schema is z.enum(VALID_DATE_FORMAT_VALUES); its module
* runs lingui macros on import, which only a build can, so the list it is made
* of is what is tested here. Every format the select offers, and the default a
* new team is stored with, has to be in it. A time zone — what the form used
* to validate a date format with — is not.
*
* Run: npx tsx --test packages/lib/__tests__/date-formats.test.ts
*/
import assert from 'node:assert/strict';
import { describe, test } from 'node:test';
import {
DATE_FORMATS,
DEFAULT_DOCUMENT_DATE_FORMAT,
VALID_DATE_FORMAT_VALUES,
} from '../constants/date-formats';
const isValid = (value: string) => VALID_DATE_FORMAT_VALUES.some((format) => format === value);
describe('date formats', () => {
test('every format the preferences select offers is valid', () => {
for (const format of DATE_FORMATS) {
assert.ok(isValid(format.value), format.value);
}
});
test('the format a team is stored with by default is valid', () => {
assert.ok(isValid(DEFAULT_DOCUMENT_DATE_FORMAT));
});
test('a time zone is not a date format', () => {
assert.equal(isValid('Australia/Melbourne'), false);
});
});