Skip to content

Dynamic type fails when used inside "partial" helper #1294

Open
@jakubwolny

Description

@jakubwolny

When dynamic type is used inside partial then it receives also undefined value for validation

import { assert, object, partial, dynamic, literal } from "superstruct";

const User = object({
  kind: literal("user"),
});

const Bot = object({
  kind: literal("bot"),
});

const userOrBot = dynamic((value) => {
  return value.kind === "user" ? User : Bot;
});

const partialDataType = partial(
  object({
    user: userOrBot,
  })
);

try {
  assert({}, partialDataType);
} catch (e) {
  // Error: Cannot read properties of undefined (reading 'kind')
}

I created a sandbox code to prove it: https://codesandbox.io/p/devbox/wqh8pv?file=%2Findex.js

In my opinion this is not expected behaviour since dynamic type definition has no way of knowing in what context it was used, "undefined" value should simply work in this case, because it was allowed in partial definition.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions