Skip to content
This repository has been archived by the owner on May 23, 2021. It is now read-only.

Generate JSON Schema from TypeScript #50

Open
azu opened this issue Aug 25, 2020 · 0 comments
Open

Generate JSON Schema from TypeScript #50

azu opened this issue Aug 25, 2020 · 0 comments

Comments

@azu
Copy link
Member

azu commented Aug 25, 2020

textlint-script build can generate JSON Schema of rule options from TypeScript type definition.

It allows textlint to introduce option validation and help us to create dynamic option UI.

mport * as TJS from "typescript-json-schema";
import path from "path";

    const settings: TJS.PartialArgs = {
        required: true
    };
    const compilerOptions: TJS.CompilerOptions = {
        strictNullChecks: true
    };
    const basePath = path.join(__dirname, "fixtures");
    const program = TJS.getProgramFromFiles(
        [path.join(__dirname, "fixtures/rule.ts")],
        compilerOptions,
        basePath
    );
    const schema = TJS.generateSchema(program, "Options", settings);
    console.log(schema);

input

import type { TextlintRuleModule } from "@textlint/types";

export type Options = {
    k1: string;
    k2: number;
    k3: {
        k3a: string;
    }
}
const reporter: TextlintRuleModule<Options> = (_context) => {
    return {};
};
export default {
    linter: reporter,
    fixer: reporter
};

output

{
  type: 'object',
  properties: {
    k1: { type: 'string' },
    k2: { type: 'number' },
    k3: { type: 'object', properties: [Object], required: [Array] }
  },
  required: [ 'k1', 'k2', 'k3' ],
  '$schema': 'http://json-schema.org/draft-07/schema#'
}

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant