formik-validator-zod 2.1.0 → 2.2.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 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:
@@ -49,5 +45,5 @@ Yes! This library is used in a couple of production codebases that I'm aware of,
49
45
  including my current employers.
50
46
 
51
47
  I don't expect the library will need a lot of active maintenance going forwards.
52
- This is due to its limited scope and the fact Formik itsely seems to be
48
+ This is due to its limited scope and the fact Formik itself seems to be
53
49
  abandoned.
@@ -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;
@@ -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
- if (curr.path.length) {
16
- return merge(acc, curr.path.reduceRight((errors, pathSegment) => ({
17
- [pathSegment]: !Object.keys(errors).length
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": "2.1.0",
3
+ "version": "2.2.0",
4
4
  "description": "Helper to simplify validating Formik values with Zod",
5
5
  "type": "module",
6
- "main": "dist/index.js",
7
- "types": "dist/index.d.ts",
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",
15
+ "build": "npm-run-all -p build:esm build:cjs",
11
16
  "test": "vitest"
12
17
  },
13
18
  "keywords": [
@@ -26,6 +31,7 @@
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
36
  "typescript": "^4.8.4",
31
37
  "vitest": "^0.24.3",
@@ -1,34 +0,0 @@
1
- # This workflow will run tests using node and then publish a package to GitHub Packages when a release is created
2
- # For more information see: https://docs.github.com/en/actions/publishing-packages/publishing-nodejs-packages
3
-
4
- name: Node.js Package
5
-
6
- on:
7
- release:
8
- types: [created]
9
-
10
- jobs:
11
- build:
12
- runs-on: ubuntu-latest
13
- steps:
14
- - uses: actions/checkout@v4
15
- - uses: actions/setup-node@v4
16
- with:
17
- node-version: 20
18
- - run: npm ci
19
- - run: npm test
20
-
21
- publish-npm:
22
- needs: build
23
- runs-on: ubuntu-latest
24
- steps:
25
- - uses: actions/checkout@v4
26
- - uses: actions/setup-node@v4
27
- with:
28
- node-version: 20
29
- registry-url: https://registry.npmjs.org/
30
- - run: npm ci
31
- - run: npm run build
32
- - run: npm publish
33
- env:
34
- NODE_AUTH_TOKEN: ${{secrets.npm_token}}
@@ -1,26 +0,0 @@
1
- name: Tests
2
- on:
3
- push:
4
- branches: ['main']
5
- pull_request:
6
- branches: ['main']
7
- jobs:
8
- build:
9
- runs-on: ubuntu-latest
10
- strategy:
11
- matrix:
12
- node-version: [18.x, 20.x, 22.x]
13
- steps:
14
- - name: Checkout repo
15
- uses: actions/checkout@v4
16
- - name: Use Node.js ${{ matrix.node-version }}
17
- uses: actions/setup-node@v4
18
- with:
19
- node-version: ${{ matrix.node-version }}
20
- cache: 'npm'
21
- - name: Install dependencies
22
- run: npm ci
23
- - name: Check code style
24
- run: npx prettier . --check
25
- - name: Run tests
26
- run: npm test
package/jsr.json DELETED
@@ -1,6 +0,0 @@
1
- {
2
- "name": "@glazy/formik-validator-zod",
3
- "version": "2.0.1",
4
- "exports": "./lib/index.ts",
5
- "exclude": ["test/**", "package-lock.json", "tsconfig.json"]
6
- }
File without changes