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

Is there a way to get a context inside .default(() => {...}) ? #1984

Closed
AlimovSV opened this issue Apr 17, 2023 · 4 comments
Closed

Is there a way to get a context inside .default(() => {...}) ? #1984

AlimovSV opened this issue Apr 17, 2023 · 4 comments

Comments

@AlimovSV
Copy link

The library allows specifying the context when using getDefault method:

schema.getDefault({
  context: { ... }
});

but I don't see any way to get a passed context inside the field.default(() => { ... } method. The whole example:

const schema =  Yup.object({
  from: Yup.string().default(() => moment().tz(>> get timezone here <<).startOf('day').toISOString()),
  until: Yup.string().default(() => moment().tz(>> get timezone here <<).endOf('day').toISOString()),
}),

schema.getDefault({ context: { timezone: 'UTC' });

// -->> should return `{ from: '...', until: '...' }` where from and until is a begin/end of day in UTC timezone

Please help, thanks for any advice!

@Edleychris
Copy link

Here's how you can modify your example to use the context:

const schema = Yup.object({ from: Yup.string().default((_, { context }) => moment().tz(context.timezone).startOf('day').toISOString()), until: Yup.string().default((_, { context }) => moment().tz(context.timezone).endOf('day').toISOString()), });

const defaultValues = schema.getDefault({ context: { timezone: 'UTC' }}); console.log(defaultValues); // { from: '2023-04-25T00:00:00.000Z', until: '2023-04-25T23:59:59.999Z' }

the default function takes two arguments: the first argument represents the value of the field (which is not used in this case), and the second argument represents the context object. The context.timezone property is used to get the timezone for the moment().tz() function calls. I hope this helps.

@AlimovSV
Copy link
Author

@Edleychris thank you for your reply! Unfortunately, your code doesn't work for me (latest yup@1.1.1).

schema.getDefault({ context: { timezone: 'UTC' }});
Uncaught:
TypeError: Cannot destructure property 'context' of 'undefined' as it is undefined.
    at StringSchema.<anonymous> (REPL3:1:165)
    at StringSchema._getDefault (/Users/falcon/Repositories/gosource/parks/parks-products-frontend/node_modules/yup/index.js:796:62)
    at StringSchema.getDefault (/Users/falcon/Repositories/gosource/parks/parks-products-frontend/node_modules/yup/index.js:802:19)
    at /Users/falcon/Repositories/gosource/parks/parks-products-frontend/node_modules/yup/index.js:1793:57
    at Array.forEach (<anonymous>)
    at ObjectSchema._getDefault (/Users/falcon/Repositories/gosource/parks/parks-products-frontend/node_modules/yup/index.js:1783:17)
    at ObjectSchema.getDefault (/Users/falcon/Repositories/gosource/parks/parks-products-frontend/node_modules/yup/index.js:802:19)

In the master, I see that getDefault has been calling without any arguments: https://github.com/jquense/yup/blob/master/src/schema.ts#L610-L620 that explains why I see a null-reference error. What yup version did you try?

@AlimovSV
Copy link
Author

Any news?

@AlimovSV
Copy link
Author

@jquense Thank you very much, I hope this change will be released soon.

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

2 participants