Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(form/validation): TypedSchema Core #42

Merged
merged 6 commits into from
Aug 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 47 additions & 12 deletions packages/core/src/form/formContext.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,21 @@
import { Arrayable, DisabledSchema, FormObject, Path, PathValue, TouchedSchema, ValiditySchema } from '../types';
import { Ref } from 'vue';
import {
Arrayable,
DisabledSchema,
FormObject,
Path,
PathValue,
TouchedSchema,
TypedSchema,
ErrorsSchema,
TypedSchemaError,
} from '../types';
import { cloneDeep, merge, normalizeArrayable } from '../utils/common';
import { escapePath, findLeaf, getFromPath, isPathSet, setInPath, unsetPath as unsetInObject } from '../utils/path';
import { FormSnapshot } from './formSnapshot';

export type FormValidationMode = 'native' | 'schema';

export interface FormContext<TForm extends FormObject = FormObject> {
id: string;
getFieldValue<TPath extends Path<TForm>>(path: TPath): PathValue<TForm, TPath>;
Expand All @@ -20,6 +33,9 @@
setFieldDisabled<TPath extends Path<TForm>>(path: TPath, value: boolean): void;
getFieldErrors<TPath extends Path<TForm>>(path: TPath): string[];
setFieldErrors<TPath extends Path<TForm>>(path: TPath, message: Arrayable<string>): void;
getValidationMode(): FormValidationMode;
getErrors: () => TypedSchemaError[];
clearErrors: () => void;
hasErrors: () => boolean;
getValues: () => TForm;
setValues: (newValues: Partial<TForm>, opts?: SetValueOptions) => void;
Expand All @@ -31,26 +47,28 @@
mode: 'merge' | 'replace';
}

export interface FormContextCreateOptions<TForm extends FormObject = FormObject> {
export interface FormContextCreateOptions<TForm extends FormObject = FormObject, TOutput extends FormObject = TForm> {
id: string;
values: TForm;
touched: TouchedSchema<TForm>;
disabled: DisabledSchema<TForm>;
errors: ValiditySchema<TForm>;
errors: Ref<ErrorsSchema<TForm>>;
schema: TypedSchema<TForm, TOutput> | undefined;
snapshots: {
values: FormSnapshot<TForm>;
touched: FormSnapshot<TouchedSchema<TForm>>;
};
}

export function createFormContext<TForm extends FormObject = FormObject>({
export function createFormContext<TForm extends FormObject = FormObject, TOutput extends FormObject = TForm>({
id,
values,
disabled,
errors,
schema,
touched,
snapshots,
}: FormContextCreateOptions<TForm>): FormContext<TForm> {
}: FormContextCreateOptions<TForm, TOutput>): FormContext<TForm> {
function setFieldValue<TPath extends Path<TForm>>(path: TPath, value: PathValue<TForm, TPath> | undefined) {
setInPath(values, path, cloneDeep(value));
}
Expand All @@ -75,14 +93,14 @@
unsetInObject(values, path, true);
unsetInObject(touched, path, true);
unsetInObject(disabled, escapePath(path), true);
unsetInObject(errors, escapePath(path), true);
unsetInObject(errors.value, escapePath(path), true);
}

function unsetPath<TPath extends Path<TForm>>(path: TPath) {
unsetInObject(values, path, false);
unsetInObject(touched, path, false);
unsetInObject(disabled, escapePath(path), false);
unsetInObject(errors, escapePath(path), false);
unsetInObject(errors.value, escapePath(path), false);
}

function getFieldInitialValue<TPath extends Path<TForm>>(path: TPath) {
Expand All @@ -102,7 +120,13 @@
}

function hasErrors() {
return !!findLeaf(errors, l => Array.isArray(l) && l.length > 0);
return !!findLeaf(errors.value, l => Array.isArray(l) && l.length > 0);
}

function getErrors(): TypedSchemaError[] {
return Object.entries(errors.value)
.map<TypedSchemaError>(([key, value]) => ({ path: key, errors: value as string[] }))
.filter(e => e.errors.length > 0);
}

function setInitialValues(newValues: Partial<TForm>, opts?: SetValueOptions) {
Expand Down Expand Up @@ -140,23 +164,23 @@
return;
}

// Delete all keys, then set new values
// Clear the Object
Object.keys(values).forEach(key => {
delete values[key];
delete values[key as keyof typeof values];
});

// We escape paths automatically
Object.keys(newValues).forEach(key => {
setFieldValue(escapePath(key) as any, newValues[key]);

Check warning on line 174 in packages/core/src/form/formContext.ts

View workflow job for this annotation

GitHub Actions / ts-lint-test

Unexpected any. Specify a different type

Check warning on line 174 in packages/core/src/form/formContext.ts

View workflow job for this annotation

GitHub Actions / ts-lint-test

Unexpected any. Specify a different type
});
}

function getFieldErrors<TPath extends Path<TForm>>(path: TPath) {
return [...(getFromPath<string[]>(errors, escapePath(path), []) || [])];
return [...(getFromPath<string[]>(errors.value, escapePath(path), []) || [])];
}

function setFieldErrors<TPath extends Path<TForm>>(path: TPath, message: Arrayable<string>) {
setInPath(errors, escapePath(path), message ? normalizeArrayable(message) : []);
setInPath(errors.value, escapePath(path), message ? normalizeArrayable(message) : []);
}

function setTouched(newTouched: Partial<TouchedSchema<TForm>>, opts?: SetValueOptions) {
Expand All @@ -174,6 +198,10 @@
merge(touched, newTouched);
}

function clearErrors() {
errors.value = {} as ErrorsSchema<TForm>;
}

function revertValues() {
setValues(cloneDeep(snapshots.values.originals.value), { mode: 'replace' });
}
Expand All @@ -182,6 +210,10 @@
setTouched(cloneDeep(snapshots.touched.originals.value), { mode: 'replace' });
}

function getValidationMode(): FormValidationMode {
return schema ? 'schema' : 'native';
}

return {
id,
getValues: () => cloneDeep(values),
Expand All @@ -204,5 +236,8 @@
setFieldErrors,
getFieldErrors,
hasErrors,
getErrors,
clearErrors,
getValidationMode,
};
}
Loading
Loading