jsonvalidator

package module
v0.0.0-...-51a0c85 Latest Latest
Warning

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

Go to latest
Published: May 29, 2023 License: ISC Imports: 9 Imported by: 1

README

go-json-validator

Go-json-validator is now available as part of go-ejson.

Introduction

The go-json-validator library contains tooling to validate JSON data. While the standard Go parser will only check basic types, this package lets developers write code to validate any property.

Usage

Refer to the Go package documentation for information about the API.

Example
type SignupData struct {
	EmailAddress         string `json:"emailAddress"`
	Password             string `json:"password"`
	PasswordConfirmation string `json:"password"`
	AcceptTOS            bool   `json:"acceptTOS"`
}

func (d *SignupData) ValidateJSON(v *jsonvalidator.Validator) {
	v.CheckStringMatch2("emailAddress", d.EmailAddress, EmailAddressRE,
		"invalidEmailAddress", "invalid email address")

	v.CheckStringLengthMin("password", d.Password, 8)

	v.Check("passwordConfirmation", d.PasswordConfirmation == d.Password,
		"passwordMismatch", "password confirmation and password do not match")

	v.Check("acceptTOS", d.AcceptTOS, "tosNotAccepted",
		"you must accept terms of services to sign up")
}

Simply implement ValidateJSON for structures that need extra validation, and use jsonvalidation.Unmarshal to both decode and validate data.

Errors are reported using type ValidationErrors which can contain one or more validation errors. Each validation error contains a JSON pointer to the location of the error and an error code to facilitate error handling.

Licensing

Go-json-validator is open source software distributed under the ISC license.

Documentation

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func ConvertUnmarshallingError

func ConvertUnmarshallingError(err error) error

func Unmarshal

func Unmarshal(data []byte, dest interface{}) error

func UnmarshalDecoder

func UnmarshalDecoder(d *json.Decoder, dest interface{}) error

func UnmarshalReader

func UnmarshalReader(r io.Reader, dest interface{}) error

func Validate

func Validate(value interface{}) error

Types

type Validatable

type Validatable interface {
	ValidateJSON(v *Validator)
}

type ValidationError

type ValidationError struct {
	Pointer jsonpointer.Pointer `json:"pointer"`
	Code    string              `json:"code"`
	Message string              `json:"message"`
}

func (ValidationError) Error

func (err ValidationError) Error() string

type ValidationErrors

type ValidationErrors []*ValidationError

func (ValidationErrors) Error

func (errs ValidationErrors) Error() string

type Validator

type Validator struct {
	Pointer jsonpointer.Pointer
	Errors  ValidationErrors
}

func NewValidator

func NewValidator() *Validator

func (*Validator) AddError

func (v *Validator) AddError(token interface{}, code, format string, args ...interface{})

func (*Validator) Check

func (v *Validator) Check(token interface{}, value bool, code, format string, args ...interface{}) bool

func (*Validator) CheckIntMax

func (v *Validator) CheckIntMax(token interface{}, i int, max int) bool

func (*Validator) CheckIntMin

func (v *Validator) CheckIntMin(token interface{}, i int, min int) bool

func (*Validator) CheckIntMinMax

func (v *Validator) CheckIntMinMax(token interface{}, i int, min, max int) bool

func (*Validator) CheckObject

func (v *Validator) CheckObject(token interface{}, value interface{}) bool

func (*Validator) CheckOptionalObject

func (v *Validator) CheckOptionalObject(token interface{}, value interface{}) bool

func (*Validator) CheckStringLengthMax

func (v *Validator) CheckStringLengthMax(token interface{}, s string, max int) bool

func (*Validator) CheckStringLengthMin

func (v *Validator) CheckStringLengthMin(token interface{}, s string, min int) bool

func (*Validator) CheckStringLengthMinMax

func (v *Validator) CheckStringLengthMinMax(token interface{}, s string, min, max int) bool

func (*Validator) CheckStringMatch

func (v *Validator) CheckStringMatch(token interface{}, s string, re *regexp.Regexp) bool

func (*Validator) CheckStringMatch2

func (v *Validator) CheckStringMatch2(token interface{}, s string, re *regexp.Regexp, code, format string, args ...interface{}) bool

func (*Validator) CheckStringNotEmpty

func (v *Validator) CheckStringNotEmpty(token interface{}, s string) bool

func (*Validator) CheckStringURI

func (v *Validator) CheckStringURI(token interface{}, s string) bool

func (*Validator) Error

func (v *Validator) Error() ValidationErrors

func (*Validator) Pop

func (v *Validator) Pop()

func (*Validator) Push

func (v *Validator) Push(token interface{})

func (*Validator) WithChild

func (v *Validator) WithChild(token interface{}, fn func())

Jump to

Keyboard shortcuts

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