grantila / typeconv

Convert between JSON Schema, TypeScript, GraphQL, Open API and SureType
MIT License
421 stars 8 forks source link

'Required' not respected for JSON schema references #14

Closed hborchardt closed 1 year ago

hborchardt commented 2 years ago

Hi, I am using typeconv to convert a JSON schema to suretype. I noticed that for required properties whose type is a referenced type, the 'required()' function call does not appear in the generated suretype declaration.

Repro: test.ts:

export interface A {}

export interface B {
  a: A;
}

Run:

npx typeconv@1.7.0 -f ts -t jsc -o . test.ts
npx typeconv@1.7.0 -f jsc -t st -o ./out test.json

output:

import { suretype, v, compile } from 'suretype';

const schemaA = suretype({
    name: "A",
    title: "A"
}, v.object({}));

export interface A {
}

// ...
const schemaB = suretype({
    name: "B",
    title: "B"
}, v.object({
    a: schemaA
}));

export interface B {
    a: A;
}

should be

v.object({
    a: schemaA.required()
})

The inlined type is created correctly, which also means that, for example, the ensureB function is of type never, so cannot be called.

I think that in this line, it should be wrap instead of wrapAnnotations: https://github.com/grantila/core-types-suretype/blob/1ae1e9fa802054e7de7a5c89226aa074cc642b6d/lib/json-schema-to-suretype.ts#L666

Thanks for building this :)

grantila commented 1 year ago

Thanks for the great description and accurate fix!

github-actions[bot] commented 1 year ago

:tada: This issue has been resolved in version 2.1.0 :tada:

The release is available on:

Your semantic-release bot :package::rocket: