Json schema

From wikinotes

Define schemas for json/yaml, can be loaded into LSPs or used to validate schemas.
Note that json-schema is for static validation, and you can't define some attributes based on others.

Documentation

official docs https://json-schema.org/specification
reference https://json-schema.org/understanding-json-schema/reference/type
official tutorial https://json-schema.org/learn/getting-started-step-by-step
json schema store (several file formats) https://www.schemastore.org/json/

Example

NOTE:

json-schema does not support comments, they are added here for reference only

{
    // schema metadata
    "$schema": "https://json-schema.org/draft/2020-12/schema",
    "title": "Fitness tracker",
    "description": "A file for tracking fitness info",

    // the root type
    "type": "object",
    "properties": {

        // food is a nested object
        /*
         * food:
         *   anything:
         *     breakdown: ..anything
         *     calories:  123
         *     fat:       3.21
         *     vitamins:
         *       a:      123
         *       omega3: 123
        */
        "food": {
            "type": "object",
            "patternProperties": {  // any string as a key, then define it's schema
                ".*": {
                    "type": "object",
                    "properties": {
                        "breakdown": {},
                        "calories": { "type": ["integer", "null"] },
                        "fat": { "type": ["number", "null"] }
                        "vitamins": {
                          "type": "object",
                          "patternProperties": {
                              ".*": {
                                  "type": "integer"
                              }
                          }
                    }
                }
            }
        },

        // activity is another available key
        /*
         * activity:
         *   anything:
         *     5km_run:  123
         *     10km_run: 456
        */
        "activity": {
            "type": "object",
            "patternProperties": {
                ".*": { "type": ["integer", "null"] }
            }
        }
    }
}

Notes

WARNING:

this is far from comprehensive, just enough to do what I needed for now.

json schema tools
json schema datatypes
json schema conditionals