From 5b29ebd7cf8f68585f6d9b30c27c09c3cb7c5c54 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C4=9Bj=20Chalk?= Date: Tue, 21 May 2024 15:32:02 +0200 Subject: [PATCH] feat(models): add optional table title --- packages/models/docs/models-reference.md | 61 ++++++++++++++++++---- packages/models/src/lib/table.ts | 45 +++++++++------- packages/models/src/lib/table.unit.test.ts | 1 + 3 files changed, 79 insertions(+), 28 deletions(-) diff --git a/packages/models/docs/models-reference.md b/packages/models/docs/models-reference.md index 2d1ddbb01..615787048 100644 --- a/packages/models/docs/models-reference.md +++ b/packages/models/docs/models-reference.md @@ -6,10 +6,10 @@ Detailed information _Object containing the following properties:_ -| Property | Description | Type | -| :------- | :------------------------ | :----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | -| `issues` | List of findings | _Array of [Issue](#issue) items_ | -| `table` | Table of related findings | _Object with properties:_ | +| Property | Description | Type | +| :------- | :------------------------ | :--------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | +| `issues` | List of findings | _Array of [Issue](#issue) items_ | +| `table` | Table of related findings | _Object with properties:_ _or_ _Object with properties:_ | _All properties are optional._ @@ -1193,6 +1193,13 @@ _Object containing the following properties:_ _(\*) Required._ +## PrimitiveValue + +_Union of the following possible types:_ + +- `string` +- `number` + ## Report _Object containing the following properties:_ @@ -1253,19 +1260,53 @@ _Returns:_ - [AuditOutputs](#auditoutputs) _or_ _Promise of_ [AuditOutputs](#auditoutputs) -## TableHeading +## TableAlignment + +Cell alignment + +_Enum string, one of the following possible values:_ + +- `'left'` +- `'center'` +- `'right'` -Source file location +## TableColumnObject _Object containing the following properties:_ -| Property | Type | -| :------------- | :------- | -| **`key`** (\*) | `string` | -| `label` | `string` | +| Property | Description | Type | +| :------------- | :------------- | :-------------------------------- | +| **`key`** (\*) | | `string` | +| `label` | | `string` | +| `align` | Cell alignment | [TableAlignment](#tablealignment) | _(\*) Required._ +## TableColumnPrimitive + +Cell alignment + +_Enum string, one of the following possible values:_ + +- `'left'` +- `'center'` +- `'right'` + +## TableRowObject + +Object row + +_Object record with dynamic keys:_ + +- _keys of type_ `string` +- _values of type_ [PrimitiveValue](#primitivevalue) + +## TableRowPrimitive + +Primitive row + +_Array of [PrimitiveValue](#primitivevalue) items._ + ## UploadConfig _Object containing the following properties:_ diff --git a/packages/models/src/lib/table.ts b/packages/models/src/lib/table.ts index 1d6a0065e..447d19b00 100644 --- a/packages/models/src/lib/table.ts +++ b/packages/models/src/lib/table.ts @@ -26,25 +26,34 @@ export const tableRowPrimitiveSchema = z.array(primitiveValueSchema, { }); export type TableRowPrimitive = z.infer; -const tablePrimitiveSchema = z.object( - { - columns: z.array(tableAlignmentSchema).optional(), - rows: z.array(tableRowPrimitiveSchema), - }, - { description: 'Table with primitive rows and optional alignment columns' }, +const tableSharedSchema = z.object({ + title: z.string().optional().describe('Display title for table'), +}); +const tablePrimitiveSchema = tableSharedSchema.merge( + z.object( + { + columns: z.array(tableAlignmentSchema).optional(), + rows: z.array(tableRowPrimitiveSchema), + }, + { description: 'Table with primitive rows and optional alignment columns' }, + ), ); - -const tableObjectSchema = z.object( - { - columns: z - .union([z.array(tableAlignmentSchema), z.array(tableColumnObjectSchema)]) - .optional(), - rows: z.array(tableRowObjectSchema), - }, - { - description: - 'Table with object rows and optional alignment or object columns', - }, +const tableObjectSchema = tableSharedSchema.merge( + z.object( + { + columns: z + .union([ + z.array(tableAlignmentSchema), + z.array(tableColumnObjectSchema), + ]) + .optional(), + rows: z.array(tableRowObjectSchema), + }, + { + description: + 'Table with object rows and optional alignment or object columns', + }, + ), ); export const tableSchema = (description = 'Table information') => diff --git a/packages/models/src/lib/table.unit.test.ts b/packages/models/src/lib/table.unit.test.ts index 5027be87c..b6000949c 100644 --- a/packages/models/src/lib/table.unit.test.ts +++ b/packages/models/src/lib/table.unit.test.ts @@ -163,6 +163,7 @@ describe('tableSchema', () => { it('should parse complete table', () => { const fullTable: Table = { + title: 'Largest Contentful Paint element', columns: [ // center is often the default when rendering in MD or HTML { key: 'phase', label: 'Phase' },