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

Inconsistency in returned value for schema with transformation and default value #1578

Open
mckacz opened this issue Feb 7, 2022 · 1 comment

Comments

@mckacz
Copy link

mckacz commented Feb 7, 2022

Describe the bug

Hi, I've noticed the following unexpected behavior:
When using transform() with default() there is inconsistency in returned value between undefined and {} input.

To Reproduce

import * as yup from "yup";

const baseSchema = yup
  .number()
  .transform(v => v * 1000)
  .default(1);

console.log(yup.object({ value: baseSchema }).validateSync({})); // { value: 1 }
console.log(yup.object({ value: baseSchema }).validateSync(undefined)); // { value: 1000 }

Full test suite:
https://codesandbox.io/s/yup-transform-with-default-inconsistency-3tewl?file=/index.test.ts

Expected behavior

The same value for each case - either default value with or without transformation applied.

@jquense
Copy link
Owner

jquense commented Aug 23, 2022

Ok so the reason for this is because in the undefined case, the object is being defaulted to {value: 1} initially because: https://github.com/jquense/yup#object-schema-defaults

In the other case, the input is not undefined, so the default object isn't used but value is still undefined. Since there is no value to process no transforms are run, and you just get the default

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

No branches or pull requests

2 participants