Skip to content

Instantly share code, notes, and snippets.

@CatTail
Last active December 7, 2016 09:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save CatTail/4069953c51325e20a0a3d6b2598d91db to your computer and use it in GitHub Desktop.
Save CatTail/4069953c51325e20a0a3d6b2598d91db to your computer and use it in GitHub Desktop.
Generate json schema definition (yaml format) from example snippt
#! /bin/bash
# npm install -g yamljs json-schema-generator
# json-pretty came from [here](https://gist.github.com/CatTail/fc172a7fe6f300528665e279592c6500)
cat "${1:-/dev/stdin}" | json-pretty | json-schema-generator | tail -n +2 | json2yaml -d 10 -
@CatTail
Copy link
Author

CatTail commented Dec 7, 2016

Input

{
  account_message_id: "1",
  "read_at": 1444444444, // comment
  "create_at": 1444444444,
  "title": "example title",
  "type": "some_type",
}

Output

$schema: 'http://json-schema.org/draft-04/schema#'
description: ""
type: object
properties:
  account_message_id:
    type: string
    minLength: 1
  read_at:
    type: number
  create_at:
    type: number
  title:
    type: string
    minLength: 1
  type:
    type: string
    minLength: 1
required:
  - account_message_id
  - read_at
  - create_at
  - title
  - type

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment