zerot
    Preparing search index...

    Function validates

    • Creates a validation condition that uses a Zod schema to validate and optionally transform input. This condition is marked as a validator for the @contract decorator, meaning it can modify the input.

      Type Parameters

      • TSchema extends ZodType<any, ZodTypeDef, any>

        The Zod schema type.

      • TInput = unknown

        The expected type of the input before validation.

      • TOutput = TypeOf<TSchema>

        The inferred type from the Zod schema, or the transformed type.

      Parameters

      • schema: TSchema

        The Zod schema to validate the input against.

      • Optionaltransformer: (input: TypeOf<TSchema>) => TOutput

        An optional function to transform the validated input.

      Returns ContractValidator<TInput, TOutput>

      A validator function that takes input and returns the validated/transformed input.

      If the input validation fails.

      const UserSchema = z.object({ name: z.string(), email: z.string().email() });

      class MyService {
      @contract({
      requires: [validates(UserSchema)],
      })
      async processUser(user: z.infer<typeof UserSchema>) {
      // user is guaranteed to conform to UserSchema here
      console.log("Processing user:", user);
      }
      }