optional input number with Zod schema

1.3k views Asked by At

Hi everyone I'm using for the first time Zod with React useForm and I'm having some trouble setting up a optional input.

Zod Schema

 discount: z
    .string()
    .optional()
    .transform((discount) =>
      discount === "" || discount === undefined ? undefined : parseInt(discount)
    )
    .pipe(z.number().nonnegative().max(100)),

Input

<FormInput<FormData, InputProps>
          id="DiscountFormCreateProduct"
          register={register}
          rules={{ required: false, valueAsNumber: true }}
          errors={errors}
          className="col-span-3"
          name="discount"
          labelText="Discount"
          label={"Discount number "}
          Formtype={FormElementType.INPUT}
          placeholder="Discount(Eg 5%)"
          isRequired={false}
          type="number"
          min={0}
          max={100}
        />

As you can see my input as rules not required but I still have the error messgae: Expected string, received nan

thanks for your help

0

There are 0 answers