cli icon indicating copy to clipboard operation
cli copied to clipboard

feat(models): improve table schema and parsing logic

Open BioPhoton opened this issue 1 year ago • 2 comments

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 Table type merges the headings and. alignments
  • [ ] the parse result of the tableSchema produces 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>() {

}

BioPhoton avatar Apr 22 '24 19:04 BioPhoton

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 },
  );

matejchalk avatar Apr 23 '24 08:04 matejchalk

Drop the generic types for now

vmasek avatar Apr 25 '24 13:04 vmasek