Skip to content

Commit

Permalink
feat(typings): update yup types
Browse files Browse the repository at this point in the history
  • Loading branch information
ysfaran committed Nov 7, 2021
1 parent 4dbe084 commit 7efb239
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 35 deletions.
53 changes: 34 additions & 19 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 9 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,13 @@
"dependencies": {},
"peerDependencies": {
"react": "^16.8.0 || ^17.0.1",
"react-dom": "^16.8.0 || ^17.0.1"
"react-dom": "^16.8.0 || ^17.0.1",
"yup": "^0.32.11"
},
"peerDependenciesMeta": {
"yup": {
"optional": true
}
},
"devDependencies": {
"@babel/preset-env": "^7.12.7",
Expand All @@ -54,7 +60,7 @@
"@types/jest": "^27.0.2",
"@types/react": "^17.0.0",
"@types/react-dom": "^17.0.0",
"@types/yup": "^0.29.9",
"@types/yup": "^0.29.13",
"@typescript-eslint/eslint-plugin": "^5.3.0",
"@typescript-eslint/parser": "^5.3.0",
"babel-jest": "^27.3.1",
Expand All @@ -78,7 +84,7 @@
"rollup-plugin-typescript2": "^0.29.0",
"ts-jest": "^27.0.7",
"typescript": "^4.1.2",
"yup": "^0.31.0"
"yup": "^0.32.11"
},
"keywords": [
"react",
Expand Down
14 changes: 7 additions & 7 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -385,11 +385,11 @@ export type ValidateFunction<
value: ValuesType[K],
values: ValuesType,
context: Context
) => yup.Schema<any> | Error | undefined;
) => yup.AnySchema | Error | undefined;

export type Validations<ValuesType> = {
[K in keyof ValuesType]?:
| yup.Schema<any>
| yup.AnySchema
| ValidateFunction<ValuesType, K, unknown>;
};

Expand All @@ -401,19 +401,19 @@ export type DefaultError<
};

export type DefaultValidationReturnType<
VF extends yup.Schema<any> | ValidateFunction<any, any> | undefined
> = VF extends yup.Schema<any>
VF extends yup.AnySchema | ValidateFunction<any, any> | undefined
> = VF extends yup.AnySchema
? string[]
: VF extends ValidateFunction<any, any, infer E>
? E extends yup.Schema<any>
? string[] | Exclude<E, yup.Schema<any>>
? E extends yup.AnySchema
? string[] | Exclude<E, yup.AnySchema>
: E
: never;

export interface ValidateYupSchemaArgs<ValuesType, K extends keyof ValuesType> {
value: ValuesType[K];
values: ValuesType;
schema: yup.Schema<any>;
schema: yup.AnySchema;
context: object;
}

Expand Down
10 changes: 5 additions & 5 deletions src/validation/DefaultValidator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ export class DefaultValidator<
}: ValidateFunctionArgs<ValuesType, K, E>) {
const schemaOrResult = validate(value, values, context);

const schema = schemaOrResult as yup.Schema<any>;
const schema = schemaOrResult as yup.AnySchema;

if (schema && schema.validateSync) {
if (isYupSchema(schema)) {
return this.validateYupSchema({ value, values, schema, context });
} else {
return schemaOrResult as E | undefined;
Expand All @@ -48,7 +48,7 @@ export class DefaultValidator<
}: ValidateYupSchemaArgs<ValuesType, K>) {
try {
schema.validateSync(value, { context: { ...values, ...context } });
} catch (err) {
} catch (err: any) {
if (err.name === "ValidationError") {
return err.errors;
} else {
Expand Down Expand Up @@ -81,12 +81,12 @@ export class DefaultValidator<
return this.validateYupSchema({
value,
values,
schema: validate as yup.Schema<any>,
schema: validate as yup.AnySchema,
context,
});
} else {
console.warn(
`Expected validation of type function or yup.Schema, but received type: ${typeof validate})`,
`Expected validation of type function or yup.AnySchema, but received type: ${typeof validate})`,
validate
);
}
Expand Down
2 changes: 1 addition & 1 deletion test/DefaultValidator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ describe("DefaultValidator", () => {
const validator = new DefaultValidator<TestModel>({
aString: yup.string().when("aString", {
is: () => 1 / 0,
then: "erro should be thrown before",
then: "error should be thrown before" as any,
}),
});

Expand Down

0 comments on commit 7efb239

Please sign in to comment.