cli
cli copied to clipboard
feat(models): improve table schema and parsing logic
User story
derived from https://github.com/code-pushup/cli/pull/630#discussion_r1574635336
As a developer I want to have good typing and types and function arguments with intuitive and types that have good DX.
The table type lacks in the following parts:
- no generic type
- alignment separated from headings adds complexity
Acceptance criteria
- [ ] the
Tabletype merges theheadingsand.alignments - [ ] the parse result of the
tableSchemaproduces a generic typed output- [ ] Rows are derived from the input
Implementation details
Schemas:
import { z } from 'zod';
export const tableHeadingSchema = z.object(
{
key: z.string(),
label: z.string().optional(),
alignment: z.enum(['l', 'c', 'r']).optional(),
},
{ description: 'Table heading' },
);
export type TableHeading = z.infer<typeof tableHeadingSchema>;
export const tableRowSchema = z.union([
z.array(primitiveValueSchema),
z.record(primitiveValueSchema),
]);
export type TableRow = z.infer<typeof tableRowSchema>;
export const tableSchema = (description = 'Table information') =>
z.object(
{
headings: z.array(tableHeadingSchema).optional(),
rows: z.array(tableRowSchema),
},
{ description },
);
export type Table = z.infer<ReturnType<typeof tableSchema>>;
Parser:
export function tableSchemaParse<T>() {
}
Your example misses the merged headings and alignment into columns:
export const tableSchema = (description = 'Table information') =>
z.object(
{
headings: z.array(tableHeadingSchema).optional(),
alignment: z.array(tableAlignmentSchema).optional(),
rows: z.array(tableRowSchema),
},
{ description },
);
You need to add a union of the two column types:
const tableColumnsSchema = z.union([
z.array(tableHeadingSchema),
z.array(tableAlignmentSchema),
]);
export const tableSchema = (description = 'Table information') =>
z.object(
{
rows: z.array(tableRowSchema),
columns: tableColumnsSchema.optional(),
},
{ description },
);
Drop the generic types for now