ajv-validator / ajv-validator/ajv

How to make oneOf field nullable with typescript

Aperta
#1,727 1 commento 0 reazioni 0 assegnatari Vedi su GitHub
Lingua principale
TypeScript
Stelle
14.8k
Fork
1k
Metriche di merge delle PR
Nessuna PR unita negli ultimi 30g

Descrizione

I have field called `platform` which can either be a `string` or a `string[]` this field can also be nullable/undefined (not passed).

**typescript interface**
```
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 schema**

```
export const EntityLeaderboardQuerySchema: JSONSchemaType = {
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,
};
```

As you can see ive attempted to add nullable field to both objects within the oneOf array. However there is still an issue with the types

> [ERROR] 11:07:49 ⨯ Unable to compile TypeScript:
> src/api/leaderboard/entity/entity-leaderboard.interface.ts:43:14 - error TS2322: Type '{ type: "object"; properties: { rank: { type: "string"; }; entity_types: { oneOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }; country: { type: "string"; nullable: true; }; region: { ...; }; start_date: { ...; }; end_date: { ...; }; top: { ...; }; platform: { ...; }; }; required: ("ra...' is not assignable to type 'UncheckedJSONSchemaType'.
> Type '{ type: "object"; properties: { rank: { type: "string"; }; entity_types: { oneOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }; country: { type: "string"; nullable: true; }; region: { ...; }; start_date: { ...; }; end_date: { ...; }; top: { ...; }; platform: { ...; }; }; required: ("ra...' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.
> Type '{ type: "object"; properties: { rank: { type: "string"; }; entity_types: { oneOf: ({ type: "string"; } | { type: "array"; items: { type: "string"; }; })[]; }; country: { type: "string"; nullable: true; }; region: { ...; }; start_date: { ...; }; end_date: { ...; }; top: { ...; }; platform: { ...; }; }; required: ("ra...' is not assignable to type '{ type: "object"; additionalProperties?: boolean | UncheckedJSONSchemaType | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.
> The types of 'properties.platform' are incompatible between these types.
> Type '{ oneOf: ({ type: "string"; nullable: boolean; } | { type: "array"; items: { type: "string"; nullable: boolean; }; nullable: boolean; })[]; }' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType & { nullable: true; const?: undefined; enum?: readonly (string | string[] | null | undefined)[] | undefined; default?: string | ... 2 more ... | undefined; })'.
> Type '{ oneOf: ({ type: "string"; nullable: boolean; } | { type: "array"; items: { type: "string"; nullable: boolean; }; nullable: boolean; })[]; }' is not assignable to type '{ type: "array"; items: UncheckedJSONSchemaType; contains?: UncheckedPartialSchema | undefined; minItems?: number | undefined; ... 4 more ...; additionalItems?: undefined; } & { ...; } & { ...; } & { ...; }'.
> Type '{ oneOf: ({ type: "string"; nullable: boolean; } | { type: "array"; items: { type: "string"; nullable: boolean; }; nullable: boolean; })[]; }' is missing the following properties from type '{ type: "array"; items: UncheckedJSONSchemaType; contains?: UncheckedPartialSchema | undefined; minItems?: number | undefined; ... 4 more ...; additionalItems?: undefined; }': type, items
>
> 43 export const EntityLeaderboardQuerySchema: JSONSchemaType = {

```
ajv: 8.6.2
typescipt: 4.3.5
```

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

Guida per i contributori

Apri la guida per i contributori

Valutazione

Questa issue non è ancora stata valutata.

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.