Skip to content

Commit

Permalink
fix(validation-errors): type inference for branded, nullable, optiona…
Browse files Browse the repository at this point in the history
…l and array types (#284)
  • Loading branch information
aleclofabbro authored Oct 22, 2024
1 parent 52d3d29 commit 2e1fe62
Showing 1 changed file with 7 additions and 4 deletions.
11 changes: 7 additions & 4 deletions packages/next-safe-action/src/validation-errors.types.ts
Original file line number Diff line number Diff line change
@@ -1,21 +1,24 @@
import type { Infer, InferIn, Schema } from "./adapters/types";
import type { Prettify } from "./utils.types";

// Basic types and arrays.
type NotObject = number | string | boolean | bigint | symbol | null | undefined | any[];

// Object with an optional list of validation errors.
type VEList = Prettify<{ _errors?: string[] }>;

// Creates nested schema validation errors type using recursion.
type SchemaErrors<S> = {
[K in keyof S]?: S[K] extends object | null | undefined ? Prettify<VEList & SchemaErrors<S[K]>> : VEList;
[K in keyof S]?: S[K] extends NotObject ? VEList : Prettify<VEList & SchemaErrors<S[K]>>;
} & {};

/**
* Type of the returned object when validation fails.
*/
export type ValidationErrors<S extends Schema | undefined> = S extends Schema
? Infer<S> extends object
? Prettify<VEList & SchemaErrors<Infer<S>>>
: VEList
? Infer<S> extends NotObject
? VEList
: Prettify<VEList & SchemaErrors<Infer<S>>>
: undefined;

/**
Expand Down

0 comments on commit 2e1fe62

Please sign in to comment.