首页 > 解决方案 > 如何使用 typescript 和 ajv 使 oneOf 字段可以为空

问题描述

我有一个名为的字段platform,它可以是一个string或一个string[]这个字段也可以是可空的/未定义的(未通过)。

打字稿界面

export interface IEntityLeaderboardQuery {
    rank: string;
    entity_types: string[] | string;
    country?: string | undefined;
    region?: string | undefined;
    start_date: string;
    end_date: string;
    top?: string | undefined;
    platform?: string[] | string | undefined;
}

json模式

export const EntityLeaderboardQuerySchema: JSONSchemaType<IEntityLeaderboardQuery> = {
    type: "object",
    properties: {
        rank: { type: "string" },
        entity_types: {
            oneOf: [
                {
                    type: "string",
                },
                {
                    type: "array",
                    items: { type: "string" },
                },
            ],
        },
        country: { type: "string", nullable: true },
        region: { type: "string", nullable: true },
        start_date: { type: "string" },
        end_date: { type: "string" },
        top: { type: "string", nullable: true },
        platform: {
            oneOf: [
                {
                    type: "string",
                    nullable: true,
                },
                {
                    type: "array",
                    items: { type: "string" },
                    nullable: true,
                },
            ],
        },
    },
    required: ["rank", "entity_types", "start_date", "end_date"],
    additionalProperties: false,
};

如您所见,我尝试将可为空的字段添加到 oneOf 数组中的两个对象。但是类型仍然存在问题

[错误] 11:07:49 ⨯ 无法编译 TypeScript:src/api/leaderboard/entity/entity-leaderboard.interface.ts:43:14 - 错误 TS2322: Type '{ type: "object"; 属性:{等级:{类型:“字符串”;}; entity_types: { oneOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }; 国家:{类型:“字符串”;可为空的:真;}; 地区: { ...; }; 开始日期: { ...; }; 结束日期: { ...; }; 最佳: { ...; }; 平台: { ...; }; }; 必需的:(“ra ...”不可分配给类型“UncheckedJSONSchemaType<IEntityLeaderboardQuery,false>”。类型“{ type:“object”;属性:{ rank:{ type:“string”;};entity_types:{ oneOf : ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }; 国家:{类型:“字符串”;可为空的:真;}; 地区: { ...; }; 开始日期: { ...; }; 结束日期: { ...; }; 最佳: { ...; }; 平台: { ...; }; }; 必需的:(“ra ...”不可分配给类型'{ type:“object”;additionalProperties?:布尔| UncheckedJSONSchemaType<unknown,false> | undefined;unevaluatedProperties?:boolean | UncheckedJSONSchemaType<unknown,false> | undefined; ... 还有 7 个 ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'。类型 '{ type: "object"; properties : { rank: { type: "string"; }; entity_types: { oneOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[] ; }; 国家:{ 类型:“字符串”;可为空:true; }; 地区:{ ...; }; start_date: { ...; }; end_date: { ...; }; 最佳: { ...; }; 平台: { ...; }; }; 必需的:(“ra ...”不可分配给类型'{ type:“object”;additionalProperties?:布尔| UncheckedJSONSchemaType<unknown,false> | undefined;unevaluatedProperties?:boolean | UncheckedJSONSchemaType<unknown,false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'。'properties.platform' 的类型在这些类型之间不兼容。类型'{ oneOf: ({ type: "string"; nullable: boolean; } | { type: "array"; items: { type: "string"; nullable: boolean; }; nullable: boolean; })[]; }' 不能赋值给 type '{ $ref: string; } | ( UncheckedJSONSchemaType<string | string[] | undefined, false> & { nullable: true; const?: undefined; enum?: 只读 (string | string[] | null | undefined)[] | 不明确的; 默认?:字符串 | ... 2 更多 ... | 不明确的; })'。类型 '{ oneOf: ({ type: "string"; nullable: boolean; } | { type: "array"; items: { type: "string"; nullable: boolean; }; nullable: boolean; })[]; }' 不可分配给类型 '{ type: "array"; 项目:UncheckedJSONSchemaType<string, false>; 包含?:UncheckedPartialSchema | 不明确的; minItems?: 数字 | 不明确的; ... 4 更多 ...; 附加项目?:未定义;} & { ...; } & { ...; } & { ...; }'。类型 '{ oneOf: ({ type: "string"; nullable: boolean; } | { type: "array"; items: { type: "string"; nullable: boolean; }; nullable: boolean; })[]; }' 缺少 type '{ type: "array"; 中的以下属性 项目:UncheckedJSONSchemaType<string, false>; 包含?:UncheckedPartialSchema | 不明确的; minItems?: 数字 | 不明确的; ... 4 更多 ...; 附加项目?:未定义;}':类型,项目

43 导出常量 EntityLeaderboardQuerySchema: JSONSchemaType = {

ajv: 8.6.2
typescipt: 4.3.5

https://replit.com/join/ngjynszvjr-kaykhan

标签: node.jstypescriptajv

解决方案


我有类似的情况,这对我有用:

oneOf: [
    {
        type: "string",
    },
    {
        type: "array",
        items: { type: "string" },
    },
    {
        type: "object",
        nullable: true,
    },
],

使用{ type: "null" }无效。


推荐阅读