jsonschema

package module
v0.0.0-...-13e685e Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 9, 2022 License: MIT Imports: 3 Imported by: 3

README

go-jsonschema-generator Build Status GoDoc

Basic json-schema generator based on Go types, for easy interchange of Go structures across languages.

Fork of github.com/mcuadros/go-jsonschema-generator. Thank you very much. Updated and refactored to handle null from pointer fields.

  • uses []string instead of string for the type field, and append "null" in case the field is a pointer.
  • adds format for known sized numbers, {"type":"integer", "format": "i16"}. Supported formats: u8,u16,u32,u64,i8,i16,i32,i64,f32,f64.

Installation

The recommended way to install go-jsonschema-generator

go get github.com/ajzo90/go-jsonschema-generator

Examples

A basic example:

package main

import (
	"fmt"
	"time"

	"github.com/ajzo90/go-jsonschema-generator"
)

type EmbeddedType struct {
	Zoo *string
}

type Item struct {
	Value string
}

type ExampleBasic struct {
	Foo bool   `json:"foo"`
	Bar string `json:",omitempty"`
	Qux *int8
	Baz []string
	EmbeddedType
	List                    []Item
	MyTime                  time.Time
	MapWithUnknownValueType map[string]interface{}
	MapWithKnownValueType   map[string]uint64
}

func main() {
	fmt.Println(jsonschema.New(ExampleBasic{}).Indented())
}
{
  "$schema": "http://json-schema.org/schema#",
  "type": [
    "object"
  ],
  "properties": {
    "Bar": {
      "type": [
        "string"
      ]
    },
    "Baz": {
      "type": [
        "array"
      ],
      "items": {
        "type": [
          "string"
        ]
      }
    },
    "List": {
      "type": [
        "array"
      ],
      "items": {
        "type": [
          "object"
        ],
        "properties": {
          "Value": {
            "type": [
              "string"
            ]
          }
        },
        "required": [
          "Value"
        ]
      }
    },
    "MapWithKnownValueType": {
      "type": [
        "object"
      ],
      "properties": {
        ".*": {
          "type": [
            "integer"
          ],
          "format": "u64"
        }
      }
    },
    "MapWithUnknownValueType": {
      "type": [
        "object"
      ],
      "additionalProperties": true
    },
    "MyTime": {
      "type": [
        "string"
      ],
      "format": "date-time"
    },
    "Qux": {
      "type": [
        "integer",
        "null"
      ],
      "format": "i8"
    },
    "Zoo": {
      "type": [
        "string",
        "null"
      ]
    },
    "foo": {
      "type": [
        "boolean"
      ]
    }
  },
  "required": [
    "foo",
    "Qux",
    "Baz",
    "Zoo",
    "List",
    "MyTime",
    "MapWithUnknownValueType",
    "MapWithKnownValueType"
  ]
}

License

MIT, see LICENSE

Documentation

Overview

Basic json-schema generator based on Go types, for easy interchange of Go structures between diferent languages.

Index

Constants

View Source
const DEFAULT_SCHEMA = "http://json-schema.org/schema#"

Variables

This section is empty.

Functions

This section is empty.

Types

type Document

type Document struct {
	Schema string `json:"$schema,omitempty"`
	// contains filtered or unexported fields
}

func New

func New(v interface{}) *Document

func (*Document) Indented

func (d *Document) Indented() string

func (*Document) Marshal

func (d *Document) Marshal() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL