Skip to content

Conversation

Karan-Palan
Copy link

@Karan-Palan Karan-Palan commented Sep 22, 2025

Fixes #623

Introduced linting schemas and auto-fixing linting issues in the schemas by integrating the @sourcemeta/jsonschema cli

Changes made:

  1. New commands added:
$ node bin/typescript-json-schema --help
Usage: typescript-json-schema <path-to-typescript-files-or-tsconfig> <type>

Options:
      --help                    Show help                         [boolean]
      {Other commands}
      --lint                    Lint generated schemas for JSON Schema best
                                practices and report issues
                                                 [boolean] [default: false]
      --fix                     Automatically fix linting issues in
                                generated schemas[boolean] [default: false]
      --lintStrict              Enable strict linting rules (use with --fix
                                to apply strict fixes)
                                                 [boolean] [default: false]
  1. Lint Generated Schemas
$ node bin/typescript-json-schema test/programs/enums-string/main.ts MyObject --lint
JSON Schema linting issues found:
../../../../../../../tmp/jsonschema-lint-IZFqAX/schema.json:5:21:
  Setting `type` alongside `enum` is considered an anti-pattern, as the enumeration choices already imply their respective types (enum_with_type)
    at schema location "/definitions/Enum/enum"

Run with --fix to automatically fix these issues.
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "definitions": {
        "Enum": {
            "enum": [
                "x",
                "y",
                "123"
            ],
            "type": "string"
        }
    },
    "properties": {
        "foo": {
            "$ref": "#/definitions/Enum"
        }
    },
    "type": "object"
}
  1. Autofix linting issues
$ node bin/typescript-json-schema test/programs/enums-string/main.ts MyObject --fix
JSON Schema auto-fix completed successfully.
{
  "$schema": "http://json-schema.org/draft-07/schema#",
  "definitions": {
    "Enum": {
      "enum": [ "x", "y", "123" ]
    }
  },
  "properties": {
    "foo": {
      "$ref": "#/definitions/Enum"
    }
  },
  "type": "object"
}
  1. Enable strict/opinionated rules and autofix linting rules
$ node bin/typescript-json-schema test/programs/interface-single/main.ts MyObject --fix --lintStrict
JSON Schema auto-fix completed successfully.
{
    "$schema": "http://json-schema.org/draft-07/schema#",
    "properties": {
        "propA": {
            "type": "number"
        },
        "propB": {
            "type": "number"
        }
    },
    "type": "object"
}

Checklist

  • Tests pass
  • No errors when linting
  • Code Formatted

Note:

For context, I, along with Juan (JSON Schema TSC member) defined linting rules for JSON Schema as a Part of a GSoC (Google Summer of code) project here - https://github.com/Karan-Palan/JSON-Schema-Linting, and implementing their auto-fixes here - https://github.com/sourcemeta/jsonschema/blob/main/docs/lint.markdown. This is what we have accomplished so far - https://github.com/Karan-Palan/GSoC-2025/blob/main/Final-report.md. We have recently added many rules prefixing unknown keywords with x- which will be introduced in the newer JSON Schema drafts. We tested the linter on real world schemas and it was adopted by various projects like Vaccum, Manfred and many others. Many projects like ModelContextProtocol use this package for schema generation. One of their maintainers suggested to have the cli integrated in this package itself.

Please:

  • Make your pull request atomic, fixing one issue at a time unless there are many relevant issues that cannot be decoupled.
  • Provide a test case & update the documentation in the Readme.md

Copy link
Collaborator

@domoritz domoritz left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pull request. I'm inclined not to merge this as one can easily call your cli afterwards. I don't yet see the benefit of integrating linting into this tool. By separating the tools, people can lint with the version they prefer.

I do like the idea of lining all generates examples in the tests, though. Can you change the pull request to only do that?

});

describe("satisfies keyword - ignore from a \"satisfies\" and build by rally type", () => {
describe('satisfies keyword - ignore from a "satisfies" and build by rally type', () => {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please use consistent quotes


if (!options.fix && result) {
console.log("JSON Schema lint results:");
console.log(result);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you also set the exit code?

}

try {
const result = execSync(cmd, {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too happy about calling a tool like this. Don't you have a more direct/js way to call your library?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Add Support for JSON Schema Linting Best Practices via jsonschema CLI

2 participants