Skip to content

Commit

Permalink
address linter changes
Browse files Browse the repository at this point in the history
  • Loading branch information
hachibeeDI committed Feb 20, 2024
1 parent d91ff9b commit 00ccf6f
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 19 deletions.
1 change: 1 addition & 0 deletions src/index.test.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import {render, screen, waitFor} from '@testing-library/react';
import userEvent from '@testing-library/user-event';

import React from 'react';

import {test, expect} from 'vitest';
Expand Down
47 changes: 28 additions & 19 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,10 +120,13 @@ function createFormStore<StateBeforeValidation extends StateRestriction, Schema
type StateKeys = keyof StateBeforeValidation;

const initialErrors = Object.freeze(
Object.keys(initialState).reduce((buf, k) => {
buf[k as StateKeys] = null;
return buf;
}, {} as any as Err),
Object.keys(initialState).reduce(
(buf, k) => {
buf[k as StateKeys] = null;
return buf;
},
{} as any as Err,
),
);

const fullInitialState: FullState = Object.freeze({
Expand All @@ -137,15 +140,18 @@ function createFormStore<StateBeforeValidation extends StateRestriction, Schema
if (result.success) {
return {...prev, isDirty: true, isValid: true, value: newValue, errors: initialErrors};
}
const newErrors = result.error.issues.reduce((buf, iss) => {
// TODO: should support nested value?
const shallowPath = iss.path[0];
if (shallowPath === undefined) {
const newErrors = result.error.issues.reduce(
(buf, iss) => {
// TODO: should support nested value?
const shallowPath = iss.path[0];
if (shallowPath === undefined) {
return buf;
}
buf[shallowPath as any as StateKeys] = iss.message;
return buf;
}
buf[shallowPath as any as StateKeys] = iss.message;
return buf;
}, {} as any as Err);
},
{} as any as Err,
);

return {...prev, isDirty: true, isValid: false, errors: newErrors, value: newValue};
};
Expand All @@ -165,14 +171,17 @@ function createFormStore<StateBeforeValidation extends StateRestriction, Schema
}),
handleIssues: (issues) => (prev) => {
// FIXME: handle duplication
const newErrors = issues.reduce((buf, iss) => {
const shallowPath = iss.path[0];
if (shallowPath === undefined) {
const newErrors = issues.reduce(
(buf, iss) => {
const shallowPath = iss.path[0];
if (shallowPath === undefined) {
return buf;
}
buf[shallowPath as any as StateKeys] = iss.message;
return buf;
}
buf[shallowPath as any as StateKeys] = iss.message;
return buf;
}, {} as any as Err);
},
{} as any as Err,
);

return {...prev, isDirty: true, isValid: false, errors: newErrors};
},
Expand Down

0 comments on commit 00ccf6f

Please sign in to comment.