json-schema-to-typescript icon indicating copy to clipboard operation
json-schema-to-typescript copied to clipboard

write JSDoc tags for unkown properties

Open elazarcoh opened this issue 4 years ago • 0 comments

Allow user to generate custom/unknown jsonschema properties as JSDoc tags. It should (help? fully?) fixes #237 Example:

// person.json
{
  "title": "Person",
  "type": "object",
  "properties": {
    "firstName": {
      "type": "string"
    },
    "lastName": {
      "type": "string"
    },
    "address": {
      "type": "array",
      "items": {
        "type": "string"
      },
      "default": [
        "123 Main St",
        "Anytown",
        "CA",
        "90210"
      ]
    },
    "age": {
      "description": "Age in years",
      "type": "integer",
      "minimum": 0
    },
    "hairColor": {
      "enum": ["black", "brown", "blue"],
      "type": "string"
    }
  },
  "required": ["firstName", "lastName"]
}
async function generate() {
  writeFileSync('person.d.ts', await compileFromFile('person.json', {jsdocTags: ["description", "minimum" , "default"]}))
}

generate()

generates:

/* tslint:disable */
/**
 * This file was automatically generated by json-schema-to-typescript.
 * DO NOT MODIFY IT BY HAND. Instead, modify the source JSONSchema file,
 * and run json-schema-to-typescript to regenerate this file.
 */

export interface Person {
  firstName: string;
  lastName: string;
  /**
   * @default ["123 Main St","Anytown","CA","90210"]
   */
  address?: string[];
  /**
   * @description "Age in years"
   * @minimum 0
   */
  age?: number;
  hairColor?: "black" | "brown" | "blue";
  [k: string]: unknown;
}

elazarcoh avatar Dec 12 '21 09:12 elazarcoh