ThomasAribart / json-schema-to-ts

Infer TS types from JSON schemas 📝
MIT License
1.47k stars 31 forks source link

allOf operator and ajv-keywords support #84

Closed svsool closed 1 year ago

svsool commented 2 years ago

Hi, thanks for this library.

I noticed that FromSchema utility does not work with ajv-keywords such as transform: ['trim'] for example. Seems like a bug to me.

image

Code to reproduce:

export const schema = {
  type: 'object',
  properties: {
    prop1: {
      type: 'string',
      allOf: [
        {
          transform: ['trim'],
        },
      ],
    },
  },
  required: ['prop1'],
  additionalProperties: false,
} as const;

type Schema = FromSchema<typeof schema>;
ThomasAribart commented 2 years ago

@svsool Thanks for the heads up. JSON-schemas are extendable, so yes, FromSchema should accept custom properties.

Modifying this as we speak.

ThomasAribart commented 2 years ago

@svsool Should be solved in 2.5.5, can you confirm this ?

Solved by https://github.com/ThomasAribart/json-schema-to-ts/pull/85

svsool commented 2 years ago

it works, thank you!

svsool commented 1 year ago

hey @ThomasAribart, unfortunately seems to be broken with json-schema-to-ts 3.7.2, TypeScript 5, and JSONSchema7:

image

this is package.json:

{
  "name": "schema-repro",
  "version": "1.0.0",
  "main": "index.js",
  "license": "MIT",
  "type": "module",
  "scripts": {
    "ts": "tsc --project ./tsconfig.json --noEmit"
  },
  "devDependencies": {
    "@tsconfig/esm": "^1.0.2",
    "@tsconfig/node18": "^1.0.1",
    "@tsconfig/strictest": "^2.0.0",
    "json-schema-to-ts": "2.5.2",
    "typescript": "^5.0.3"
  }
}

and types.ts:

import type { FromSchema } from 'json-schema-to-ts';

export const countryCode = {
    type: 'string',
    allOf: [
        {
            transform: ['trim'],
        },
        {
            minLength: 2,
            maxLength: 3,
        },
    ],
} as const;

export type CountryCode = FromSchema<typeof countryCode>;

tsconfig.json:

{
  "extends": ["@tsconfig/strictest/tsconfig.json", "@tsconfig/node18/tsconfig.json", "@tsconfig/esm/tsconfig.json"],
  "compilerOptions": {
    "typeRoots": ["node_modules/@types"],
    "outDir": "build",
    "baseUrl": "src",
    "sourceMap": true,
    "downlevelIteration": true,
    "resolveJsonModule": true,
    "exactOptionalPropertyTypes": false,
    "noImplicitReturns": false,
    "noPropertyAccessFromIndexSignature": false,
    "noUncheckedIndexedAccess": false,
    "noUnusedParameters": false,
    "checkJs": false,
  },
  "exclude": ["node_modules"]
}

run yarn ts to reproduce

ThomasAribart commented 1 year ago

Thanks @svsool ! Gonna have a look at it 👀

ThomasAribart commented 1 year ago

@svsool Should work in 2.8.0 with the new ExtendedJSONSchema and FromExtendedSchema types:

type Extension = {
  transform: string[];
};

export const countryCode = {
  type: "string",
  allOf: [
    {
      transform: ["trim"],
    },
    {
      minLength: 2,
      maxLength: 3,
    },
  ],
} as const;

export type CountryCode = FromExtendedSchema<Extension, typeof countryCode>;
// => string
svsool commented 1 year ago

thank you @ThomasAribart 🙏

smartinio commented 8 months ago

@ThomasAribart Hey 👋 This is unfortunately broken again in 2.12.0 as well as 3.0.0. Using your example