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

Union validate wrongly validates object when the runtypes have the same property and one of the properties is a Union #301

Open
jsmith opened this issue Mar 23, 2022 · 0 comments

Comments

@jsmith
Copy link

jsmith commented Mar 23, 2022

Sorry for the long title, but i think I found an issue where the type check wrongly validates an object. Reproduction link to a codesandbox but here is the code:

import { Record, Literal, Union } from "runtypes";

const FooBar = Record({
  foo: Literal("bar"),
});

const AB = Record({
  type: Union(Literal("A"), Literal("B")),
  foobar: FooBar,
});

const C = Record({
  type: Literal("C"),
  foobar: FooBar,
});

const ABC = Union(AB, C);

const input = {
  type: "C",
  foobar: {},
};

console.log(ABC.guard(input)); // returns true

The cause seems to be type: Union(Literal("A"), Literal("B")). If you split AB into A and B, it works as expected since the validator doesn't even look at A or B.

The validator first checks AB. When it does this, it checks foodbar and adds {} + FooBar it to the VisitedState. Next, when it checks C, the type property matches and then when it checks foorbar, {} + FooBar is already in the VisitedState so the object type checks successfully.

I'm not sure the best solution. Maybe the code shouldn't assume that if value and A are in VisitedState, it has been validated successfully? Thanks for the help!

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