Documentation ¶
Overview ¶
Package schema makes it easier to check if map/array structures match a certain schema. Great for testing JSON API's.
Example:
func TestJSON(t *testing.T) { reader := getJSONResponse() err := schema.MatchJSON( schema.Map{ "id": schema.IsInteger, "name": "Max Mustermann", "age": 42, "height": schema.IsFloat, "footsize": schema.IsPresent, "address": schema.Map{ "street": schema.IsString, "zip": schema.IsString, }, "tags": schema.ArrayIncluding("red"), }, reader, ) if err != nil { t.Fatal(err) } }
JSON Input
{ "id": 12, "name": "Hans Meier", "age": 42, "height": 1.91, "address": { "street": 12 }, "tags": ["blue", "green"] }
err.Error() Output
"address": Missing keys: "zip" "address.street": is no string but float64 "name": "Hans Meier" != "Max Mustermann" "tags": red:string(0) not included Missing keys: "footsize"
See https://github.com/jgroeneveld/schema for more examples.
Also see https://github.com/jgroeneveld/trial for lightweight assertions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var IsBool = MatcherFunc("IsBool", isBool)
IsBool checks if a value is a bool
var IsFloat = MatcherFunc("IsFloat", isFloat)
IsFloat checks if a value is a float
var IsInteger = MatcherFunc("IsInteger", isInteger)
IsInteger checks if a value is an integer
var IsPresent = MatcherFunc("IsPresent", isPresent)
IsPresent is a placeholder for Map to check if something "just exists"
var IsString = MatcherFunc("IsString", isString)
IsString checks if a value is an string
Functions ¶
Types ¶
type CaptureMatcher ¶
CaptureMatcher is the exposed interface for Capture
func Capture ¶
func Capture(name string) CaptureMatcher
Capture can be used once or more to capture values and to make sure a value stays the same
type Error ¶
Error is used to record errors that happen during a schema check
type MapIncluding ¶
type MapIncluding map[string]interface{}
MapIncluding checks if a map contains keys that match the given values or matchers
func (MapIncluding) Match ¶
func (m MapIncluding) Match(data interface{}) *Error
Match is the actual matching function to obey the Matcher Interface
type Matcher ¶
type Matcher interface {
Match(data interface{}) *Error
}
Matcher is the interface for all matchers
func Array ¶
func Array(exps ...interface{}) Matcher
Array compares all values of the array one by one. Either specific or other Matchers.
func ArrayEach ¶
func ArrayEach(exp interface{}) Matcher
ArrayEach checks all values of an array against the given value or Matcher
func ArrayIncluding ¶
func ArrayIncluding(exps ...interface{}) Matcher
ArrayIncluding checks that all given Matchers or values are present in the array.
func ArrayUnordered ¶
func ArrayUnordered(exps ...interface{}) Matcher
ArrayUnordered checks that all values or matchers are satisfied, ignoring the order of the array.
func StringEnum ¶
StringEnum checks if a value is included in the given Enumeration
type MatcherFuncImpl ¶
MatcherFuncImpl is a wrapper for a function to obey the Matcher interface
func MatcherFunc ¶
func MatcherFunc(name string, fun func(data interface{}) *Error) *MatcherFuncImpl
MatcherFunc turns a func into a Matcher
func (*MatcherFuncImpl) Match ¶
func (f *MatcherFuncImpl) Match(data interface{}) *Error
Match is the actual matching function
func (*MatcherFuncImpl) String ¶
func (f *MatcherFuncImpl) String() string