-
-
Notifications
You must be signed in to change notification settings - Fork 169
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
Support Yup ^0.32.0 #92
Conversation
import Yup from 'yup'; | ||
|
||
/** | ||
* From 0.32.0, Yup add TypeScript support and `path` typing is optional that's why we have `@ts-expect-error` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bluebill1049 I'm not satisfied, if you've a better solution
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
FYI:
path
: a string, indicating where there error was thrown.path
is empty at the root level.
what do you mean by that?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sorry, I haven't played with the latest Yup, probably lack of context.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, path
is empty, basically means the object key is actually the path?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the new type for ValidationError is:
export default class ValidationError extends Error {
value: any;
path?: string;
type?: string;
errors: string[];
params?: Params;
inner: ValidationError[];
static formatError(message: string | ((params: Params) => string) | unknown, params: Params): any;
static isError(err: any): err is ValidationError;
constructor(errorOrErrors: string | ValidationError | ValidationError[], value?: any, field?: string, type?: string);
}
As you can see, path
& type
could be undefined
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hmm, interesting, let's go with the ignore
for now 👍 it's a bit inconsistent for the rest of the schema libs.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, PR is ready to review :)
@@ -42,7 +42,7 @@ const errors = { | |||
], | |||
}; | |||
|
|||
const schema = yup.object().shape({ | |||
const schema = yup.object({ |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh, shape
is gone. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Awesome stuff! 🥳 Thank you very much @jorisre
foo: [{ yup: true }], | ||
createdOn: new Date('2014-09-23T19:25:25Z'), | ||
}, | ||
values: data, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👍
expect(resolve.errors['foo'][0]['loose'].types).toMatchInlineSnapshot(` | ||
|
||
const output = await yupResolver(schema)( | ||
// @ts-expect-error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
minor (don't have to fix it): Is this due to reuse the same schema? maybe worth to use a new schema instead?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is normal because yupResolver
have a better type support now :)
The schema is good but passed data are intentionally false, that's why TS throw an error.
@@ -233,6 +244,8 @@ describe('validateWithSchema', () => { | |||
return min ? schema.min(6) : schema; | |||
}), | |||
}); | |||
|
|||
// @ts-expect-error |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
assume this is the same issue above?
export const yupResolver = <T extends Yup.AnyObjectSchema>( | ||
schema: T, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@sukjae 👋🏻
It's a mistake
Feel free to send a PR ;)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Related to #86 & #91
Yup@0.32.0 add Typescript support.
@types/yup
isn't required anymore.