Skip to content

Commit

Permalink
feat: setFormDataToForm support arrays now (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
sahinvardar authored Nov 6, 2023
1 parent cf28d23 commit f746636
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 14 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.11",
"version": "0.1.12",
"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.11",
"version": "0.1.12",
"private": true,
"scripts": {
"dev": "next dev",
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.11",
"version": "0.1.12",
"description": "",
"main": "dist/index.js",
"type": "module",
Expand Down
23 changes: 13 additions & 10 deletions library/src/form-validation.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,16 +6,19 @@ export const DATA_VALIDATION_ERROR_MESSAGE_ATTRIBUTE_NAME = 'data-validation-err
export const DATA_VALIDATION_REQUIRED_ATTRIBUTE_NAME = 'data-validation-required';

function setSelectElementValue(selectElement: HTMLSelectElement, values: string[]) {
for (const child of selectElement.children) {
if (child.nodeName === 'OPTION') {
const optionElement = child as HTMLOptionElement;
optionElement.removeAttribute('selected');

if (values.includes(optionElement.value)) {
optionElement.setAttribute('selected', '');
}
}
}
const options = [...selectElement.children].filter((item) => item.nodeName === 'OPTION') as HTMLOptionElement[];
options.forEach((option) => option.removeAttribute('selected'));

const selectedOptions = options.filter((option) => values.includes(option.value));
selectedOptions.forEach((option) => option.setAttribute('selected', ''));

const valueDiff = values.filter((value) => options.find((option) => option.value === value) === undefined);
valueDiff.forEach((value) => {
const option = document.createElement('option');
option.setAttribute('value', value);
option.setAttribute('selected', '');
selectElement.add(option);
});
}

function setInputElementValue(inputElement: HTMLInputElement, value: string) {
Expand Down
26 changes: 26 additions & 0 deletions library/src/tests/form-validation.test.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import { JSDOM } from 'jsdom';
import { describe, expect, test } from 'vitest';
import { z } from 'zod';
import {
setDataToForm,
setFormDataToForm,
Expand All @@ -18,6 +20,30 @@ describe('Form Validation', () => {
const formData = new FormData(form!);

expect(formDataToData(formData, SCHEMA)).toStrictEqual(EXPECTED_DATA);

const arraySchema = z.object({
array: z.array(z.number()),
});
const formWithSelectDom = new JSDOM('<form><select multiple name="array"></select></form>');
const arrayFrom = formWithSelectDom.window.document.querySelector('form');
expect(arrayFrom).not.toBeNull();

let arrayFormDataA = new FormData(arrayFrom!);
let arrayFormDataB = new FormData(arrayFrom!);

const arrayValuesA = [0, 1, 2, 3, 4];
const arrayValuesB = [0, 3, 4];

arrayValuesA.forEach((value) => arrayFormDataA.append('array', value.toString()));
arrayValuesB.forEach((value) => arrayFormDataB.append('array', value.toString()));

setFormDataToForm(arrayFrom!, arrayFormDataA);
arrayFormDataA = new FormData(arrayFrom!);
expect(formDataToData(arrayFormDataA, arraySchema)).toStrictEqual({ array: arrayValuesA });

setFormDataToForm(arrayFrom!, arrayFormDataB);
arrayFormDataB = new FormData(arrayFrom!);
expect(formDataToData(arrayFormDataB, arraySchema)).toStrictEqual({ array: arrayValuesB });
});

test('setDataToForm', () => {
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.11",
"version": "0.1.12",
"license": "MIT",
"repository": {
"type": "git",
Expand Down

0 comments on commit f746636

Please sign in to comment.