-
-
Notifications
You must be signed in to change notification settings - Fork 36
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: set up built-in multi validation system
- Loading branch information
Showing
16 changed files
with
69 additions
and
32 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,28 +1,7 @@ | ||
import type { z } from "zod"; | ||
|
||
// Takes an object type and makes it more readable. | ||
export type Prettify<T> = { | ||
[K in keyof T]: T[K]; | ||
} & {}; | ||
|
||
// Returns type or promise of type. | ||
export type MaybePromise<T> = Promise<T> | T; | ||
|
||
// Schema type. | ||
export type Schema = z.ZodType; | ||
|
||
// Infers output schema type. | ||
export type Infer<S extends Schema> = z.infer<S>; | ||
|
||
// Infers input schema type. | ||
export type InferIn<S extends Schema> = z.input<S>; | ||
|
||
// Infers output schema type in array of schemas. | ||
export type InferArray<BAS extends readonly Schema[]> = { | ||
[K in keyof BAS]: Infer<BAS[K]>; | ||
}; | ||
|
||
// Infers input schema type in array of schemas. | ||
export type InferInArray<BAS extends readonly Schema[]> = { | ||
[K in keyof BAS]: InferIn<BAS[K]>; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
export { zodAdapter } from "./libs/zod"; | ||
|
||
export * from "./types"; |
24 changes: 24 additions & 0 deletions
24
packages/next-safe-action/src/validation-adapters/libs/zod.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
import type { z } from "zod"; | ||
import type { Infer, ValidationAdapter } from "../types"; | ||
|
||
class ZodAdapter<S extends z.ZodType> implements ValidationAdapter<S> { | ||
async validate(schema: S, data: unknown) { | ||
const result = await schema.safeParseAsync(data); | ||
|
||
if (result.success) { | ||
return { | ||
success: true, | ||
data: result.data as Infer<S>, | ||
} as const; | ||
} | ||
|
||
return { | ||
success: false, | ||
issues: result.error.issues.map(({ message, path }) => ({ message, path })), | ||
} as const; | ||
} | ||
} | ||
|
||
export function zodAdapter() { | ||
return new ZodAdapter<z.ZodType>(); | ||
} |
23 changes: 23 additions & 0 deletions
23
packages/next-safe-action/src/validation-adapters/types.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import type { z } from "zod"; | ||
|
||
export type Schema = z.ZodType; | ||
export type Infer<S extends Schema> = S extends z.ZodType ? z.infer<S> : never; | ||
export type InferIn<S extends Schema> = S extends z.ZodType ? z.input<S> : never; | ||
export type InferArray<BAS extends readonly Schema[]> = { | ||
[K in keyof BAS]: Infer<BAS[K]>; | ||
}; | ||
export type InferInArray<BAS extends readonly Schema[]> = { | ||
[K in keyof BAS]: InferIn<BAS[K]>; | ||
}; | ||
|
||
export type ValidationIssue = { | ||
message: string; | ||
path?: Array<string | number | symbol>; | ||
}; | ||
|
||
export interface ValidationAdapter<S extends Schema> { | ||
validate( | ||
schema: S, | ||
data: unknown | ||
): Promise<{ success: true; data: Infer<S> } | { success: false; issues: ValidationIssue[] }>; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters