diff --git a/package-lock.json b/package-lock.json index 6df7ba6..3a5c0e8 100644 --- a/package-lock.json +++ b/package-lock.json @@ -5120,6 +5120,12 @@ "integrity": "sha512-qcUXuemtEu+E5wZSJHNxUXeCZhAfXKQ41D+duX+VYPde7xyEVZci+/oXKJL13tnRs9lR2pr4fod59GT6/X1/yQ==", "dev": true }, + "@types/lodash": { + "version": "4.14.176", + "resolved": "https://registry.npmjs.org/@types/lodash/-/lodash-4.14.176.tgz", + "integrity": "sha512-xZmuPTa3rlZoIbtDUyJKZQimJV3bxCmzMIO2c9Pz9afyDro6kr7R79GwcB6mRhuoPmV2p1Vb66WOJH7F886WKQ==", + "dev": true + }, "@types/minimist": { "version": "1.2.1", "resolved": "https://registry.npmjs.org/@types/minimist/-/minimist-1.2.1.tgz", @@ -5224,9 +5230,9 @@ "dev": true }, "@types/yup": { - "version": "0.29.9", - "resolved": "https://registry.npmjs.org/@types/yup/-/yup-0.29.9.tgz", - "integrity": "sha512-ZtjjlrHuHTYctHDz3c8XgInjj0v+Hahe32N/4cDa2banibf9w6aAgxwx0jZtBjKKzmGIU4NXhofEsBW1BbqrNg==", + "version": "0.29.13", + "resolved": "https://registry.npmjs.org/@types/yup/-/yup-0.29.13.tgz", + "integrity": "sha512-qRyuv+P/1t1JK1rA+elmK1MmCL1BapEzKKfbEhDBV/LMMse4lmhZ/XbgETI39JveDJRpLjmToOI6uFtMW/WR2g==", "dev": true }, "@typescript-eslint/eslint-plugin": { @@ -6351,7 +6357,8 @@ "dependencies": { "hosted-git-info": { "version": "2.8.8", - "resolved": "", + "resolved": "https://registry.npmjs.org/hosted-git-info/-/hosted-git-info-2.8.8.tgz", + "integrity": "sha512-f/wzC2QaWBs7t9IYqB4T3sR1xviIViXJRJTWBlx2Gf3g0Xi5vI7Yy4koXQ1c9OYDGHN9sBy1DQ2AB8fqZBWhUg==", "dev": true }, "normalize-package-data": { @@ -11317,9 +11324,9 @@ "dev": true }, "lodash-es": { - "version": "4.17.15", - "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.15.tgz", - "integrity": "sha512-rlrc3yU3+JNOpZ9zj5pQtxnx2THmvRykwL4Xlxoa8I9lHBlVbbyPhgyPMioxVZ4NqyxaVVtaJnzsyOidQIhyyQ==", + "version": "4.17.21", + "resolved": "https://registry.npmjs.org/lodash-es/-/lodash-es-4.17.21.tgz", + "integrity": "sha512-mKnC+QJ9pWVzv+C4/U3rRsHapFfHvQFoFB92e52xeyGMcX6/OlIl78je1u8vePzYZSkkogMPJ2yjxxsb89cxyw==", "dev": true }, "lodash._reinterpolate": { @@ -11621,6 +11628,12 @@ "integrity": "sha512-sGkPx+VjMtmA6MX27oA4FBFELFCZZ4S4XqeGOXCv68tT+jb3vk/RyaKWP0PTKyWtmLSM0b+adUTEvbs1PEaH2w==", "dev": true }, + "nanoclone": { + "version": "0.2.1", + "resolved": "https://registry.npmjs.org/nanoclone/-/nanoclone-0.2.1.tgz", + "integrity": "sha512-wynEP02LmIbLpcYw8uBKpcfF6dmg2vcpKqxeH5UcoKEYdExslsdUA4ugFauuaeYdTB76ez6gJW8XAZ6CgkXYxA==", + "dev": true + }, "natural-compare": { "version": "1.4.0", "resolved": "https://registry.npmjs.org/natural-compare/-/natural-compare-1.4.0.tgz", @@ -16065,31 +16078,33 @@ "dev": true }, "yup": { - "version": "0.31.0", - "resolved": "https://registry.npmjs.org/yup/-/yup-0.31.0.tgz", - "integrity": "sha512-t4nFalWdyIZW2n9wgJVracCe/VPbFmh0/fKaKz40gxcZHCZPOhm4akWYg08t0OOiBhV5xvqLY+fnqzCnLh+ItQ==", + "version": "0.32.11", + "resolved": "https://registry.npmjs.org/yup/-/yup-0.32.11.tgz", + "integrity": "sha512-Z2Fe1bn+eLstG8DRR6FTavGD+MeAwyfmouhHsIUgaADz8jvFKbO/fXc2trJKZg+5EBjh4gGm3iU/t3onKlXHIg==", "dev": true, "requires": { - "@babel/runtime": "^7.10.5", - "lodash": "^4.17.20", - "lodash-es": "^4.17.11", + "@babel/runtime": "^7.15.4", + "@types/lodash": "^4.14.175", + "lodash": "^4.17.21", + "lodash-es": "^4.17.21", + "nanoclone": "^0.2.1", "property-expr": "^2.0.4", "toposort": "^2.0.2" }, "dependencies": { "@babel/runtime": { - "version": "7.12.5", - "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.12.5.tgz", - "integrity": "sha512-plcc+hbExy3McchJCEQG3knOsuh3HH+Prx1P6cLIkET/0dLuQDEnrT+s27Axgc9bqfsmNUNHfscgMUdBpC9xfg==", + "version": "7.16.0", + "resolved": "https://registry.npmjs.org/@babel/runtime/-/runtime-7.16.0.tgz", + "integrity": "sha512-Nht8L0O8YCktmsDV6FqFue7vQLRx3Hb0B37lS5y0jDRqRxlBG4wIJHnf9/bgSE2UyipKFA01YtS+npRdTWBUyw==", "dev": true, "requires": { "regenerator-runtime": "^0.13.4" } }, "regenerator-runtime": { - "version": "0.13.7", - "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.7.tgz", - "integrity": "sha512-a54FxoJDIr27pgf7IgeQGxmqUNYrcV338lf/6gH456HZ/PhX+5BcwHXG9ajESmwe6WRO0tAzRUrRmNONWgkrew==", + "version": "0.13.9", + "resolved": "https://registry.npmjs.org/regenerator-runtime/-/regenerator-runtime-0.13.9.tgz", + "integrity": "sha512-p3VT+cOEgxFsRRA9X4lkI1E+k2/CtnKtU4gcxyaCUreilL/vqI6CdZ3wxVUx3UOUg+gnUOQQcRI7BmSI656MYA==", "dev": true } } diff --git a/package.json b/package.json index 2c917d9..ad5a64e 100644 --- a/package.json +++ b/package.json @@ -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", @@ -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", @@ -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", diff --git a/src/types.ts b/src/types.ts index 077865a..2c7b763 100644 --- a/src/types.ts +++ b/src/types.ts @@ -385,11 +385,11 @@ export type ValidateFunction< value: ValuesType[K], values: ValuesType, context: Context -) => yup.Schema | Error | undefined; +) => yup.AnySchema | Error | undefined; export type Validations = { [K in keyof ValuesType]?: - | yup.Schema + | yup.AnySchema | ValidateFunction; }; @@ -401,19 +401,19 @@ export type DefaultError< }; export type DefaultValidationReturnType< - VF extends yup.Schema | ValidateFunction | undefined -> = VF extends yup.Schema + VF extends yup.AnySchema | ValidateFunction | undefined +> = VF extends yup.AnySchema ? string[] : VF extends ValidateFunction - ? E extends yup.Schema - ? string[] | Exclude> + ? E extends yup.AnySchema + ? string[] | Exclude : E : never; export interface ValidateYupSchemaArgs { value: ValuesType[K]; values: ValuesType; - schema: yup.Schema; + schema: yup.AnySchema; context: object; } diff --git a/src/validation/DefaultValidator.ts b/src/validation/DefaultValidator.ts index 0768be4..567c064 100644 --- a/src/validation/DefaultValidator.ts +++ b/src/validation/DefaultValidator.ts @@ -31,9 +31,9 @@ export class DefaultValidator< }: ValidateFunctionArgs) { const schemaOrResult = validate(value, values, context); - const schema = schemaOrResult as yup.Schema; + 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; @@ -48,7 +48,7 @@ export class DefaultValidator< }: ValidateYupSchemaArgs) { try { schema.validateSync(value, { context: { ...values, ...context } }); - } catch (err) { + } catch (err: any) { if (err.name === "ValidationError") { return err.errors; } else { @@ -81,12 +81,12 @@ export class DefaultValidator< return this.validateYupSchema({ value, values, - schema: validate as yup.Schema, + 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 ); } diff --git a/test/DefaultValidator.test.ts b/test/DefaultValidator.test.ts index 51576c8..d34b594 100644 --- a/test/DefaultValidator.test.ts +++ b/test/DefaultValidator.test.ts @@ -42,7 +42,7 @@ describe("DefaultValidator", () => { const validator = new DefaultValidator({ aString: yup.string().when("aString", { is: () => 1 / 0, - then: "erro should be thrown before", + then: "error should be thrown before" as any, }), });