Skip to content

Commit 6a3bf13

Browse files
committed
fix(spec): declare orderBy's padded-name rule as a regex, so the JSON Schema publishes it
A superRefine is dropped by z.toJSONSchema and would have added eighteen x-dropped-refinements sites; a native .regex() publishes the same rule as the schema's pattern, which a validator reading packages/spec/json-schema then refuses too. Co-Authored-By: Claude <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01E1GNboWQRhh5vidwh2hxTY
1 parent 5d64c37 commit 6a3bf13

2 files changed

Lines changed: 24 additions & 14 deletions

File tree

‎packages/spec/src/ui/view.test.ts‎

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2311,13 +2311,25 @@ describe('GroupingFieldSchema', () => {
23112311
])('refuses %s whitespace at `orderBy`, by name', (_label, spelling, remedy) => {
23122312
const result = GroupingFieldSchema.safeParse({ field: 'period_label', orderBy: spelling });
23132313
expect(result.success).toBe(false);
2314-
expect(result.error!.issues.map((i) => [i.path.join('.'), i.code])).toEqual([['orderBy', 'custom']]);
2314+
expect(result.error!.issues.map((i) => [i.path.join('.'), i.code])).toEqual([['orderBy', 'invalid_format']]);
23152315
const message = result.error!.issues[0]!.message;
23162316
expect(message).toContain('grouping.fields[].orderBy');
23172317
expect(message).toContain(JSON.stringify(spelling));
23182318
expect(message).toContain(remedy);
23192319
});
23202320

2321+
// A native `.regex()`, not a refinement: the published JSON Schema states the
2322+
// rule, so a validator reading packages/spec/json-schema refuses it too (#18670).
2323+
it('publishes the padded-name rule on `orderBy` as the JSON Schema `pattern`', () => {
2324+
const json = z.toJSONSchema(GroupingFieldSchema, { unrepresentable: 'any', io: 'input' }) as {
2325+
properties: Record<string, { minLength?: number; pattern?: string }>;
2326+
};
2327+
expect(json.properties.orderBy).toMatchObject({ minLength: 1, pattern: '^(?:\\S|\\S[\\s\\S]*\\S)?$' });
2328+
const pattern = new RegExp(json.properties.orderBy!.pattern!);
2329+
expect([' period_start', 'period_start ', ' '].map((s) => pattern.test(s))).toEqual([false, false, false]);
2330+
expect(pattern.test('period_start')).toBe(true);
2331+
});
2332+
23212333
it('refuses a padded `orderBy` through ListViewSchema at `grouping.fields[N].orderBy`', () => {
23222334
const result = ListViewSchema.safeParse({
23232335
columns: ['name'],

‎packages/spec/src/ui/view.zod.ts‎

Lines changed: 11 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1281,16 +1281,17 @@ function checkGroupingFieldName(raw: string): string | undefined {
12811281
}
12821282

12831283
/**
1284-
* Validate one `grouping.fields[].orderBy` name (#25786): the same non-padded
1285-
* rule as {@link checkGroupingFieldName}, for the same reason — the name is
1286-
* compiled into the group header query verbatim, here as a `min` / `max`
1287-
* operand. Returns the author-facing refusal, or `undefined` when it conforms.
1288-
* The empty string is the schema's `min(1)` and the compiler's
1284+
* The refusal for a padded `grouping.fields[].orderBy` name (#25786): the same
1285+
* non-padded rule as {@link checkGroupingFieldName}, for the same reason — the
1286+
* name is compiled into the group header query verbatim, here as a `min` /
1287+
* `max` operand. Declared as the key's `.regex()` rather than a
1288+
* `.superRefine()`, so the published JSON Schema states the rule as its
1289+
* `pattern` instead of adding an `x-dropped-refinements` site (#18670). The
1290+
* empty string is the key's `min(1)` and the compiler's
12891291
* `grouping_order_by_blank`, not this.
12901292
*/
1291-
function checkGroupingOrderByName(raw: string): string | undefined {
1292-
if (NON_PADDED_FIELD_NAME_PATTERN.test(raw)) return undefined;
1293-
1293+
function groupingOrderByPaddedRefusal(input: unknown): string {
1294+
const raw = String(input);
12941295
const trimmed = raw.trim();
12951296
const remedy = trimmed === ''
12961297
? 'Name the field to order the groups by, or remove `orderBy` — this value is nothing but whitespace.'
@@ -1410,7 +1411,7 @@ const groupByFieldCheck = (view: GroupByFieldView) => (raw: string, ctx: z.Refin
14101411
* classes because two backends order strings by two collations — and
14111412
* `os validate` refuses any other (`list-view-group-order-unorderable`). The
14121413
* name is compiled verbatim, so a padded one is refused here as `field`'s is
1413-
* (#17360's ruling, {@link checkGroupingOrderByName}). The header set is
1414+
* (#17360's ruling, {@link groupingOrderByPaddedRefusal}). The header set is
14141415
* sorted whole: a `limit` on the header query slices before any sort (the
14151416
* "Header cardinality is unbounded" limit on {@link GroupingConfigSchema}).
14161417
*/
@@ -1434,10 +1435,7 @@ export const GroupingFieldSchema = lazySchema(() => strictObject({
14341435
+ 'nothing): by the group key, or by the `orderBy` value each header row carries when `orderBy` is set',
14351436
),
14361437
orderBy: z.string().min(1)
1437-
.superRefine((raw, ctx) => {
1438-
const refusal = checkGroupingOrderByName(raw);
1439-
if (refusal) ctx.addIssue({ code: 'custom', message: refusal });
1440-
})
1438+
.regex(NON_PADDED_FIELD_NAME_PATTERN, { error: (iss) => groupingOrderByPaddedRefusal(iss.input) })
14411439
.optional()
14421440
.describe(
14431441
'Field whose value orders this level\'s groups, when that is not the grouping field — a field of the '

0 commit comments

Comments
 (0)