reactjs 为什么我在Yup验证的条件中得到一个错误?

5us2dqdw  于 2023-11-18  发布在  React
关注(0)|答案(1)|浏览(128)

我正在使用Yup进行表单验证的TypeScript项目中工作。我遇到了when函数和is条件的问题。TypeScript引发了与类型相关的错误(TS2769),我在解决该错误时遇到了问题。

export type TClubFormData = {
  title: string;
  description: string;
  info: string;
  city: string;
  address: string;
  phone: string;
  picture: File | null;
  pictureUrl: string;
  latitudeMap: string;
  longitudeMap: string;
  openingTime: string;
  closingTime: string;
};

x

const initialValues: TClubFormData = {
    title: (club && club.title) || '',
    description: (club && club.description) || '',
    info: (club && club.info) || '',
    address: (club && club.address) || '',
    city: (club && club.city) || '',
    phone: (club && club.phone) || '',
    picture: null,
    pictureUrl: (club && club.picture) || '',
    latitudeMap: (club && club.latitudeMap) || '',
    longitudeMap: (club && club.longitudeMap) || '',
    openingTime: (club && club.openingTime) || '00:00',
    closingTime: (club && club.closingTime) || '00:00',
  };
export const CreateAndEditClubFormSchema = Yup.object().shape({
  title: Yup.string().required('Անվանումը պարտադիր է'),
  description: Yup.string().required('Նկարագիրը պարտադիր է'),
  info: Yup.string(),
  city: Yup.string().required('Քաղաքը պարտադիր է'),
  address: Yup.string().required('Հասցեն պարտադիր է'),
  phone: Yup.string(),
  // picture: Yup.mixed().nullable().required('Նկարը պարտադիր է'),
  picture: Yup.mixed().when('pictureUrl', {
    is: (pictureUrl: string) => !pictureUrl, // ERROR HERE
    then: Yup.mixed().nullable().required('Նկարը պարտադիր է'),
    otherwise: Yup.mixed().nullable(),
  }),
  latitudeMap: Yup.string().matches(
    /^(\+|-)?((\d((\.)|\.\d{1,6})?)|(0*?[0-8]\d((\.)|\.\d{1,6})?)|(0*?90((\.)|\.0{1,6})?))$/,
    'Սխալ կորդինատային լայնություն'
  ),
  longitudeMap: Yup.string().matches(
    /^(\+|-)?((\d((\.)|\.\d{1,6})?)|(0*?\d\d((\.)|\.\d{1,6})?)|(0*?1[0-7]\d((\.)|\.\d{1,6})?)|(0*?180((\.)|\.0{1,6})?))$/,
    'Սխալ կորդինատային երկայնություն'
  ),
  openingTime: Yup.string().required('Opening time is required'),
  closingTime: Yup.string().required('Closing time is required'),
});
TS2769: No overload matches this call.
Overload  1  of  4 ,
(keys: string | string[], builder: ConditionBuilder<MixedSchema<AnyPresentValue | undefined, AnyObject, undefined, "">>): MixedSchema<...>
, gave the following error.
Argument of type
{     is: (pictureUrl: string) => boolean;     then: Yup.MixedSchema<AnyPresentValue, Yup.AnyObject, undefined, "">;     otherwise: Yup.MixedSchema<AnyPresentValue | null | undefined, Yup.AnyObject, undefined, "">; }
is not assignable to parameter of type
ConditionBuilder<MixedSchema<AnyPresentValue | undefined, AnyObject, undefined, "">>
Object literal may only specify known properties, and  is  does not exist in type
ConditionBuilder<MixedSchema<AnyPresentValue | undefined, AnyObject, undefined, "">>

的数据
如何解决此错误?

xvw2m8pv

xvw2m8pv1#

这对我很有用

picture: Yup.mixed() .test('is-picture-url-empty', 'Նկարը պարտադիր է', function () { const pictureUrl = this.resolve(Yup.ref('pictureUrl')); return pictureUrl ? true : this.createError({ path: this.path, message: 'Նկարը պարտադիր է' }); }) .nullable(),

字符串

相关问题