zerot
    Preparing search index...

    Function returns

    • Creates a post-condition that validates the output of a method against a Zod schema.

      Type Parameters

      • TSchema extends ZodType<any, ZodTypeDef, any>

        The Zod schema type.

      • TOutput = TypeOf<TSchema>

        The expected type of the output to be validated.

      • TInput = any

        The expected type of the original input to the method.

      • TContext = any

        The expected type of the authentication context.

      Parameters

      • schema: TSchema

        The Zod schema to validate the output against.

      Returns ContractEnsuresCondition<TOutput, TInput, TContext>

      A condition function that takes output, input, and context, and returns a boolean.

      If the output validation fails.

      const UserOutputSchema = z.object({ id: z.string(), name: z.string() });

      class MyService {
      @contract({
      ensures: [returns(UserOutputSchema)],
      })
      async getUser(id: string): Promise<z.infer<typeof UserOutputSchema>> {
      // Assume this fetches a user
      return { id: id, name: "John Doe" };
      }
      }