bcherny/json-schema-to-typescript

If `minProperties` is greater than or equal to the number of declared properties and `additionalProperties` is `false`, all properties should be required

andreww2012 opened this issue · 2 comments

Hello, I think the following schema should produce the object type with all the properties required:

{
  "type": "object",
  "additionalProperties": false,
  "minProperties": 2,
  "properties": {
    "a": {
      "type": "number"
    },
    "b": {
      "type": "string"
    }
  }
}

The current output:

export interface Demo {
  a?: number;
  b?: string;
}

The expected output:

export interface Demo {
  a: number;
  b: string;
}

What is the usecase? "minProperties" is used for various types of hash tables or when only a subset of any properties listed is required (in which case all properties are optional as far as types concerned). The example above can be expressed as declaring "required" array and it will result in a more readable schema and most likely more performant validation function.

It (kind of) prevents fields duplication problem if all fields are required (I wish JSON schema had an easy and safe way to express this). Kind of, because if new ones are added, one may still forget to update minProperties.

And, well, technically the current behaviour is incorrect.