Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Open
jakubwolny opened this issue Mar 2, 2025 · 0 comments
Open

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

jakubwolny opened this issue Mar 2, 2025 · 0 comments

Comments

@jakubwolny
Copy link
Contributor

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant