dwyl / learn-json-schema

:snowflake: If you have used a REST API you will be familiar with JSON schemas, now do you want to *understand* them...?
7 stars 0 forks source link

Visualise JSON Schema using JSON Schema Viewer #9

Open sohilpandya opened 8 years ago

sohilpandya commented 8 years ago

Why You have a really great schema in place, which is understood by all the technical people in your team, but what about the non-technical people? Visualisation of a schema will lead to better understanding of the schema across the team.

How We will use JSON Schema Viewer to visualise the schema

sohilpandya commented 8 years ago

Steps to creating your very first visualisation of a schema

Repo Setup

  1. Clone repository: git clone https://github.com/jlblcc/json-schema-viewer.git
  2. Enter project directory: cd json-schema-viewer
  3. Install dependencies via Bower: bower install
    • if you don't have bower installed: npm i -g bower. (strange decision by developer to have dependencies across both bower and npm.
  4. Install dependencies via NPM: npm install

Schema Setup

You will notice that the documentation for getting a simple schema up and running is not so straight forward, so here is what I suggest:

schema.json

{
  "id": "schema.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "version": "1.1.3",
  "description": "schema for ADIwg mdJSON metadata",
  "type": "object",
  "required": ["version", "contact", "metadata"],
  "properties": {
    "user": {
      "$ref": "sub-schema/user.json",
      "example": "../schema-example/user.json"
    }
  }
}

user.json

{
  "id": "user.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "Contains user details",
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "address": {
      "type":"object",
      "properties": {
        "billing_address": {
          "$ref": "address.json",
          "example": "../schema-example/address.json"
        },
        "delivery_address": {
          "$ref": "address.json",
          "example": "../schema-example/address.json"
        }
      }
    },
    "age": {
      "type":"number",
      "maxValue": 100
    }
  }
}

address.json

{
  "id": "address.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "Contains user address",
  "properties": {
    "street_name": {
      "type": "string"
    },
    "city": {
      "type": "string"
    },
    "postcode": {
      "type": "string"
    },
    "country": {
      "type": "string"
    }
  }
}

JSON-Schema-Viewer Setup

screen shot 2016-11-03 at 15 46 04

Build & Run JSON Schema Viewer

We have our very first JSON Schema Viewer

_And it should look something like this 🎉 _ screen shot 2016-11-03 at 15 32 07

To add examples for each json file, simple add some examples in json file and link it to the schema

Example user.json

{
  "id": "user.json#",
  "$schema": "http://json-schema.org/draft-04/schema#",
  "type": "object",
  "description": "Contains user details",
  "properties": {
    "first_name": {
      "type": "string"
    },
    "last_name": {
      "type": "string"
    },
    "address": {
      "type":"object",
      "properties": {
        "billing_address": {
          "$ref": "address.json",
          "example": "../schema-example/address.json"
        },
        "delivery_address": {
          "$ref": "address.json",
          "example": "../schema-example/address.json"
        }
      }
    },
    "age": {
      "type":"number",
      "maxValue": 100
    }
  }
}
sohilpandya commented 8 years ago

Having experimented with this a little more, I feel that JSON Schema Viewer is rough around the edges, It works great if your schema is not too complex.