jquense / yup

Dead simple Object schema validation
MIT License
22.94k stars 935 forks source link

When I use when condition repeatedly, I get an Overload error. #2159

Open zbkmr opened 10 months ago

zbkmr commented 10 months ago

@jquense I wasn't sure if the issue was related to this or not.

https://github.com/jquense/yup/pull/1924

I have problems every time I add the second when condition.

const validations: Yup.ObjectSchema<DetailedFormProps> = Yup.object()
  .shape(
    {
      startDate: Yup.date()
        .nullable()
        .when('endDate', {
          is: (endDate: Date) => endDate,
          then: () =>
            Yup.date().required(
              'accounts_accountActivities_startDateEndDateSelection_errorMessage'
            ),
          otherwise: () => Yup.date(),
        })
        .defined()
        .when('endDate', (endDate: Date, schema) =>
          schema
            .test(
              'is-three-months-between-dates',
              'accounts_accountActivities_startDateEndDateRange_errorMessage',
              (startDate?: Date | null) =>
                startDate && endDate ? IsDateDifferenceMoreThanMonths(startDate, endDate, 12) : true
            )
            .max(endDate || new Date(), 'accounts_accountActivities_startDateEndDate_errorMessage')
        ),

MY error message

error TS2769: No overload matches this call.
  Overload 1 of 4, '(keys: string | string[], builder: ConditionBuilder<DateSchema<Date | null, AnyObject, undefined, "">>): DateSchema<Date | null, AnyObject, undefined, "">', gave the following error.
    Argument of type '(endDate: Date, schema: DateSchema<Date | null, AnyObject, undefined, "">) => DateSchema<Date | null, AnyObject, undefined, "">' is not assignable to parameter of type 'ConditionBuilder<DateSchema<Date | null, AnyObject, undefined, "">>'.
      Types of parameters 'endDate' and 'values' are incompatible.
        Type 'any[]' is missing the following properties from type 'Date': toDateString, toTimeString, toLocaleDateString, toLocaleTimeString, and 38 more.
  Overload 2 of 4, '(keys: string | string[], options: ConditionConfig<DateSchema<Date | null, AnyObject, undefined, "">>): DateSchema<Date | null, AnyObject, undefined, "">', gave the following error.
    Argument of type '(endDate: Date, schema: DateSchema<Date | null, AnyObject, undefined, "">) => DateSchema<Date | null, AnyObject, undefined, "">' is not assignable to parameter of type 'ConditionConfig<DateSchema<Date | null, AnyObject, undefined, "">>'.
      Property 'is' is missing in type '(endDate: Date, schema: DateSchema<Date | null, AnyObject, undefined, "">) => DateSchema<Date | null, AnyObject, undefined, "">' but required in type 'ConditionConfig<DateSchema<Date | null, AnyObject, undefined, "">>'.
export interface DetailedFormProps {
  startDate: Date | null;
  // endDate: Date | null;
  /*  minimumAmount: string;
  maximumAmount: string;
  explanation: string;
  sort: DictionaryLine | null;
  transactionType: DictionaryLine | null;
  channelCode: DictionaryLine | null;
  operationType: DictionaryLine | null; */
}

Thanks in advance for the answer.

yamanidev commented 9 months ago

I am having a similar issue. Any updates?

ricohumme commented 8 months ago

same here. Seems that the ConditionBuilder is unaware of the ConditionConfig

remstone7 commented 8 months ago

bump, ran into same issue as well