为什么我的 Yup 验证条件出现错误?

问题描述 投票:0回答:1

我正在开发一个 TypeScript 项目,我使用 Yup 进行表单验证。我遇到了

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;
};
 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, "">>

如何解决这个错误?

javascript reactjs next.js formik yup
1个回答
0
投票

这对我有用

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(), 
© www.soinside.com 2019 - 2024. All rights reserved.