goschema

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

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

Go to latest
Published: Sep 19, 2017 License: LGPL-3.0 Imports: 9 Imported by: 1

README

goschema

Define data-structure validation in go code. Can be used to validate data from json/yaml files. Uses https://github.com/xeipuuv/gojsonschema to do the actual validation.

usage

It's implemented in a fluent interface style:

personSchema := NewObjectType("Example person schema", func(p ObjectType) {
	p.String("firstName", "")
	p.String("lastName", "").Optional()
	p.Int("age", "Age in years").Min(0).Max(5).Optional()
	p.Enum("gender", "").Add("male", "A male specimen").Add("female", "A female specimen")
})
// person must be a map with string keys
// it can also be loaded from a json file with json.Unmarshal(data, mapPointer)
goschema.Validate(personSchema, person)

The validation is executed by generating a jsonschema, which is then validated against the datastructure:

{
  "additionalProperties":false,
  "properties":{
    "age":{
      "description":"Age in years",
      "maximum":5,
      "minimum":0,
      "type":"integer"
    },
    "firstName":{
      "type":"string"
    },
    "gender":{
      "enum":[
        "male",
        "female"
      ],
      "type":"string"
    },
    "lastName":{
      "type":"string"
    }
  },
  "required":[
    "firstName",
    "gender"
  ],
  "title":"Example Schema",
  "type":"object"
}

doc generation

Creating plain text output with documentation of the validation format in the sense of:

Example person schema
---------------------
age       //  optional, Age in years as int from 0 to 5
firstName // firstName as string
gender    // 
  male    // A male speciemen
  female  // A female speciemen
lastName  //  optional, lastName as string

can be done with

str := goschema.Doc(personSchema)

This data can be extracted from the defined values above including the help string which is usually the second parameter in data-type definition calls

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func AsGOJSONSchema

func AsGOJSONSchema(t Type) interface{}

AsGOJSONSchema returns the validator as a map, slice, scalar composite defining the json schema

func AsJSONSchema

func AsJSONSchema(t Type) (string, error)

AsJSONSchema returns the validator as a json schema string

func Doc

func Doc(t Type) string

Doc returns a string containing a very coarse documentation built from the validator definition

func ValidateGO

func ValidateGO(t Type, document interface{}) *gutil.ErrorCollector

ValidateGO will validate the provided interface which can be a composite of maps, slices and scalars, restricted to constructs that are allowed in json (map keys must be strings, only float64, string, bool as scalars)

Types

type AnyType

type AnyType interface {
	Type
}

AnyType can be just anything, map, list, null, scalar...

func NewAnyType

func NewAnyType(description string) AnyType

type BoolType

type BoolType interface {
	Type
}

BoolType validates that the value is of type bool

func NewBoolType

func NewBoolType(description string) BoolType

type EnumType

type EnumType interface {
	Type
	Add(key string, desc string) EnumType
}

EnumType validates that a given string is one of the provided values

func NewEnumType

func NewEnumType(description string) EnumType

type IntType

type IntType interface {
	Type
	Min(min int) IntType
	Max(min int) IntType
}

IntType validates

func NewIntType

func NewIntType(description string) IntType

type ListType

type ListType interface {
	Type
	TypeDefs
}

ListType validates that the value is a list values

func NewListType

func NewListType(subType func(m ListType)) ListType

type MapType

type MapType interface {
	Type
	TypeDefs
}

MapType validates that the element is a map/hash (key value pairs) key is always string, value can be defined

func NewMapType

func NewMapType(description string, subType func(m MapType)) MapType

type NullType

type NullType interface {
	Type
}

NullType validates that the given value is nil/null

func NewNullType

func NewNullType(description string) NullType

type ObjectAttribute

type ObjectAttribute interface {
	TypeDefs
}

ObjectAttribute validates one single attribute of an object

type ObjectType

type ObjectType interface {
	Type
	Attribute(name string) ObjectAttribute
	Optional(name string) ObjectAttribute
}

ObjectType validates that the element is an object with named attributes

func NewObjectType

func NewObjectType(description string, each func(ObjectType)) ObjectType

type SomeOf

type SomeOf interface {
	Type
	TypeDefs
}

SomeOf validates that the value is of at least one of the defined types

func NewSomeOf

func NewSomeOf(subTypes func(m SomeOf)) SomeOf

type StringType

type StringType interface {
	Type
}

StringType validates that the value is a string

func NewStringType

func NewStringType(description string) StringType

type Type

type Type interface {
	// contains filtered or unexported methods
}

Type is an interface that is implemented by all nodes of a validation tree

type TypeDefs

type TypeDefs interface {
	String(desc string) StringType
	Int(desc string) IntType
	Bool(desc string) BoolType
	SomeOf(ops func(SomeOf)) SomeOf
	Enum(desc string) EnumType
	Null(desc string) NullType
	Any(desc string) AnyType
	Object(desc string, ops func(ObjectType)) ObjectType
	Map(ops func(MapType)) MapType
	List(ops func(ListType)) ListType
}

TypeDefs provides methods to define a validator for a child of collection validators

Jump to

Keyboard shortcuts

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