formik-validator-zod 2.1.1 → 3.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +13 -11
- package/dist/cjs/index.js +26 -0
- package/dist/cjs/package.json +1 -0
- package/dist/esm/index.d.ts +12 -0
- package/dist/{index.js → esm/index.js} +3 -9
- package/package.json +14 -8
- package/dist/index.d.ts +0 -10
package/README.md
CHANGED
|
@@ -3,10 +3,6 @@
|
|
|
3
3
|
Allows you to easily validate [Formik](https://github.com/jaredpalmer/formik)
|
|
4
4
|
forms with the power of [Zod](https://github.com/colinhacks/zod) schemas.
|
|
5
5
|
|
|
6
|
-
As of v2.0, this package uses ESM and no longer provides a CommonJS export. If
|
|
7
|
-
this is something you need, you should be able to use the
|
|
8
|
-
[dynamic import](https://v8.dev/features/dynamic-import) function.
|
|
9
|
-
|
|
10
6
|
## Installation
|
|
11
7
|
|
|
12
8
|
To install from NPM:
|
|
@@ -29,7 +25,7 @@ import { z } from 'zod'
|
|
|
29
25
|
import { withZodSchema } from 'formik-validator-zod'
|
|
30
26
|
|
|
31
27
|
const mySchema = z.object({
|
|
32
|
-
email: z.
|
|
28
|
+
email: z.email(),
|
|
33
29
|
name: z.string(),
|
|
34
30
|
age: z.number(),
|
|
35
31
|
})
|
|
@@ -43,11 +39,17 @@ const MyForm = () => {
|
|
|
43
39
|
}
|
|
44
40
|
```
|
|
45
41
|
|
|
46
|
-
##
|
|
42
|
+
## Zod version support
|
|
43
|
+
|
|
44
|
+
| formik-validator-zod | Zod |
|
|
45
|
+
| -------------------- | --------- |
|
|
46
|
+
| `3.x` | `^4.0.0` |
|
|
47
|
+
| `2.x` | `^3.19.1` |
|
|
47
48
|
|
|
48
|
-
|
|
49
|
-
|
|
49
|
+
Version 3 of this library targets Zod 4. If you are still on Zod 3, stay on
|
|
50
|
+
`formik-validator-zod@2`.
|
|
51
|
+
|
|
52
|
+
## Is this library still maintained?
|
|
50
53
|
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
abandoned.
|
|
54
|
+
Yes. The library is small and stable, and it is kept up to date with new Zod
|
|
55
|
+
releases. Issues and pull requests are welcome.
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.withZodSchema = void 0;
|
|
7
|
+
const deepmerge_1 = __importDefault(require("deepmerge"));
|
|
8
|
+
/**
|
|
9
|
+
* Allows you to easily use Zod schemas with the <Formik /> component `validate`
|
|
10
|
+
* prop.
|
|
11
|
+
*
|
|
12
|
+
* ```js
|
|
13
|
+
* <Formik {...} validate={withZodSchema(yourSchema)}>
|
|
14
|
+
* ```
|
|
15
|
+
*/
|
|
16
|
+
const withZodSchema = (schema, params) => (values) => {
|
|
17
|
+
const result = schema.safeParse(values, params);
|
|
18
|
+
if (result.success)
|
|
19
|
+
return {};
|
|
20
|
+
return result.error.issues.reduce((acc, curr) => {
|
|
21
|
+
return (0, deepmerge_1.default)(acc, curr.path.reduceRight((errors, pathSegment) => ({
|
|
22
|
+
[pathSegment]: !Object.keys(errors).length ? curr.message : errors,
|
|
23
|
+
}), {}));
|
|
24
|
+
}, {});
|
|
25
|
+
};
|
|
26
|
+
exports.withZodSchema = withZodSchema;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"type":"commonjs"}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import type { ZodType } from 'zod';
|
|
2
|
+
type ParseParams = Parameters<ZodType['safeParse']>[1];
|
|
3
|
+
/**
|
|
4
|
+
* Allows you to easily use Zod schemas with the <Formik /> component `validate`
|
|
5
|
+
* prop.
|
|
6
|
+
*
|
|
7
|
+
* ```js
|
|
8
|
+
* <Formik {...} validate={withZodSchema(yourSchema)}>
|
|
9
|
+
* ```
|
|
10
|
+
*/
|
|
11
|
+
export declare const withZodSchema: <T>(schema: ZodType<T>, params?: ParseParams) => (values: T) => Partial<T>;
|
|
12
|
+
export {};
|
|
@@ -12,14 +12,8 @@ export const withZodSchema = (schema, params) => (values) => {
|
|
|
12
12
|
if (result.success)
|
|
13
13
|
return {};
|
|
14
14
|
return result.error.issues.reduce((acc, curr) => {
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
? curr.message
|
|
19
|
-
: errors,
|
|
20
|
-
}), {}));
|
|
21
|
-
}
|
|
22
|
-
const key = curr.path[0];
|
|
23
|
-
return Object.assign(Object.assign({}, acc), { [key]: curr.message });
|
|
15
|
+
return merge(acc, curr.path.reduceRight((errors, pathSegment) => ({
|
|
16
|
+
[pathSegment]: !Object.keys(errors).length ? curr.message : errors,
|
|
17
|
+
}), {}));
|
|
24
18
|
}, {});
|
|
25
19
|
};
|
package/package.json
CHANGED
|
@@ -1,13 +1,18 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "formik-validator-zod",
|
|
3
|
-
"version": "
|
|
3
|
+
"version": "3.0.0",
|
|
4
4
|
"description": "Helper to simplify validating Formik values with Zod",
|
|
5
5
|
"type": "module",
|
|
6
|
-
"
|
|
7
|
-
"
|
|
6
|
+
"types": "./dist/esm/index.d.ts",
|
|
7
|
+
"exports": {
|
|
8
|
+
"import": "./dist/esm/index.js",
|
|
9
|
+
"require": "./dist/cjs/index.js"
|
|
10
|
+
},
|
|
8
11
|
"scripts": {
|
|
9
12
|
"prepare": "husky install",
|
|
10
|
-
"build": "tsc",
|
|
13
|
+
"build:esm": "tsc -p tsconfig.esm.json",
|
|
14
|
+
"build:cjs": "tsc -p tsconfig.cjs.json && echo '{\"type\":\"commonjs\"}' > dist/cjs/package.json",
|
|
15
|
+
"build": "npm-run-all -p build:esm build:cjs",
|
|
11
16
|
"test": "vitest"
|
|
12
17
|
},
|
|
13
18
|
"keywords": [
|
|
@@ -20,20 +25,21 @@
|
|
|
20
25
|
"author": "Luke Glazebrook <Glazy@users.noreply.github.com>",
|
|
21
26
|
"repository": {
|
|
22
27
|
"type": "git",
|
|
23
|
-
"url": "https://github.com/
|
|
28
|
+
"url": "git+https://github.com/Glazy/formik-validator-zod.git"
|
|
24
29
|
},
|
|
25
30
|
"license": "MIT",
|
|
26
31
|
"devDependencies": {
|
|
27
32
|
"formik": "^2.2.9",
|
|
28
33
|
"husky": "^8.0.0",
|
|
34
|
+
"npm-run-all": "^4.1.5",
|
|
29
35
|
"pretty-quick": "^3.1.3",
|
|
30
|
-
"typescript": "^
|
|
36
|
+
"typescript": "^5.9.3",
|
|
31
37
|
"vitest": "^0.24.3",
|
|
32
|
-
"zod": "^
|
|
38
|
+
"zod": "^4.0.0"
|
|
33
39
|
},
|
|
34
40
|
"peerDependencies": {
|
|
35
41
|
"formik": "^2.2.9",
|
|
36
|
-
"zod": "^
|
|
42
|
+
"zod": "^4.0.0"
|
|
37
43
|
},
|
|
38
44
|
"dependencies": {
|
|
39
45
|
"deepmerge": "^4.3.1"
|
package/dist/index.d.ts
DELETED
|
@@ -1,10 +0,0 @@
|
|
|
1
|
-
import type { ZodSchema, ParseParams } from 'zod';
|
|
2
|
-
/**
|
|
3
|
-
* Allows you to easily use Zod schemas with the <Formik /> component `validate`
|
|
4
|
-
* prop.
|
|
5
|
-
*
|
|
6
|
-
* ```js
|
|
7
|
-
* <Formik {...} validate={withZodSchema(yourSchema)}>
|
|
8
|
-
* ```
|
|
9
|
-
*/
|
|
10
|
-
export declare const withZodSchema: <T>(schema: ZodSchema<T, import("zod").ZodTypeDef, T>, params?: Partial<ParseParams>) => (values: T) => Partial<T>;
|