Skip to content

Latest commit

 

History

History
77 lines (58 loc) · 1.93 KB

format.markdown

File metadata and controls

77 lines (58 loc) · 1.93 KB

Formatting

jsonschema fmt [schemas-or-directories...]
  [--check/-c] [--verbose/-v] [--extension/-e <extension>]
  [--ignore/-i <schemas-or-directories>]

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.

Examples

For example, consider this fictitious JSON Schema with inconsistent identation, 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 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