fabian-hiller / valibot

The modular and type safe schema library for validating structural data 🤖
https://valibot.dev
MIT License
6.32k stars 204 forks source link

The union schema of schemas containing a object schema that requires an union schema causes unexpected error. #925

Closed genki closed 6 days ago

genki commented 1 week ago

May be I had collided with the edge case. Extracted the reproduction code.

const ASchema = object({
  foo: union([instance(Uint8Array)]),
  // foo: instance(Uint8Array),  <-- if switched to this, no errors.
});
const BSchema = object({
  bar: array(required(ASchema)),
});
const CSchema = union([BSchema, string()]);
type Data = InferOutput<typeof CSchema>;

const data:Data = {
  bar: [
    {foo: new Uint8Array(32)}
  ],
};

describe("valibot", () => {
  test("data", () => {
    parse(CSchema, data); // throws Error
  });
});
genki commented 1 week ago

Placed more simpler reproduction code at the valibot playground.

https://valibot.dev/playground/?code=JYWwDg9gTgLgBAbzAQygZwKYBo4EkB2AZhlAPICuMYlOEARgFYYDGMOw+aMy+z2c5fMAj4cqKMgCeOKBgCO5YLIAmOLlA4BzAL5xCUCCDgAiAG7IANsDoQYxgNwAoR8xFc4AQQDKzABYYQZDgAXjh6JlYACgRHOD0ICAAuASERSIBtdS1IgEoAXRysWLgAehL4pLgs-E1cnDK4AB4AWma4YEIqgHdgGD8MZTgYCCHfYDQcfBGSA3QAOkdtHKdXTngAIR9-QJCwxhYYaOK6VGTxKUjZBSUByO9+wJzCxeWXN3gAYS2AoNDBYXwGU2D2QahgGhquQKThgkjAGDgABFkNxdgRiGRKNQYI1YfCIJ0viCAHxON5rODKFHIRLI1GhGJxE5QZLpYpxBCEBKJYxciDGbTFPJFbRk1buPm7FDoDCRInbUGU6mvcUQCwYOYWCC1PnLIA