Skip to content

yup.default() sometimes not invoked when used with yup.lazy() #699

Description

@ehahn9

Describe the bug
when yup.lazy() is used inside a parent schema, yup.default() not honored

To Reproduce

import * as yup from 'yup';

const schema = yup.object({
  num: yup.number().default(123),
  lazyNum: yup.lazy(() => yup.number().default(456))
})

it('should supply defaults in normal and lazy case', () => {
  expect(schema.validateSync(undefined)).toEqual({
    num: 123,           // GOOD
    lazyNum: 456        // FAILS: undefined,
  })
});

Expected behavior
I'd expect {num: 123, lazyNum: 456} - but maybe I'm surely misunderstanding? Maybe this
is a doc issue, but is certainly surprising behavior (at least to me ;-)) but I still need a way to
have defaults combined with lazy schemas.

Additional context

  • works fine if yup.lazy() is the top-level schema (e.g. no outer object({})
  • In this contrived test, we're ignoring the lazy value - in my real-world, the lazy function is testing
    typeof value etc.

Activity

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions