Skip to content

Commit 5c912b2

Browse files
committed
TODO cleanup
1 parent 691892b commit 5c912b2

File tree

9 files changed

+10
-115
lines changed

9 files changed

+10
-115
lines changed

CHANGELOG.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,15 +7,14 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
TODO:
99

10-
- superValidateSync cannot use superValidate anymore since TypeSchema is async.
11-
- Remove fields options for setting tainted
1210
- Default options should follow SvelteKit: resetForm: true and taintedMessage: false (only error behavior is kept)
1311

1412
## [Unreleased]
1513

1614
### Removed
1715

1816
- superForm.fields
17+
- `fields` options for setting tainted.
1918

2019
### Changed
2120

src/lib/client/superForm.ts

+7-8
Original file line numberDiff line numberDiff line change
@@ -357,8 +357,6 @@ export function superForm<
357357
if (options.validationMethod == 'onblur' && event.type == 'input') return;
358358
if (options.validationMethod == 'oninput' && event.type == 'blur') return;
359359

360-
// TODO: What to do with a programmatic change event?
361-
362360
const result = await Form_validate();
363361

364362
if (result.valid) {
@@ -379,9 +377,11 @@ export function superForm<
379377
//console.log('Form__displayNewErrors', errors); //debug
380378

381379
const { type, immediate, multiple, paths } = event;
382-
//const isProgrammaticEvent = !type;
383380
const previous = Data.errors;
384381

382+
// TODO: What to do with a programmatic change event?
383+
//const isProgrammaticEvent = !type;
384+
385385
const output: Record<string, unknown> = {};
386386
const validity = new Map<string, { el: HTMLElement; message: string }>();
387387

@@ -563,9 +563,7 @@ export function superForm<
563563

564564
let NextChange: ChangeEvent | null = null;
565565

566-
function NextChange_addValidationEvent(event: ChangeEvent) {
567-
// TODO: What to do with more than one path (programmically updated)
568-
566+
function NextChange_setHtmlEvent(event: ChangeEvent) {
569567
NextChange = event;
570568
// Wait for on:input to provide additional information
571569
setTimeout(() => Form_clientValidation(NextChange), 0);
@@ -676,7 +674,7 @@ export function superForm<
676674
}
677675
}
678676

679-
NextChange_addValidationEvent({ paths });
677+
NextChange_setHtmlEvent({ paths });
680678
}
681679

682680
/**
@@ -685,6 +683,7 @@ export function superForm<
685683
* @param newClean
686684
*/
687685
function Tainted_set(tainted: TaintedFields<T> | undefined, newClean: T | undefined) {
686+
// TODO: Is it better to set tainted values to undefined instead of just overwriting?
688687
Tainted.state.set(tainted);
689688
if (newClean) Tainted.clean = newClean;
690689
}
@@ -971,7 +970,7 @@ export function superForm<
971970

972971
let lastInputChange: ChangeEvent['paths'] | undefined;
973972

974-
// TODO: Debounce?
973+
// TODO: Debounce option?
975974
async function onInput(e: Event) {
976975
const info = inputInfo(e.target);
977976
// Need to wait for immediate updates due to some timing issue

src/lib/errors.ts

-83
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { ValidationIssue } from '@decs/typeschema';
33
import { pathExists, setPaths, traversePath, traversePaths } from './traversal.js';
44
import { SuperFormError, type ValidationErrors } from './index.js';
55
import { mergePath } from './stringPath.js';
6-
import type { FormOptions } from './client/index.js';
76

87
export function mapErrors(errors: ValidationIssue[], shape: SchemaShape) {
98
//console.log('===', errors.length, 'errors', shape);
@@ -91,88 +90,6 @@ export function updateErrors<T extends Record<string, unknown>>(
9190
return Previous;
9291
}
9392

94-
/*
95-
96-
console.log('Checking new error', error.path, error.value);
97-
98-
const isObjectError = error.path[error.path.length - 1] == '_errors';
99-
let previousError = pathExists(Previous, error.path);
100-
101-
if (!previousError) {
102-
// An object error should be displayed on blur if no error exists
103-
if (event != 'blur') return;
104-
105-
if (isObjectError) {
106-
setPaths(Previous, [error.path], error.value);
107-
return;
108-
}
109-
110-
setPaths(Previous, [error.path], undefined);
111-
previousError = pathExists(Previous, error.path)!;
112-
}
113-
114-
switch (method) {
115-
case undefined:
116-
case 'auto':
117-
if (previousError.key in previousError.parent) {
118-
console.log('Error key existed, setting', previousError.path);
119-
previousError.set(error.value);
120-
break;
121-
} else if (
122-
isObjectError ||
123-
(event == 'blur' &&
124-
error.value &&
125-
LastChanges.map((c) => c.join()).includes(error.path.join()))
126-
) {
127-
previousError.set(error.value);
128-
}
129-
130-
break;
131-
132-
/*
133-
case 'onblur':
134-
if (event == 'blur') previousError.set(data.value);
135-
break;
136-
137-
case 'oninput':
138-
if (event == 'input') previousError.set(data.value);
139-
break;
140-
141-
case 'submit-only':
142-
if (event == 'submit') previousError.set(data.value);
143-
break;
144-
*/
145-
//path?.set(data.value);
146-
147-
/*
148-
export function clearErrors<T extends Record<string, unknown>>(
149-
Errors: Writable<ValidationErrors<T>>,
150-
options: {
151-
undefinePath: (string | number | symbol)[] | null;
152-
clearFormLevelErrors: boolean;
153-
}
154-
) {
155-
Errors.update(($errors) => {
156-
traversePaths($errors, (pathData) => {
157-
if (
158-
pathData.path.length == 1 &&
159-
pathData.path[0] == '_errors' &&
160-
!options.clearFormLevelErrors
161-
) {
162-
return;
163-
}
164-
if (Array.isArray(pathData.value)) {
165-
return pathData.set(undefined);
166-
}
167-
});
168-
169-
if (options.undefinePath) setPaths($errors, [options.undefinePath], undefined);
170-
171-
return $errors;
172-
});
173-
}
174-
*/
175-
17693
export function flattenErrors(errors: ValidationErrors<Record<string, unknown>>) {
17794
return _flattenErrors(errors, []);
17895
}

src/lib/formData.ts

-16
Original file line numberDiff line numberDiff line change
@@ -265,20 +265,4 @@ function parseFormDataEntry(key: string, value: string, info: SchemaInfo): unkno
265265
default:
266266
throw new SuperFormError('Unsupported schema type for FormData: ' + type);
267267
}
268-
269-
// TODO: Enum parsing
270-
/*
271-
} else if (type == 'NativeEnum') {
272-
const zodEnum = zodType as ZodNativeEnum<EnumLike>;
273-
274-
if (value !== null && value in zodEnum.enum) {
275-
const enumValue = zodEnum.enum[value];
276-
if (typeof enumValue === 'number') return enumValue;
277-
else if (enumValue in zodEnum.enum) return zodEnum.enum[enumValue];
278-
} else if (value !== null && Object.values(zodEnum.enum).includes(value)) {
279-
return value;
280-
}
281-
return undefined;
282-
}
283-
*/
284268
}

src/lib/index.ts

-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,6 @@ export type TaintedFields<T extends Record<string, unknown>> = SuperStructArray<
7171

7272
// Cannot be a SuperStruct due to Property having to be passed on.
7373
// Deep recursive problem fixed thanks to https://www.angularfix.com/2022/01/why-am-i-getting-instantiation-is.html
74-
// TODO: Parameter for validator function must be optional
7574
export type Validators<T extends Record<string, unknown>> = Partial<{
7675
// eslint-disable-next-line @typescript-eslint/no-explicit-any
7776
[Property in keyof T]: T extends any

src/lib/traversal.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ function setPath<T extends object>(parent: T, key: keyof T, value: any) {
1616
return 'skip' as const;
1717
}
1818

19-
export function isInvalidPath(originalPath: (string | number | symbol)[], pathData: PathData) {
19+
function isInvalidPath(originalPath: (string | number | symbol)[], pathData: PathData) {
2020
return (
2121
pathData.value !== undefined &&
2222
typeof pathData.value !== 'object' &&
@@ -68,7 +68,7 @@ export function traversePath<T extends object>(
6868
: parent[key];
6969

7070
if (value === undefined) return undefined;
71-
else parent = value as T; // TODO: Handle non-object values
71+
else parent = value as T;
7272

7373
path.push(realPath[path.length]);
7474
}

src/routes/(v1)/tests/issue-176/+page.svelte

-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@
2020
setError(form, 'email', 'Suspicious email address.');
2121
} else if (form.valid) {
2222
setMessage(form, 'Valid data!');
23-
// TODO: Do something with the validated data
2423
}
2524
},
2625
onError({ result, message }) {

src/routes/(v1)/tests/reset-component/Form.svelte

-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,6 @@
2525
onUpdate({ form }) {
2626
if (form.valid) {
2727
console.log('form is valid', form.data);
28-
// TODO: Do something with the validated form.data
2928
}
3029
}
3130
});

src/tests/JSONSchema.test.ts

-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,6 @@ describe('Default values', () => {
117117
} satisfies JSONSchema7;
118118

119119
expect(defaultValues(schema)).toEqual({
120-
// TODO: Default value for an array with an item that has a default value?
121120
numberArray: []
122121
});
123122
});

0 commit comments

Comments
 (0)