generator

package module
v0.0.0-...-4821386 Latest Latest
Warning

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

Go to latest
Published: Apr 15, 2020 License: MIT Imports: 5 Imported by: 0

README

go-json-schema-generator

codecov travisci

Generate JSON Schema out of Golang schema

Usage

First install the package

go get -u github.com/urakozz/go-json-schema-generator

Then create your generator file (see Example folder)

package main

import (
	"fmt"
	"github.com/urakozz/go-json-schema-generator"
)

type Domain struct {
	Data string `json:"data"`
}

func main(){
	fmt.Println(generator.Generate(&Domain{}))
}
Supported tags
  • required:"true" - field will be marked as required
  • description:"description" - description will be added

On string fields:

  • minLength:"5" - Set the minimum length of the value
  • maxLength:"5" - Set the maximum length of the value
  • enum:"apple|banana|pear" - Limit the available values to a defined set, separated by vertical bars
  • const:"I need to be there" - Require the field to have a specific value.

On numeric types (strings and floats)

  • min:"-4.141592" - Set a minimum value
  • max:"123456789" - Set a maximum value
  • exclusiveMin:"0" - Values must be strictly greater than this value
  • exclusiveMax:"11" - Values must be strictly smaller than this value
  • const:"42" - Property must have exactly this value.
Expected behaviour

If struct field is pointer to the primitive type, then schema will allow this typa and null. E.g.:

type Domain struct {
	NullableData *string `json:"nullableData"`
}

Output

{
    "$schema": "http://json-schema.org/schema#",
    "type": "object",
    "properties": {
        "nullableData": {
            "anyOf": [
                {
                    "type": "string"
                },
                {
                    "type": "null"
                }
            ]
        }
    }
}

Documentation

Overview

Copyright Kozyrev Yury MIT license.

Index

Constants

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

Variables

This section is empty.

Functions

func Generate

func Generate(v interface{}) string

Types

type Document

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

func (*Document) Read

func (d *Document) Read(variable interface{}) *Document

Reads the variable structure into the JSON-Schema Document

func (*Document) String

func (d *Document) String() string

String return the JSON encoding of the Document as a string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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