Generate JSON schema from an argument

Generate JSON schema from an argument#

One can use dargs.json_schema.generate_json_schema() to generate JSON schema.

import json

from dargs import Argument
from dargs.json_schema import generate_json_schema
from deepmd.utils.argcheck import gen_args


a = Argument("DeePMD-kit", dtype=dict, sub_fields=gen_args())
schema = generate_json_schema(a)
with open("deepmd.json", "w") as f:
    json.dump(schema, f, indent=2)

JSON schema can be used in several JSON editors. For example, in Visual Studio Code, you can configure JSON schema in the project settings.json:

{
    "json.schemas": [
        {
            "fileMatch": [
                "/**/*.json"
            ],
            "url": "./deepmd.json"
        }
    ]
}

VS Code also allows one to specify the JSON schema in a JSON file with the $schema key. To be compatible, dargs will not throw an error for $schema in the strict mode even if $schema is not defined in the argument.

{
  "$schema": "./deepmd.json",
  "model": {}
}