formik-validator-zod 1.0.1
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/LICENSE +21 -0
- package/README.md +34 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +35 -0
- package/package.json +36 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2022 Luke Glazebrook
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
# Formik Validator Zod
|
|
2
|
+
|
|
3
|
+
Allows you to easily validate [Formik](https://github.com/jaredpalmer/formik)
|
|
4
|
+
forms with the power of [Zod](https://github.com/colinhacks/zod) schemas.
|
|
5
|
+
|
|
6
|
+
## Installation
|
|
7
|
+
|
|
8
|
+
```sh
|
|
9
|
+
npm install formik-validator-zod
|
|
10
|
+
|
|
11
|
+
yarn add formik-validator-zod
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Example
|
|
15
|
+
|
|
16
|
+
```jsx
|
|
17
|
+
import { Formik } from 'formik'
|
|
18
|
+
import { z } from 'zod'
|
|
19
|
+
import { withZodSchema } from 'formik-validator-zod'
|
|
20
|
+
|
|
21
|
+
const mySchema = z.object({
|
|
22
|
+
email: z.string().email(),
|
|
23
|
+
name: z.string(),
|
|
24
|
+
age: z.number(),
|
|
25
|
+
})
|
|
26
|
+
|
|
27
|
+
const MyForm = () => {
|
|
28
|
+
return (
|
|
29
|
+
<Formik validate={withZodSchema(mySchema)} {...}>
|
|
30
|
+
{...}
|
|
31
|
+
</Formik>
|
|
32
|
+
)
|
|
33
|
+
}
|
|
34
|
+
```
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
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>;
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __assign = (this && this.__assign) || function () {
|
|
3
|
+
__assign = Object.assign || function(t) {
|
|
4
|
+
for (var s, i = 1, n = arguments.length; i < n; i++) {
|
|
5
|
+
s = arguments[i];
|
|
6
|
+
for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
|
7
|
+
t[p] = s[p];
|
|
8
|
+
}
|
|
9
|
+
return t;
|
|
10
|
+
};
|
|
11
|
+
return __assign.apply(this, arguments);
|
|
12
|
+
};
|
|
13
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
14
|
+
exports.withZodSchema = void 0;
|
|
15
|
+
/**
|
|
16
|
+
* Allows you to easily use Zod schemas with the <Formik /> component `validate`
|
|
17
|
+
* prop.
|
|
18
|
+
*
|
|
19
|
+
* ```js
|
|
20
|
+
* <Formik {...} validate={withZodSchema(yourSchema)}>
|
|
21
|
+
* ```
|
|
22
|
+
*/
|
|
23
|
+
var withZodSchema = function (schema, params) {
|
|
24
|
+
return function (values) {
|
|
25
|
+
var result = schema.safeParse(values, params);
|
|
26
|
+
if (result.success)
|
|
27
|
+
return {};
|
|
28
|
+
return result.error.issues.reduce(function (acc, curr) {
|
|
29
|
+
var _a;
|
|
30
|
+
var key = curr.path.join('.');
|
|
31
|
+
return __assign(__assign({}, acc), (_a = {}, _a[key] = curr.message, _a));
|
|
32
|
+
}, {});
|
|
33
|
+
};
|
|
34
|
+
};
|
|
35
|
+
exports.withZodSchema = withZodSchema;
|
package/package.json
ADDED
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "formik-validator-zod",
|
|
3
|
+
"version": "1.0.1",
|
|
4
|
+
"description": "Helper to simplify validating Formik values with Zod",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"scripts": {
|
|
7
|
+
"prepare": "husky install",
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "vitest"
|
|
10
|
+
},
|
|
11
|
+
"keywords": [
|
|
12
|
+
"zod",
|
|
13
|
+
"formik",
|
|
14
|
+
"forms",
|
|
15
|
+
"schema",
|
|
16
|
+
"validation"
|
|
17
|
+
],
|
|
18
|
+
"author": "Luke Glazebrook <Glazy@users.noreply.github.com> (https://glazebrook.dev)",
|
|
19
|
+
"repository": {
|
|
20
|
+
"type": "git",
|
|
21
|
+
"url": "https://github.com/glazy/formik-validator-zod.git"
|
|
22
|
+
},
|
|
23
|
+
"license": "MIT",
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"formik": "*",
|
|
26
|
+
"husky": "^8.0.0",
|
|
27
|
+
"pretty-quick": "^3.1.3",
|
|
28
|
+
"typescript": "^4.8.4",
|
|
29
|
+
"vitest": "^0.24.3",
|
|
30
|
+
"zod": "*"
|
|
31
|
+
},
|
|
32
|
+
"peerDependencies": {
|
|
33
|
+
"formik": "^2.2.9",
|
|
34
|
+
"zod": "^3.19.1"
|
|
35
|
+
}
|
|
36
|
+
}
|