json-schema-to-ts
json-schema-to-ts copied to clipboard
Is there support for relative references?
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
.
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!
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!