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

refactor: flatten #360

Closed
wants to merge 1 commit into from
Closed
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
88 changes: 56 additions & 32 deletions library/src/error/flatten/flatten.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ import type {
BaseSchema,
BaseSchemaAsync,
Input,
Issue,
Issues,
PathItem,
} from '../../types/index.ts';
import type { ValiError } from '../ValiError/index.ts';

Expand All @@ -49,7 +51,7 @@ type ObjectPath<TEntries extends ObjectEntries | ObjectEntriesAsync> = {
/**
* Tuple key type.
*/
type TupleKey<T extends any[]> = Exclude<keyof T, keyof any[]>;
type TupleKey<T extends unknown[]> = Exclude<keyof T, keyof unknown[]>;

/**
* Tuple path type.
Expand Down Expand Up @@ -118,7 +120,7 @@ type NestedPath<TSchema extends BaseSchema | BaseSchemaAsync> =
/**
* Flat errors type.
*/
export type FlatErrors<TSchema extends BaseSchema | BaseSchemaAsync = any> = {
export type FlatErrors<TSchema extends BaseSchema | BaseSchemaAsync> = {
root?: [string, ...string[]];
nested: Partial<Record<NestedPath<TSchema>, [string, ...string[]]>>;
};
Expand All @@ -130,7 +132,7 @@ export type FlatErrors<TSchema extends BaseSchema | BaseSchemaAsync = any> = {
*
* @returns Flat errors.
*/
export function flatten<TSchema extends BaseSchema | BaseSchemaAsync = any>(
export function flatten<TSchema extends BaseSchema | BaseSchemaAsync>(
error: ValiError
): FlatErrors<TSchema>;

Expand All @@ -141,39 +143,61 @@ export function flatten<TSchema extends BaseSchema | BaseSchemaAsync = any>(
*
* @returns Flat errors.
*/
export function flatten<TSchema extends BaseSchema | BaseSchemaAsync = any>(
export function flatten<TSchema extends BaseSchema | BaseSchemaAsync>(
issues: Issues
): FlatErrors<TSchema>;

export function flatten<TSchema extends BaseSchema | BaseSchemaAsync = any>(
export function flatten<TSchema extends BaseSchema | BaseSchemaAsync>(
arg1: ValiError | Issues
): FlatErrors<TSchema> {
return (Array.isArray(arg1) ? arg1 : arg1.issues).reduce<FlatErrors<TSchema>>(
(flatErrors, issue) => {
if (issue.path) {
if (
issue.path.every(
({ key }) => typeof key === 'string' || typeof key === 'number'
)
) {
const path = issue.path.map(({ key }) => key).join('.') as NestedPath<
Input<TSchema>
>;
if (flatErrors.nested[path]) {
flatErrors.nested[path]!.push(issue.message);
} else {
flatErrors.nested[path] = [issue.message];
}
}
} else {
if (flatErrors.root) {
flatErrors.root.push(issue.message);
} else {
flatErrors.root = [issue.message];
}
}
return flatErrors;
},
{ nested: {} }
const issues = Array.isArray(arg1) ? arg1 : arg1.issues;
return issues.reduce<FlatErrors<TSchema>>(processIssue, { nested: {} });
}

function processIssue<TSchema extends BaseSchema | BaseSchemaAsync>(
flatErrors: FlatErrors<TSchema>,
issue: Issue
): FlatErrors<TSchema> {
if (issue.path && isPathValid(issue.path)) {
const path = formatPath(issue.path);
updateNestedErrors(flatErrors, path, issue.message);
} else {
updateRootErrors(flatErrors, issue.message);
}
return flatErrors;
}

function isPathValid(path: PathItem[]): boolean {
return path.every(
({ key }) => typeof key === 'string' || typeof key === 'number'
);
}

function formatPath<TSchema extends BaseSchema | BaseSchemaAsync>(
path: PathItem[]
): NestedPath<TSchema> {
return path.map(({ key }) => key).join('.') as NestedPath<Input<TSchema>>;
}

function updateNestedErrors<TSchema extends BaseSchema | BaseSchemaAsync>(
flatErrors: FlatErrors<TSchema>,
path: keyof Record<NestedPath<TSchema>, [string, ...string[]]>,
message: string
): void {
if (flatErrors.nested[path]) {
flatErrors.nested[path]!.push(message);
} else {
flatErrors.nested[path] = [message];
}
}

function updateRootErrors<TSchema extends BaseSchema | BaseSchemaAsync>(
flatErrors: FlatErrors<TSchema>,
message: string
): void {
if (flatErrors.root) {
flatErrors.root.push(message);
} else {
flatErrors.root = [message];
}
}