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

Is there support for relative references? #129

Open
gz-95 opened this issue Mar 14, 2023 · 2 comments
Open

Is there support for relative references? #129

gz-95 opened this issue Mar 14, 2023 · 2 comments

Comments

@gz-95
Copy link

gz-95 commented Mar 14, 2023

Thanks for this really useful library! I was just wondering if this library supports relative references? The following is an example of what I'm trying to achieve:

import { FromSchema } from "json-schema-to-ts";

const types = {
  $id: "https://example.local/types/",
  $schema: "http://json-schema.org/draft-07/schema#",
  definitions: {
    foo: {
      $ref: "#/definitions/bar",
    },
    bar: {
      type: "integer",
    },
  },
} as const;

const root = {
  $ref: "https://example.local/types/#/definitions/foo",
} as const;

type Root = FromSchema<
  typeof root,
  {
    references: [typeof types];
  }
>;

Version 2.7.2 currently resolves Root to never, whereas I was expecting it to resolve to number.

@gz-95
Copy link
Author

gz-95 commented May 22, 2023

I've just tried the example code from the README on version v2.8.2 as follow:

const userSchema = {
  $id: "http://example.com/schemas/user.json",
  type: "object",
  properties: {
    name: { type: "string" },
    age: { type: "integer" },
  },
  required: ["name", "age"],
  additionalProperties: false,
} as const;

const usersSchema = {
  type: "array",
  items: {
    $ref: "http://example.com/schemas/user.json",
  },
} as const;

type Users = FromSchema<typeof usersSchema, { references: [typeof userSchema] }>;
// => {
//  name: string;
//  age: string;
// }[]

const anotherUsersSchema = {
  $id: "http://example.com/schemas/users.json",
  type: "array",
  items: { $ref: "user.json" },
} as const;

const root = {
  $ref: "http://example.com/schemas/users.json",
} as const;

type Root = FromSchema<
  typeof root,
  {
    references: [typeof userSchema, typeof anotherUsersSchema];
  }
>;

It seems Root resolves to [] whereas I was expecting it to be { name: string; age: number; }[]. I'm not really sure how references is supposed to work at this point. Any help would be greatly appreciated!

@ThomasAribart
Copy link
Owner

ThomasAribart commented May 23, 2023

Hello @gz-95 !

Good catch 👍 It seems like relative references work but only one-level deep (the README example works but not yours), probably because the $id is built by prefixing by the $ref prefix (here http://example.com/schemas/), which is not passed to the second reference.

I'll look into that ASAP and let you know!

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

No branches or pull requests

2 participants