Skip to content

Commit

Permalink
feat: add toFormikValidate function
Browse files Browse the repository at this point in the history
  • Loading branch information
amatiasq committed May 4, 2022
1 parent 593d4f8 commit d787897
Show file tree
Hide file tree
Showing 3 changed files with 82 additions and 3 deletions.
21 changes: 20 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

[![codecov](https://codecov.io/gh/robertLichtnow/zod-formik-adapter/branch/master/graph/badge.svg?token=Z5V1VKCGV9)](https://codecov.io/gh/robertLichtnow/zod-formik-adapter)

This library adapts a [zod](https://www.npmjs.com/package/zod) schema to work as a `validationSchema` prop on [Formik](https://www.npmjs.com/package/formik)
This library adapts a [zod](https://www.npmjs.com/package/zod) schema to work as a `validationSchema` prop or `validate` prop on [Formik](https://www.npmjs.com/package/formik)

**IMPORTANT: Currently, this library does not work with zod union. See more [here](https://github.com/robertLichtnow/zod-formik-adapter/issues/2).**

Expand Down Expand Up @@ -36,3 +36,22 @@ const Component = () => (
</Formik>
);
```

```TSX
import { z } from 'zod';
import { Formik } from 'formik';
import { toFormikValidate } from 'zod-formik-adapter';

const Schema = z.object({
name: z.string(),
age: z.number(),
});

const Component = () => (
<Formik
validate={toFormikValidate(Schema)}
>
{...}
</Formik>
);
```
29 changes: 28 additions & 1 deletion index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -38,4 +38,31 @@ export function toFormikValidationSchema<T>(
}
},
};
}
}

function createValidationResult(error: z.ZodError) {
const result: Record<string, string> = {};

for (const x of error.errors) {
result[x.path.filter(Boolean).join(".")] = x.message;
}

return result;
}

/**
* Wrap your zod schema in this function when providing it to Formik's validate prop
* @param schema The zod schema
* @returns An validate function as expected by Formik
*/
export function toFormikValidate<T>(
schema: z.ZodSchema<T>,
params?: Partial<z.ParseParams>
) {
return async (values: T) => {
const result = await schema.safeParseAsync(values, params);
if (!result.success) {
return createValidationResult(result.error);
}
};
}
35 changes: 34 additions & 1 deletion tests/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from "zod";
import { toFormikValidationSchema, ValidationError } from '../index';
import { toFormikValidationSchema, toFormikValidate } from '../index';

describe("toFormikValidationSchema", () => {
it("should pass validate without errors", async () => {
Expand Down Expand Up @@ -40,6 +40,39 @@ describe("toFormikValidationSchema", () => {
});
});

describe("toFormikValidate", () => {
it("should pass validate without errors", async () => {
// given
const object = { name: "mock", age: 32 };
const { schema } = makeSut();
const validate = toFormikValidate(schema);

// when
const errors = await validate(object);

// then
expect(errors).toEqual(undefined);
});

it("should fail validate with error object", async () => {
// given
const object = { name: undefined, age: "32" } as any;
const { schema } = makeSut();
const validate = toFormikValidate(schema);

const error = {
name: "Required",
age: "Expected number, received string"
};

// when
const errors = await validate(object)

// then
expect(errors).toMatchObject(error);
});
});



function makeSut() {
Expand Down

0 comments on commit d787897

Please sign in to comment.