yup 验证中的对象数组不起作用
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'.
您声明您的 licenseKeySchema
是 LicenseKey
的 Partial
:
export const licenseKeySchema = yup
.object<Partial<apiTypes.LicenseKey>>() <- this line
.shape({ ... })
所以你必须在 Endpoint
:
中做同样的事情
export interface Endpoint {
hostname: string;
licenseKeys: Partial<LicenseKey>[]; <- this line
ipAddress: string;
...
}
编辑:
这里是 stackblitz:https://stackblitz.com/edit/typescript-yxmshr?file=yup.ts
其中有错误。只要在 Endpoint
界面中取消注释行的注释并注释掉它上面的行,它就可以工作了。
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'.
您声明您的 licenseKeySchema
是 LicenseKey
的 Partial
:
export const licenseKeySchema = yup
.object<Partial<apiTypes.LicenseKey>>() <- this line
.shape({ ... })
所以你必须在 Endpoint
:
export interface Endpoint {
hostname: string;
licenseKeys: Partial<LicenseKey>[]; <- this line
ipAddress: string;
...
}
编辑:
这里是 stackblitz:https://stackblitz.com/edit/typescript-yxmshr?file=yup.ts
其中有错误。只要在 Endpoint
界面中取消注释行的注释并注释掉它上面的行,它就可以工作了。