Skip to content

Invalid key: Expected \"...\" but received undefined #1216

Open
@felipedeboni

Description

@felipedeboni

I've found a problem with custom error messages, not sure when it started tho.

Example schema:

const Schema = v.object({
  email: v.pipe(
    v.string("Whoops! It should be a string."),
    v.nonEmpty("Looks like your forgot to tell what is your email."),
    v.email("Your email must be valid.")
  ),
});

Current behavior:

If the object being validate does not have the key, looks like an internal message is returned, while all other options have a "fancy" message.

// message is "Invalid key: Expected \"email\" but received undefined",
const result1 = v.safeParse(Schema, {}); 

// message is "Whoops! It should be a string." 
const result2 = v.safeParse(Schema, { email: undefined });
const result3 = v.safeParse(Schema, { email: null });
const result4 = v.safeParse(Schema, { email: 1 });

Expected output:

All calls should return the same message Whoops! It should be a string. for the email key.

Reproduction:

Playground link

Metadata

Metadata

Assignees

Labels

duplicateThis issue or pull request already existsintendedThe behavior is expected

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions