Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Q] How to extend schema with another schema in variable with typescript? #2326

Open
budarin opened this issue Sep 11, 2023 · 1 comment
Open

Comments

@budarin
Copy link

budarin commented Sep 11, 2023

try to extend status schema with the newStatus

import { JSONSchemaType } from "ajv";

type Id = number;
type StatusName = string;
type StatusColor = string;

type NewStatus = {
  status: StatusName;
  color: StatusColor;
};

type Status = {
  status_id: Id;
} & NewStatus;


const newStatus: JSONSchemaType<NewStatus> = {
  type: "object",

  properties: {
    status: {
      type: "string",
      minLength: 3,
      maxLength: 20,
    },

    color: {
      type: "string",
      pattern: "#[a-f0-9]{6}",
    },
  },

  required: ["status", "color"],
  additionalProperties: false,
};

const status: JSONSchemaType<Status> = {
  type: "object",

  properties: {
    status_id: {
      type: "integer",
    },

    ...newStatus.properties,
  },

  required: ["status_id", "status", "color"],
  additionalProperties: false,
};

but get the error for status:

[{
	"resource": "index.ts",
	"owner": "typescript",
	"code": "2322",
	"severity": 8,
	"message": "Type '{ type: \"object\"; properties: { status?: { $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; }) | undefined; color?: { $ref: string; } | ... 1 more ... | undefined; status_id: { ....' is not assignable to type 'JSONSchemaType<Status>'.\n  Type '{ type: \"object\"; properties: { status?: { $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; }) | undefined; color?: { $ref: string; } | ... 1 more ... | undefined; status_id: { ....' is not assignable to type '({ anyOf: readonly UncheckedJSONSchemaType<Status, false>[]; } & { [keyword: string]: any; $id?: string | undefined; $ref?: string | undefined; $defs?: Record<string, UncheckedJSONSchemaType<...>> | undefined; definitions?: Record<...> | undefined; }) | ({ ...; } & { ...; }) | ({ ...; } & ... 2 more ... & { ...; })'.\n    Type '{ type: \"object\"; properties: { status?: { $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; }) | undefined; color?: { $ref: string; } | ... 1 more ... | undefined; status_id: { ....' is not assignable to type '{ type: \"object\"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; } & { ...; } & { ...; } & { ...; }'.\n      Type '{ type: \"object\"; properties: { status?: { $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; }) | undefined; color?: { $ref: string; } | ... 1 more ... | undefined; status_id: { ....' is not assignable to type '{ type: \"object\"; additionalProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; unevaluatedProperties?: boolean | UncheckedJSONSchemaType<unknown, false> | undefined; ... 7 more ...; maxProperties?: number | undefined; }'.\n        The types of 'properties.status' are incompatible between these types.\n          Type '{ $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; }) | undefined' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; })'.\n            Type 'undefined' is not assignable to type '{ $ref: string; } | (UncheckedJSONSchemaType<string, false> & { nullable?: false | undefined; const?: string | undefined; enum?: readonly string[] | undefined; default?: string | undefined; })'.",
	"source": "ts",
	...
}]

image

image

How is it correct from the typescript point of view to extend the status schema with the newStatus schema?

@budarin budarin changed the title [Q] How to extend schema with another schema in variable with typescript& [Q] How to extend schema with another schema in variable with typescript? Sep 11, 2023
@budarin
Copy link
Author

budarin commented Sep 13, 2023

I have found a solution that fixes the error

import { PropertiesSchema } from "ajv/dist/types/json-schema";

...

const status: JSONSchemaType<Status> = {
  type: "object",

  properties: {
    status_id: {
      type: "integer",
    },

    ...(newStatus.properties as PropertiesSchema<NewStatus>),
  },

  required: ["status_id", "status", "color"],
  additionalProperties: false,
};

but I am not sure that it is correct

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Development

No branches or pull requests

2 participants
@budarin and others