Skip to content

Commit

Permalink
feat: added formDataToData and type fixes (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar authored Nov 1, 2023
1 parent a9c2e8e commit a899959
Show file tree
Hide file tree
Showing 9 changed files with 35 additions and 9 deletions.
2 changes: 1 addition & 1 deletion examples/html/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "zod-form-validation-html-example",
"version": "0.1.6",
"version": "0.1.7",
"description": "",
"main": "index.html",
"scripts": {
Expand Down
2 changes: 1 addition & 1 deletion examples/next/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react",
"version": "0.1.6",
"version": "0.1.7",
"private": true,
"scripts": {
"dev": "next dev",
Expand Down
2 changes: 1 addition & 1 deletion examples/svelte/src/lib/components/form.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
let doValidateOnInput = false;
$: {
form && data && setFormDataToForm(form, objectToFormData(data));
form && data && setFormDataToForm(form, data);
data && console.log( objectToFormData(data))
}
Expand Down
2 changes: 1 addition & 1 deletion library/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@vardario/zod-form-validation",
"version": "0.1.6",
"version": "0.1.7",
"description": "",
"main": "dist/index.js",
"type": "module",
Expand Down
7 changes: 6 additions & 1 deletion library/src/form-validation.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { z } from 'zod';
import { flattenSchema, formDataToObject, groupIssuesByName, preprocess } from './utils.js';
import { flattenSchema, formDataToObject, groupIssuesByName, objectToFormData, preprocess } from './utils.js';

export const DATA_VALIDATION_ERROR_ATTRIBUTE_NAME = 'data-validation-error';
export const DATA_VALIDATION_ERROR_MESSAGE_ATTRIBUTE_NAME = 'data-validation-error-message';
Expand Down Expand Up @@ -50,6 +50,11 @@ export function setFormDataToForm(form: HTMLFormElement, formData: FormData) {
}
}

export function setDataToForm(form: HTMLFormElement, data: any) {
const formData = objectToFormData(data);
setFormDataToForm(form, formData);
}

export function validateFormData<TSchema extends z.Schema>(formData: FormData, schema: TSchema) {
const object = preprocess(formDataToObject(formData), schema);
return schema.safeParse(object);
Expand Down
15 changes: 13 additions & 2 deletions library/src/tests/form-validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import { describe, expect, test } from 'vitest';
import {
setDataToForm,
setFormDataToForm,
setRequiresToForm,
setValidationErrorsToForm,
validateFormData,
} from '../form-validation.js';
import { formDataToObject, preprocess } from '../utils.js';
import { formDataToData } from '../utils.js';
import { EXPECTED_DATA, SCHEMA, getDom, getFormData } from './test-fixtures.js';

describe('Form Validation', () => {
Expand All @@ -16,7 +17,17 @@ describe('Form Validation', () => {
setFormDataToForm(form!, getFormData());
const formData = new FormData(form!);

expect(preprocess(formDataToObject(formData), SCHEMA)).toStrictEqual(EXPECTED_DATA);
expect(formDataToData(formData, SCHEMA)).toStrictEqual(EXPECTED_DATA);
});

test('setDataToForm', () => {
const dom = getDom();
const form = dom.window.document.querySelector('form');
expect(form).not.toBeNull();

setDataToForm(form!, EXPECTED_DATA);
const formData = new FormData(form!);
expect(formDataToData(formData, SCHEMA)).toStrictEqual(EXPECTED_DATA);
});

test('validateFormData', () => {
Expand Down
5 changes: 5 additions & 0 deletions library/src/tests/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { describe, expect, test } from 'vitest';
import {
flattenObject,
flattenSchema,
formDataToData,
formDataToObject,
objectToFormData,
parseFormData,
Expand Down Expand Up @@ -103,4 +104,8 @@ describe('utils', () => {
expect(result.data).toStrictEqual(EXPECTED_DATA);
}
});

test('formDataToData', () => {
expect(formDataToData(getFormData(), SCHEMA)).toStrictEqual(EXPECTED_DATA);
});
});
7 changes: 6 additions & 1 deletion library/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,12 @@ export function formDataToObject(formData: FormData) {
return result;
}

export function objectToFormData(obj: ObjectType) {
export function formDataToData<TSchema extends z.ZodSchema>(formData: FormData, schema: TSchema){
return preprocess(formDataToObject(formData), schema);

}

export function objectToFormData(obj: any) {
const flatObject = flattenObject(obj) as Record<string, any>;

const formData = new FormData();
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
{
"private": true,
"name": "root",
"version": "0.1.6",
"version": "0.1.7",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit a899959

Please sign in to comment.