Giter VIP home page Giter VIP logo

ts-transformer-json-schema's People

Contributors

geomaster avatar ipetrovic11 avatar kimamula avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

ts-transformer-json-schema's Issues

Nested interfaces

interface IUser {
    email: string;
    password: string;
}

interface ICompany {
    workspace: string;
}

interface IOnboardRequest {
    user: IUser;
    company: ICompany;
}

Optional enums generate invalid schema when strict mode enabled

This is a weird one.

An example:

import { schema } from 'ts-transformer-json-schema';

enum MyEnum {
    A = 'A',
    B = 'B'
}

interface IContainsMyEnum {
    enum?: MyEnum;
}

console.log(JSON.stringify(schema<IContainsMyEnum>(), null, 2));

When strict mode is disabled in tsconfig.json (i.e. strict property not set in compilerOptions), the output is as expected:

{
  "enum": {
    "type": "enum",
    "values": [
      "A",
      "B"
    ],
    "optional": true
  }
}

However, when strict mode is enabled (i.e. "strict": "true" in compilerOptions), the output is this:

{
  "enum": [
    {
      "type": "MyEnum.A"
    },
    {
      "type": "MyEnum.B"
    },
    {
      "type": "forbidden"
    }
  ]
}

fastest-validator then complains that MyEnum.A etc. are not valid types, which is correct.

It should be noted that this does not occur with non-optional enums, i.e. they generate the correct enum type in the validation schema.

Enumberables

enum UserGroup {
    Admin = 'admin',
    Manager = 'manager',
    Employee = 'employee'
}

enum UserConfirmationState {
    Pending = 'pending',
    Confirmed = 'confirmed'
}

interface IUserWithGroupsAndState {
    groups: UserGroup[];
    confirmation: UserConfirmationState;
}

Nested objects are generated incorrectly.

interface IInner {
  num: number;
  str: string;
}

interface IOuter {
  nested: IInner;
  num: number;
}

Should generate the following output:

{
  num: { type: 'number'},
  nested: {
    type: "object", props: {
      num: { type: 'number'},
      str: { type: 'string'}
    },
}

However it currently generates the following:

{
  type: "object",
  props: {
    num: { type: 'number'}
    nested: {
      num: { type: 'number'},
      str: { type: 'string'}
    },			
  }
}

Validate with the fastest validator how it handles optional union.

Currently:

interface IOptional {
        readonly optional?: string | number;
    }

is resolved into:

{
    type: 'object', props: {
        optional: [{ type: "string" }, { type: "number" }]
    }
}

which actually just ignores ?(question mark - optional).

fastest-validator doesn't have example how that should be handled, there are two options on my mind currently:

{
    type: 'object', props: {
        optional: [{ type: "string" }, { type: "number" },{type: "forbidden"}]
    }
}

or

{
    type: 'object', props: {
        optional: {
            optional: true, type: [{ type: "string" }, { type: "number" }]
        }
    }
}

Infinite recursion issue

interface IStep1 {
  step2: IStep2;
}

interface IStep2 {
  step1: IStep1;
}

In this case there is infinite recursion which should be stopped.
Proposal for solution:

  • Once infinite recursion is detected that same element type repeats somewhere in the tree, it should be defined as {type: "any"}

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.