Giter Club home page Giter Club logo

go-json-schema-generator's Introduction

go-json-schema-generator

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"
                }
            ]
        }
    }
}

go-json-schema-generator's People

Contributors

bigblind avatar urakozz avatar

Watchers

 avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.