jsonapivalidator

package module
v0.0.0-...-14362f4 Latest Latest
Warning

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

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

README

jsonapivalidator Build Status GoDoc

A validator that determines whether arbitrary JSON payloads are in compliance with the JSON API specification. If the JSON is not compliant this validator will indicate the reason why.

This validator has been written in Go, but it is easy to use it in a Javascript environment thanks to GopherJS; see github.com/aren55555/corroborate for a working JS implementation.

Installation

go get -u github.com/aren55555/jsonapivalidator

Examples

Valid JSON API Payload
req, err := http.DefaultClient.Get("https://raw.githubusercontent.com/aren55555/jsonapivalidator/master/test/samples/valid/default.json")

result, err := jsonapivalidator.UnmarshalAndValidate(req.Body)
if err != nil {
  panic(err)
}

if result.IsValid() {
  fmt.Println("The JSON sample was valid!")
}
Invalid JSON API Payload
req, err = http.DefaultClient.Get("https://raw.githubusercontent.com/aren55555/jsonapivalidator/master/test/samples/invalid/default.json")

result, err = jsonapivalidator.UnmarshalAndValidate(req.Body)
if err != nil {
  panic(err)
}

fmt.Println("Errors:")
for i, err := range result.Errors() {
  fmt.Println("\t", i, err)
}

fmt.Println("Warnings:")
for i, err := range result.Warnings() {
  fmt.Println("\t", i, err)
}

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// ErrInvalidDocumentStructure is the error returned when the JSON was not
	// a POJO at the top level
	ErrInvalidDocumentStructure = errors.New(`A JSON object MUST be at the ` +
		`root of every JSON API request and response containing data. This ` +
		`object defines a document's "top level".`)

	// ErrAtLeastOneRoot is the error returned when a document is missing a top
	// level member
	ErrAtLeastOneRoot = errors.New("A document MUST contain at least one of the" +
		"following top-level members: /data, /errors or /meta")
	// ErrRootDataAndErrors is the error returned when the top level document
	// contains both data and errors members
	ErrRootDataAndErrors = errors.New("The members /data and /errors MUST NOT " +
		"coexist in the same document")
	// ErrRootIncludedWithoutData is the error returned when the top level
	// document contains an included member without a data member
	ErrRootIncludedWithoutData = errors.New("The /included member MUST NOT be " +
		"be present if there is no /data member")

	// ErrInvalidDataType is for data not being a hash array or null JSON type
	ErrInvalidDataType = errors.New("/data must contain a value that is a {}, " +
		"[] or null")

	// ErrInvalidIncludedType is for errors not being an array
	ErrInvalidIncludedType = errors.New("/included must contain a []")

	// ErrInvalidErrorsType is for errors not being an array
	ErrInvalidErrorsType = errors.New("/errors must contain a []")
	// ErrNotErrorObject is the error when the error object is not a {} with
	// string keys
	ErrNotErrorObject = errors.New("The value at the error key was " +
		"not a valid error object {}")
	// ErrInvalidErrorMember is for when the errors object has an unexpected
	// member key
	ErrInvalidErrorMember = errors.New("Invalid member to /errors")

	// ErrNotJSONAPIObject is for when the "jsonapi" member in the root doc was
	// not a {}
	ErrNotJSONAPIObject = errors.New("/jsonapi must contain a value that is a {}")

	// ErrResourceObjectMissingID is returned when a resouce object did not have
	// any value for the id key
	ErrResourceObjectMissingID = errors.New("A resource object MUST contain an " +
		"id")
	// ErrResourceObjectMissingType is returned when a resouce object did not have
	// any value for the type key
	ErrResourceObjectMissingType = errors.New("A resource object MUST contain a" +
		" type")
	// ErrResourceObjectIdentifierMissingID is returned when a resouce object did
	// not have any value for the id key
	ErrResourceObjectIdentifierMissingID = errors.New("A resource object MUST " +
		"contain an id")
	// ErrResourceObjectIdentifierMissingType is returned when a resouce object
	// did not have any value for the type key
	ErrResourceObjectIdentifierMissingType = errors.New("A resource object MUST" +
		" contain a type")
	// ErrNotAResource is returend when something should have been a resource
	// object or a resource identifier
	ErrNotAResource = errors.New("Was not a resource object or a single " +
		"resource identifier object")

	// ErrIDNotString is for when an id member was anything but a string
	ErrIDNotString = errors.New("id was not a string")
	// ErrTypeNotString is for when a type member was anything but a string
	ErrTypeNotString = errors.New("type was not a string")
	// ErrInvalidURL is the error returned when a value should have been a URL but
	// was an invalid URL
	ErrInvalidURL = errors.New("URL was not a valid URL")

	// ErrNotAttributesObject is the error when the "attributes" value was not a
	// hash {}
	ErrNotAttributesObject = errors.New("The value at the attributes key was " +
		"not a valid attributes object {}")

	// ErrNotMetaObject is the error when the "attributes" value was not a
	// hash {}
	ErrNotMetaObject = errors.New("The value at the meta key was " +
		"not a valid meta object {}")

	// ErrNotRelationshipsObject is the error when the "relationships" value was
	// not a hash {}
	ErrNotRelationshipsObject = errors.New("The value at the relationships key " +
		"was not a valid relationships object {}")
	// ErrRelationshipsObjectOneRequiredMember is used in the case where a
	// relationships object did not have at least one of the required members
	ErrRelationshipsObjectOneRequiredMember = errors.New("A relationship object" +
		" MUST contain at least one of the following: links, data or meta")
	// ErrNotRelationshipObject is the error when a key in a "relationships" {}
	// did not contain a {} also
	ErrNotRelationshipObject = errors.New("The value for the relationship was " +
		"not a valid relationship object {}")
	// ErrInvalidResourceLinkage is the error that is returned if the resource
	// linkage was not represented
	ErrInvalidResourceLinkage = errors.New("Resource linkage MUST be " +
		"represented as one of the following: null for empty-to-one " +
		"relationships, an empty array [] for empty to-many relationships," +
		"a single resource identifier object for non-empty to-one relationships," +
		"or an array of resource identifier objects for non-empty to-many " +
		"relationships")

	// ErrNotLinksObject is the error returned if the links object could not be
	// type asserted as a {string => *}
	ErrNotLinksObject = errors.New("The value at the links key was " +
		"not a valid links object {}")
	// ErrNotLinkObject is the error returned if the link object could not be
	// type asserted as a {string => *}
	ErrNotLinkObject = errors.New("The value at the link key was " +
		"not a valid link object {}")
	// ErrInvalidLinkType is the error returned if a link object was something
	// other than a {} or string
	ErrInvalidLinkType = errors.New("A link MUST be represented as either: a " +
		"a string containing the link’s URL OR a link object")
	// ErrInvalidLinkMember is the error returned if a link object member was not
	// href or meta
	ErrInvalidLinkMember = errors.New("Invalid member to link object; only " +
		"href and meta are permitted")
)
View Source
var (
	// WarnAttributesObjectFK is the warning produced when FKs may have been
	// detected in the attributes object
	WarnAttributesObjectFK = errors.New("Although has-one foreign keys (e.g. author_id) " +
		"are often stored internally alongside other information to be " +
		"represented in a resource object, these keys SHOULD NOT appear as " +
		"attributes.")

	// WarnAttributesObjectHasRelationshipsMember is the warning produced when a
	// relationships member name has been detected anywhere in the attributes
	// object
	WarnAttributesObjectHasRelationshipsMember = errors.New("Complex data " +
		"structures involving JSON objects and arrays are allowed as attribute " +
		"values. However, any object that constitutes or is contained in an " +
		"attribute MUST NOT contain a relationships member, as it has been " +
		"reserved by this specification for future use.")

	// WarnAttributesObjectHasLinksMember is the warning produced when a
	// relationships member name has been detected anywhere in the attributes
	// object
	WarnAttributesObjectHasLinksMember = errors.New("Complex data " +
		"structures involving JSON objects and arrays are allowed as attribute " +
		"values. However, any object that constitutes or is contained in an " +
		"attribute MUST NOT contain a links member, as it has been " +
		"reserved by this specification for future use.")
)

Functions

This section is empty.

Types

type Result

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

Result is the container for wrapping validation results

func NewResult

func NewResult() (r *Result)

NewResult instantiates a Result struct

func UnmarshalAndValidate

func UnmarshalAndValidate(data io.Reader) (result *Result, err error)

UnmarshalAndValidate will read the JSON data and provide a validator result

func Validate

func Validate(root interface{}) (result *Result)

Validate checks the root payload against the JSONAPI sepc, returns a Result populated with all spec violations.

func (*Result) AddError

func (r *Result) AddError(err error)

AddError will append the error to the Result

func (*Result) AddWarning

func (r *Result) AddWarning(warning error)

AddWarning will append the error to the Result

func (*Result) Errors

func (r *Result) Errors() (errors []error)

Errors will return all the errors in the result as a slice

func (*Result) HasError

func (r *Result) HasError(err error) bool

HasError checks whether the Result has the particular Error

func (*Result) HasErrors

func (r *Result) HasErrors() bool

HasErrors is whether or not the Result has Errors in general

func (*Result) HasWarning

func (r *Result) HasWarning(warning error) bool

HasWarning checks whether the Result has the particular Error

func (*Result) HasWarnings

func (r *Result) HasWarnings() bool

HasWarnings is whether or not the Result has Errors in general

func (*Result) IsValid

func (r *Result) IsValid() bool

IsValid summarizes if the validation result was valid or not

func (*Result) Warnings

func (r *Result) Warnings() (warnings []error)

Warnings will return all the warnings in teh result as a slice

Directories

Path Synopsis

Jump to

Keyboard shortcuts

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