Skip to content

Commit 017e641

Browse files
committed
fix table element validators being too strict
1 parent b903dc1 commit 017e641

File tree

3 files changed

+12
-5
lines changed

3 files changed

+12
-5
lines changed

jsEngine/api/MarkdownAPI.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -187,7 +187,7 @@ export class MarkdownAPI {
187187
* @param body the table body
188188
*/
189189
public createTable(header: string[], body: string[][]): TableElement {
190-
validateAPIArgs(z.object({ header: z.array(z.string()), body: z.array(z.array(z.string())) }), { header, body });
190+
validateAPIArgs(z.object({ header: z.array(z.string()), body: this.apiInstance.validators.tableElementBody }), { header, body });
191191

192192
return new TableElement(this.apiInstance, header, body);
193193
}

jsEngine/api/markdown/AbstractMarkdownElementContainer.ts

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -136,8 +136,8 @@ export abstract class AbstractMarkdownElementContainer extends AbstractMarkdownE
136136
return element;
137137
}
138138

139-
createTable(header: string[], body: string[][]): TableElement {
140-
validateAPIArgs(z.object({ header: z.array(z.string()), body: z.array(z.array(z.string())) }), { header, body });
139+
createTable(header: string[], body: TableElementType[][]): TableElement {
140+
validateAPIArgs(z.object({ header: z.array(z.string()), body: this.apiInstance.validators.tableElementBody }), { header, body });
141141

142142
const element = new TableElement(this.apiInstance, header, body);
143143
this.addElement(element);
@@ -228,18 +228,20 @@ export class CodeElement extends AbstractMarkdownLiteral {
228228
}
229229
}
230230

231+
export type TableElementType = string | number | boolean | null | undefined;
232+
231233
/**
232234
* Represents a markdown table.
233235
*/
234236
export class TableElement extends AbstractMarkdownLiteral {
235237
header: string[];
236238
body: string[][];
237239

238-
constructor(apiInstance: API, header: string[], body: string[][]) {
240+
constructor(apiInstance: API, header: string[], body: TableElementType[][]) {
239241
super(apiInstance);
240242

241243
this.header = header;
242-
this.body = body;
244+
this.body = body.map(row => row.map(cell => (cell === null || cell === undefined ? '' : cell.toString())));
243245
}
244246

245247
public toString(): string {

jsEngine/utils/Validators.ts

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { API } from 'jsEngine/api/API';
44
import { AbstractMarkdownElement } from 'jsEngine/api/markdown/AbstractMarkdownElement';
5+
import type { TableElementType } from 'jsEngine/api/markdown/AbstractMarkdownElementContainer';
56
import type {
67
ButtonPromptButtonOptions,
78
ButtonPromptOptions,
@@ -46,6 +47,8 @@ export class Validators {
4647
tFile: z.ZodType<TFile, any, any>;
4748
cachedMetadata: z.ZodType<CachedMetadata, any, any>;
4849
block: z.ZodType<Block, any, any>;
50+
tableElementType: z.ZodType<TableElementType, any, any>;
51+
tableElementBody: z.ZodType<TableElementType[][], any, any>;
4952
jsExecutionContext: z.ZodType<JsExecutionContext, any, any>;
5053
engineExecutionParams: z.ZodType<EngineExecutionParams, any, any>;
5154
engineExecutionParamsNoCode: z.ZodType<Omit<EngineExecutionParams, 'code'>, any, any>;
@@ -75,6 +78,8 @@ export class Validators {
7578
to: z.number(),
7679
}),
7780
);
81+
this.tableElementType = schemaForType<TableElementType>()(z.union([z.string(), z.number(), z.boolean(), z.null(), z.undefined()]));
82+
this.tableElementBody = schemaForType<TableElementType[][]>()(z.array(z.array(this.tableElementType)));
7883
this.jsExecutionContext = schemaForType<JsExecutionContext>()(
7984
z.object({
8085
file: this.tFile.optional(),

0 commit comments

Comments
 (0)