schema

package
v1.4.1 Latest Latest
Warning

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

Go to latest
Published: Aug 26, 2016 License: MIT Imports: 6 Imported by: 0

Documentation

Overview

schema provides a testing utility for testing API types against a predefined JSON schema.

The core philosophy for this package is as follows: when a new API is accepted, JSON Schema files should be added to document the types that are exchanged over this new API. Those files are placed in the `/api/schema` directory, and are used by the schema.Validate function to test that particular instances of these types as represented in Go match the predefined schema that was proposed as a part of the API.

For ease of use, this file defines several constants, one for each schema file's name, to easily pass around during tests.

As briefly described above, to validate that a Go type matches the schema for a particular API call, one should use the schema.Validate() function.

Index

Constants

View Source
const (
	LockListSchema       = "lock_list_schema.json"
	LockRequestSchema    = "lock_request_schema.json"
	LockResponseSchema   = "lock_response_schema.json"
	UnlockRequestSchema  = "unlock_request_schema.json"
	UnlockResponseSchema = "unlock_response_schema.json"
)

Variables

This section is empty.

Functions

func Refute

func Refute(t *testing.T, schemaName string, got interface{})

Refute ensures that a particular Go object does not validate the JSON schema given.

If validation against the schema is successful, then the test will fail after logging.

func Validate

func Validate(t *testing.T, schemaName string, got interface{})

Validate validates a Go object against JSON schema in a testing environment. If the validation fails, then the test will fail after logging all of the validation errors experienced by the validator.

Types

type SchemaValidator

type SchemaValidator struct {
	// Schema is the JSON schema to validate against.
	//
	// Subject is the instance of Go type that will be validated.
	Schema, Subject gojsonschema.JSONLoader
}

SchemaValidator uses the gojsonschema library to validate the JSON encoding of Go objects against a pre-defined JSON schema.

func NewSchemaValidator

func NewSchemaValidator(t *testing.T, schemaName string, got interface{}) *SchemaValidator

func (*SchemaValidator) Assert

func (v *SchemaValidator) Assert(t *testing.T)

Assert preforms the validation assertion against the given *testing.T.

func (*SchemaValidator) Refute

func (v *SchemaValidator) Refute(t *testing.T)

Refute refutes that the given subject will validate against a particular schema.

Jump to

Keyboard shortcuts

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