schema

package module
v0.1.0 Latest Latest
Warning

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

Go to latest
Published: Jun 7, 2017 License: MIT Imports: 10 Imported by: 2

README

gin-jsonschema: JSON Schema validation wrapper for Gin Build Status

Installation

go get github.com/Hepri/gin-jsonschema

Dependencies :

Usage

var testSchema string = `
{
    "title": "Test Schema",
    "type": "object",
    "properties": {
        "value": {
            "type": "integer"
        }
    },
    "required": ["value"]
}
`

type TestBody struct {
    Value int `json:"value"`
}


// using BindJSON
r.POST("/bind", func(c *gin.Context) {
    var js TestBody
    if schema.BindJSON(c, testSchema, &js) == nil {
        // do stuff
    }
})


// validate using schema as string
r.POST("/string", schema.Validate(handlerFunc, testSchema))


// validate using schema as *gojsonschema.Schema
loader := gojsonschema.NewStringLoader(testSchema)
sc, _ := gojsonschema.NewSchema(loader)
r.POST("/schema", schema.ValidateSchema(handlerFunc, sc))


// using wrapper inside handler
var someHandler = func(c *gin.Context) {
    return schema.Validate(func (c *gin.Context) {
        c.JSON(http.StatusOK, gin.H{
            "message": "OK",
        })
    }, testSchema)
}
r.POST("/wrap_inside", someHandler)

Read possible ways to build *gojsonschema.Schema in documentation

Example HTTP Service

package main

import (
	"github.com/gin-gonic/gin"

	"net/http"

	"github.com/Hepri/gin-jsonschema"
)

var testSchema string = `
{
    "title": "Test Schema",
    "type": "object",
    "properties": {
        "value": {
            "type": "integer"
        }
    },
    "required": ["value"]
}
`

type testBody struct {
	Value int `json:"value"`
}

func handlerFunc(c *gin.Context) {
	c.Status(http.StatusOK)
}

func bindJSONHandler(c *gin.Context) {
	var js testBody
	if schema.BindJSON(c, testSchema, &js) == nil {
		c.JSON(http.StatusOK, gin.H{
			"value": js.Value,
		})
	}
}

func main() {
	r := gin.Default()

	// wrap handler, all invalid json schema requests will produce Bad Request
	r.POST("/validate", schema.Validate(handlerFunc, testSchema))

	// use BindJSON inside handler
	r.POST("/bind", bindJSONHandler)
	r.Run(":8080")
}

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func BindJSON

func BindJSON(c *gin.Context, schemaStr string, obj interface{}) error

func BindJSONSchema

func BindJSONSchema(c *gin.Context, schema *gojsonschema.Schema, obj interface{}) (err error)

func Validate

func Validate(handler gin.HandlerFunc, schemaStr string) gin.HandlerFunc

func ValidateSchema

func ValidateSchema(handler gin.HandlerFunc, schema *gojsonschema.Schema) gin.HandlerFunc

Types

type ErrCannotBuildSchema

type ErrCannotBuildSchema struct {
	// contains filtered or unexported fields
}

func NewErrCannotBuildSchema

func NewErrCannotBuildSchema(err error) *ErrCannotBuildSchema

func (*ErrCannotBuildSchema) Error

func (e *ErrCannotBuildSchema) Error() string

type ErrSchemaValidation

type ErrSchemaValidation struct {
	Errors []string
}

func NewErrSchemaValidation

func NewErrSchemaValidation(errors []string) *ErrSchemaValidation

func (*ErrSchemaValidation) Error

func (e *ErrSchemaValidation) Error() string

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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