Skip to content

Latest commit

 

History

History
94 lines (70 loc) · 2.44 KB

File metadata and controls

94 lines (70 loc) · 2.44 KB

Formatting

jsonschema fmt [schemas-or-directories...]
  [--check/-c] [--verbose/-v] [--debug/-g]
  [--resolve/-r <schemas-or-directories> ...]
  [--extension/-e <extension>] [--ignore/-i <schemas-or-directories>]
  [--keep-ordering/-k] [--indentation/-n <spaces>] [--default-dialect/-d <uri>]
  [--json/-j]

Schemas are code. As such, they are expected follow consistent stylistic conventions. Just as code-formatters like clang-format, JavaScript's prettier, and rustfmt, the JSON Schema CLI offers a fmt command to format schemas based on industry-standard conventions and to check their adherence on a continuous integration environment.

This command does not support YAML schemas yet.

Examples

For example, consider this fictitious JSON Schema with inconsistent indentation, spacing, keyword ordering, and more:

{ "$schema":"https://json-schema.org/draft/2020-12/schema",
      "type": "string","pattern": "^(?!0000)\\d{4}$",
  "$id": "https://example.com/iso8601/v1.json",
      "title":    "ISO 8601 four-digit year (YYYY)" }

After formatting it, the JSON Schema looks like this:

{
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "$id": "https://example.com/iso8601/v1.json",
  "title": "ISO 8601 four-digit year (YYYY)",
  "type": "string",
  "pattern": "^(?!0000)\\d{4}$"
}

Format JSON Schemas in-place

jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json

Format JSON Schemas in-place while preserving keyword ordering

jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json --keep-ordering

Format JSON Schemas in-place while indenting on 4 spaces

jsonschema fmt path/to/my/schema_1.json path/to/my/schema_2.json --indentation 4

Format every .json file in a given directory (recursively)

jsonschema fmt path/to/schemas/

Format every .json file in the current directory (recursively)

jsonschema fmt

Format every .json file in a given directory while ignoring another

jsonschema fmt path/to/schemas/ --ignore path/to/schemas/nested

Format every .schema.json file in the current directory (recursively)

jsonschema fmt --extension .schema.json

Check that a single JSON Schema is properly formatted

jsonschema fmt path/to/my/schema.json --check