Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Generate Json Schema #6

Open
SorsOps opened this issue Mar 19, 2023 · 1 comment
Open

Generate Json Schema #6

SorsOps opened this issue Mar 19, 2023 · 1 comment
Labels
enhancement New feature or request good first issue Good for newcomers

Comments

@SorsOps
Copy link
Member

SorsOps commented Mar 19, 2023

We have strong typing available through Typescript. Could we use Typescript reflection on these to then generate json schemas for downstream users who might not have Typescript available. The first obvious use case would be providing $schema values for json parsers like the Monaco editor.

A simple implementation is shown below , but I don't want any handwritten schemas like this being distributed if they can get out out of sync.

{
  "$id": "https://example.com/address.schema.json",
  "$schema": "https://json-schema.org/draft/2020-12/schema",
  "description": "Design Tokens Draft spec v1",
  "definitions": {
    "boxShadowToken": {
      "type": "object",
      "properties": {
        "value": {
          "type": [
            "string",
            "object"
          ],
          "additionalProperties": false,
          "properties": {
            "x": {
              "type": "string"
            },
            "y": {
              "type": "string"
            },
            "blur": {
              "type": "string"
            },
            "spread": {
              "type": "string"
            },
            "color": {
              "type": "string"
            },
            "type": {
              "type": "string"
            }
          }
        },
        "extensions": {
          "type": "object"
        },
        "type": {
          "type": "string",
          "enum": [
            "boxShadow"
          ]
        },
        "description": {
          "type": "string"
        }
      },
      "required": [
        "type",
        "value"
      ]
    },
    "borderToken": {
      "type": "object",
      "properties": {
        "value": {
          "type": [
            "string",
            "object"
          ],
          "additionalProperties": false,
          "properties": {
            "color": {
              "type": "string"
            },
            "width": {
              "type": "string"
            },
            "style": {
              "type": "string"
            }
          }
        },
        "extensions": {
          "type": "object"
        },
        "description": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "border"
          ]
        }
      },
      "required": [
        "type",
        "value"
      ]
    },
    "typographyToken": {
      "type": "object",
      "properties": {
        "value": {
          "type": [
            "string",
            "object"
          ],
          "additionalProperties": false,
          "properties": {
            "fontFamily": {
              "type": "string"
            },
            "fontWeight": {
              "type": "string"
            },
            "lineHeight": {
              "type": "string"
            },
            "fontSize": {
              "type": "string"
            },
            "letterSpacing": {
              "type": "string"
            },
            "paragraphSpacing": {
              "type": "string"
            },
            "paragraphIndent": {
              "type": "string"
            },
            "textDecoration": {
              "type": "string"
            },
            "textCase": {
              "type": "string"
            }
          }
        },
        "extensions": {
          "type": "object"
        },
        "description": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "typography"
          ]
        }
      },
      "required": [
        "type",
        "value"
      ]
    },
    "token": {
      "type": "object",
      "properties": {
        "value": {
          "type": "string"
        },
        "extensions": {
          "type": "object"
        },
        "description": {
          "type": "string"
        },
        "type": {
          "type": "string",
          "enum": [
            "fontWeights",
            "borderRadius",
            "borderWidth",
            "color",
            "spacing",
            "dimension",
            "sizing",
            "fontFamilies",
            "other",
            "lineHeights",
            "opacity",
            "fontSizes",
            "textCase",
            "textDecoration",
            "paragraphSpacing"
          ]
        }
      },
      "required": [
        "type",
        "value"
      ]
    },
    "tokenScope": {
      "type": "object",
      "patternProperties": {
        "^[A-Za-z0-9\\._-]+$": {
          "oneOf": [
            {
              "$ref": "#/definitions/tokenScope"
            },
            {
              "$ref": "#/definitions/token"
            },
            {
              "$ref": "#/definitions/typographyToken"
            },
            {
              "$ref": "#/definitions/boxShadowToken"
            },
            {
              "$ref": "#/definitions/borderToken"
            }
          ]
        }
      }
    }
  },
  "type": "object",
  "patternProperties": {
    "^[A-Za-z0-9\\._-]+$": {
      "type": "object",
      "required": true,
      "oneOf": [
        {
          "$ref": "#/definitions/tokenScope"
        },
        {
          "$ref": "#/definitions/token"
        },
        {
          "$ref": "#/definitions/typographyToken"
        },
        {
          "$ref": "#/definitions/boxShadowToken"
        },
        {
          "$ref": "#/definitions/borderToken"
        }
      ]
    }
  }
}
@jorenbroekema
Copy link
Member

Good idea!

Is it possible to generate a JSON schema from a Typescript type automatically? We'd need to target the SingleToken type then I think, and generate it on prepublish, so it's always in sync with the types.

PRs welcome!

@jorenbroekema jorenbroekema added enhancement New feature or request good first issue Good for newcomers labels Mar 19, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers
Projects
None yet
Development

No branches or pull requests

2 participants