Skip to content

Commit 1e3df2a

Browse files
committed
resolve CI formatting and structural schema verification
1 parent a578839 commit 1e3df2a

1 file changed

Lines changed: 43 additions & 5 deletions

File tree

renderers/web_core/src/v0_8/schema/common-types.ts

Lines changed: 43 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -59,12 +59,52 @@ const DataValueMapItemSchema: z.ZodType<any> = z.lazy(() =>
5959
if (count !== 1) {
6060
ctx.addIssue({
6161
code: z.ZodIssueCode.custom,
62-
message: `Value map item must have at least one value property (valueString, valueNumber, valueBoolean, valueMap), found ${count}.`,
62+
message: `Value map item must have exactly one value property (valueString, valueNumber, valueBoolean, valueMap), found ${count}.`,
6363
});
6464
}
6565
}),
6666
);
6767

68+
export const DataValueSchema = z
69+
.object({
70+
key: z.string(),
71+
valueString: z.string().optional(),
72+
valueNumber: z.number().optional(),
73+
valueBoolean: z.boolean().optional(),
74+
valueMap: z.array(DataValueMapItemSchema).optional(),
75+
})
76+
.strict()
77+
.superRefine((val: any, ctx: z.RefinementCtx) => {
78+
let count = 0;
79+
if (val.valueString !== undefined) count++;
80+
if (val.valueNumber !== undefined) count++;
81+
if (val.valueBoolean !== undefined) count++;
82+
if (val.valueMap !== undefined) count++;
83+
if (count !== 1) {
84+
ctx.addIssue({
85+
code: z.ZodIssueCode.custom,
86+
message: `Value must have exactly one value property (valueString, valueNumber, valueBoolean, valueMap), found ${count}.`,
87+
});
88+
}
89+
})
90+
.superRefine((val: any, ctx: z.RefinementCtx) => {
91+
const checkDepth = (v: any, currentDepth: number) => {
92+
if (currentDepth > 5) {
93+
ctx.addIssue({
94+
code: z.ZodIssueCode.custom,
95+
message: 'valueMap recursion exceeded maximum depth of 5.',
96+
});
97+
return;
98+
}
99+
if (v.valueMap && Array.isArray(v.valueMap)) {
100+
for (const item of v.valueMap) {
101+
checkDepth(item, currentDepth + 1);
102+
}
103+
}
104+
};
105+
checkDepth(val, 1);
106+
});
107+
68108
export function createDataValueSchema(options: {maxDepth?: number} = {}) {
69109
const maxDepth = options.maxDepth ?? 5;
70110
return DataValueMapItemSchema.superRefine((val: any, ctx: z.RefinementCtx) => {
@@ -86,8 +126,6 @@ export function createDataValueSchema(options: {maxDepth?: number} = {}) {
86126
});
87127
}
88128

89-
export const DataValueSchema = createDataValueSchema();
90-
91129
export const NumberValueSchema = z
92130
.object({
93131
path: z.string().optional(),
@@ -129,7 +167,7 @@ export const ActionSchema = z.object({
129167
literalNumber: z.number().optional(),
130168
literalBoolean: z.boolean().optional(),
131169
})
132-
.describe('The dynamic value. Define EXACTLY ONE of the nested properties.')
170+
.describe('The dynamic value. Define at least one of the nested properties.')
133171
.strict()
134172
.superRefine(atLeastOneKey),
135173
}),
@@ -353,4 +391,4 @@ export const ListSchema = z.object({
353391

354392
export const CardSchema = z.object({
355393
child: z.string().describe('The ID of the component to be rendered inside the card.'),
356-
});
394+
});

0 commit comments

Comments
 (0)