#467

catalog.validate() skips per-component props validation when the catalog has more than one component

Opencrispearsonopened 4d ago

Summary

catalog.validate() checks each element's props against its component's schema only when the catalog has exactly one component. With two or more components, any props object is accepted.

Versions

@json-render/core 0.21.4, with @json-render/react 0.21.4 for the schema. The same code is present in 0.22.0 (read from the published tarball, not run).

Reproduction (run on 0.21.4, Node 24)

import { z } from "zod";
import { defineCatalog } from "@json-render/core";
import { schema } from "@json-render/react/schema";

const bar = { props: z.object({ done: z.number().int().min(0).max(10) }), description: "bar" };
const label = { props: z.object({ text: z.string().max(20) }), description: "label" };

const one = defineCatalog(schema, { components: { Bar: bar }, actions: {} });
const two = defineCatalog(schema, { components: { Bar: bar, Label: label }, actions: {} });

const bad = { root: "a", elements: { a: { type: "Bar", props: { done: 99 }, children: [] } } };

console.log(one.validate(bad).success); // false (expected)
console.log(two.validate(bad).success); // true  (props not checked)

Cause (as far as we can tell)

In buildZodType, case "propsOf" returns z.record(z.string(), z.unknown()) when there is more than one schema, instead of choosing the schema for the element's type (for example a union keyed on type).

Expected

Each element's props are validated against the schema of its own component type, whatever the number of components.

Workaround we use

After validate(), we check each element against catalog.data.components[type].props ourselves.

Activity

0

No activity yet.