dhaval thakor
dhaval thakor

Reputation: 13

array of objects in yup validation not working

    export const licenseKeySchema = yup
      .object<Partial<apiTypes.LicenseKey>>()
      .shape({
        username: yup.string().required(),
        lastUpdatedOn: yup.number().required(),
        licenseKey: yup.string().required(),
        expiry: yup.number().required()
      });
    
    export const endpointSchema = yup.object<Partial<apiTypes.Endpoint>>({
      hostname: yup.string().required(),
      ipAddress: yup.string().required(),
      lcmIdentifier: yup.string().required(),
      licenseKeys: yup.array().of(licenseKeySchema).required()
    });
    
    
    export interface LicenseKey {
      vmid: string;
      createdBy: string;
      lastModifiedBy: string;
      createdOn: number;
      lastUpdatedOn: number;
      properties?: any;
      username: string;
      hostname: string;
      lcmIdentifier: string;
      licenseKey: string;
      orgId: string;
      accessKey: string;
      lemansGatewayUri: string;
      message?: string;
      accessKeyLink: string;
      expiry: number;
      lastHeartBeat?: any;
      ipAddress: string;
    }
    
    export interface Endpoint {
      hostname: string;
      licenseKeys: LicenseKey[];
      ipAddress: string;
      lcmIdentifie

 }

getting below error Type 'ArraySchema<Shape<Partial | undefined, { username: string; lastUpdatedOn: number; licenseKey: string; expiry: number; }>>' is not assignable to type 'MixedSchema<LicenseKey[] | undefined>'. The types of 'nullable(...).nullable' are incompatible between these types. Type '{ (isNullable?: true | undefined): NullableArraySchema<Shape<Partial | undefined, { username: string; lastUpdatedOn: number; licenseKey: string; expiry: number; }>>; (isNullable: false): ArraySchema<...>; (isNullable?: boolean | undefined): ArraySchema<...>; }' is not assignable to type '{ (isNullable?: true | undefined): MixedSchema<LicenseKey[] | null | undefined>; (isNullable: false): MixedSchema<LicenseKey[] | undefined>; (isNullable?: boolean | undefined): MixedSchema<...>; }'. Types of parameters 'isNullable' and 'isNullable' are incompatible. Type 'false' is not assignable to type 'true | undefined'.

Upvotes: 1

Views: 3619

Answers (1)

Adam Jenkins
Adam Jenkins

Reputation: 55772

You declared your licenseKeySchema to be a Partial of LicenseKey:

    export const licenseKeySchema = yup
      .object<Partial<apiTypes.LicenseKey>>() <- this line
      .shape({ ... })

so you must do the same in your Endpoint:

    export interface Endpoint {
      hostname: string;
      licenseKeys: Partial<LicenseKey>[]; <- this line
      ipAddress: string;
      ...
    }

EDIT:

Here is the stackblitz: https://stackblitz.com/edit/typescript-yxmshr?file=yup.ts

It has an error in it. As soon as you uncomment the commented out line and comment out the one above it in the Endpoint interface, then it works.

Upvotes: 1

Related Questions