Skip to content

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

Open

Description

@crispearson

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions