Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: TheEdoRan/next-safe-action
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: v7.9.5
Choose a base ref
...
head repository: TheEdoRan/next-safe-action
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: v7.9.6
Choose a head ref
  • 2 commits
  • 2 files changed
  • 2 contributors

Commits on Oct 21, 2024

  1. chore(website): fix typo in standalone middleware section

    TheEdoRan committed Oct 21, 2024

    Verified

    This commit was created on GitHub.com and signed with GitHub’s verified signature. The key has expired.
    Copy the full SHA
    52d3d29 View commit details

Commits on Oct 22, 2024

  1. fix(validation-errors): type inference for branded, nullable, optiona…

    …l and array types (#284)
    aleclofabbro authored Oct 22, 2024
    Copy the full SHA
    2e1fe62 View commit details
Showing with 8 additions and 5 deletions.
  1. +7 −4 packages/next-safe-action/src/validation-errors.types.ts
  2. +1 −1 website/docs/define-actions/middleware.md
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;

/**
2 changes: 1 addition & 1 deletion website/docs/define-actions/middleware.md
Original file line number Diff line number Diff line change
@@ -265,4 +265,4 @@ export const actionClientWithMyMiddleware = actionClient.use(myMiddleware1).use(

An action defined using the `actionClientWithMyMiddleware` will contain `foo`, `baz` and `john` in its context.

\* Note that you can pass, **but not required to**, an object with two generic properties to the `createMiddleware()` function: `ctx` \[1\], `metadata` \[2\] and `serverError` \[3\]. Those keys are optional, and you should only provide them if you want your middleware to require **at minimum** the shape you passed in as generic. By doing that, following the above example, you can then access `ctx.foo` and `metadata.actionName` in the middleware you're defining, and by awaiting the `next` function you'll see that `serverError` is an object with the `message` property. If you pass a middleware that requires those properties to a client that doesn't have them, you'll get an error in `use()` method.
\* Note that you can pass, **but not required to**, an object with three generic properties to the `createMiddleware()` function: `ctx` \[1\], `metadata` \[2\] and `serverError` \[3\]. Those keys are optional, and you should only provide them if you want your middleware to require **at minimum** the shape you passed in as generic. By doing that, following the above example, you can then access `ctx.foo` and `metadata.actionName` in the middleware you're defining, and by awaiting the `next` function you'll see that `serverError` is an object with the `message` property. If you pass a middleware that requires those properties to a client that doesn't have them, you'll get an error in `use()` method.