validate

package
v0.0.0-...-d3ccc4f Latest Latest
Warning

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

Go to latest
Published: Sep 11, 2018 License: Apache-2.0, Apache-2.0 Imports: 17 Imported by: 0

README

Validation helpers Build Status codecov Slack Status

license GoDoc

Documentation

Index

Constants

This section is empty.

Variables

View Source
var (
	// Debug is true when the SWAGGER_DEBUG env var is not empty
	Debug = os.Getenv("SWAGGER_DEBUG") != ""
)

Functions

func AgainstSchema

func AgainstSchema(schema *spec.Schema, data interface{}, formats strfmt.Registry) error

AgainstSchema validates the specified data with the provided schema, when no schema is provided it uses the json schema as default

func Enum

func Enum(path, in string, data interface{}, enum interface{}) *errors.Validation

Enum validates if the data is a member of the enum

func FormatOf

func FormatOf(path, in, format, data string, registry strfmt.Registry) *errors.Validation

FormatOf validates if a string matches a format in the format registry

func MaxItems

func MaxItems(path, in string, size, max int64) *errors.Validation

MaxItems validates that there are at most n items in a slice

func MaxLength

func MaxLength(path, in, data string, maxLength int64) *errors.Validation

MaxLength validates a string for maximum length

func Maximum

func Maximum(path, in string, data, max float64, exclusive bool) *errors.Validation

Maximum validates if a number is smaller than a given maximum

func MaximumInt

func MaximumInt(path, in string, data, max int64, exclusive bool) *errors.Validation

MaximumInt validates if a number is smaller than a given maximum

func MaximumUint

func MaximumUint(path, in string, data, max uint64, exclusive bool) *errors.Validation

MaximumUint validates if a number is smaller than a given maximum

func MinItems

func MinItems(path, in string, size, min int64) *errors.Validation

MinItems validates that there are at least n items in a slice

func MinLength

func MinLength(path, in, data string, minLength int64) *errors.Validation

MinLength validates a string for minimum length

func Minimum

func Minimum(path, in string, data, min float64, exclusive bool) *errors.Validation

Minimum validates if a number is smaller than a given minimum

func MinimumInt

func MinimumInt(path, in string, data, min int64, exclusive bool) *errors.Validation

MinimumInt validates if a number is smaller than a given minimum

func MinimumUint

func MinimumUint(path, in string, data, min uint64, exclusive bool) *errors.Validation

MinimumUint validates if a number is smaller than a given minimum

func MultipleOf

func MultipleOf(path, in string, data, factor float64) *errors.Validation

MultipleOf validates if the provided number is a multiple of the factor

func Pattern

func Pattern(path, in, data, pattern string) *errors.Validation

Pattern validates a string against a regular expression

func Required

func Required(path, in string, data interface{}) *errors.Validation

Required validates an interface for requiredness

func RequiredNumber

func RequiredNumber(path, in string, data float64) *errors.Validation

RequiredNumber validates a number for requiredness

func RequiredString

func RequiredString(path, in, data string) *errors.Validation

RequiredString validates a string for requiredness

func Spec

func Spec(doc *loads.Document, formats strfmt.Registry) error

Spec validates a spec document It validates the spec json against the json schema for swagger and then validates a number of extra rules that can't be expressed in json schema:

  • definition can't declare a property that's already defined by one of its ancestors
  • definition's ancestor can't be a descendant of the same model
  • each api path should be non-verbatim (account for path param names) unique per method
  • each security reference should contain only unique scopes
  • each security scope in a security definition should be unique
  • each path parameter should correspond to a parameter placeholder and vice versa
  • each referencable definition must have references
  • each definition property listed in the required array must be defined in the properties of the model
  • each parameter should have a unique `name` and `type` combination
  • each operation should have only 1 parameter of type body
  • each reference must point to a valid object
  • every default value that is specified must validate against the schema for that property
  • items property is required for all schemas/definitions of type `array`

func UniqueItems

func UniqueItems(path, in string, data interface{}) *errors.Validation

UniqueItems validates that the provided slice has unique elements

Types

type Defaulter

type Defaulter interface {
	Apply()
}

type DefaulterFunc

type DefaulterFunc func()

func (DefaulterFunc) Apply

func (f DefaulterFunc) Apply()

type EntityValidator

type EntityValidator interface {
	Validate(interface{}) *Result
}

An EntityValidator is an interface for things that can validate entities

type HeaderValidator

type HeaderValidator struct {
	KnownFormats strfmt.Registry
	// contains filtered or unexported fields
}

A HeaderValidator has very limited subset of validations to apply

func NewHeaderValidator

func NewHeaderValidator(name string, header *spec.Header, formats strfmt.Registry) *HeaderValidator

NewHeaderValidator creates a new header validator object

func (*HeaderValidator) Validate

func (p *HeaderValidator) Validate(data interface{}) *Result

Validate the value of the header against its schema

type ParamValidator

type ParamValidator struct {
	KnownFormats strfmt.Registry
	// contains filtered or unexported fields
}

A ParamValidator has very limited subset of validations to apply

func NewParamValidator

func NewParamValidator(param *spec.Parameter, formats strfmt.Registry) *ParamValidator

NewParamValidator creates a new param validator object

func (*ParamValidator) Validate

func (p *ParamValidator) Validate(data interface{}) *Result

Validate the data against the description of the parameter

type Result

type Result struct {
	Errors     []error
	MatchCount int
	Defaulters []Defaulter
}

Result represents a validation result

func (*Result) AddErrors

func (r *Result) AddErrors(errors ...error)

AddErrors adds errors to this validation result

func (*Result) ApplyDefaults

func (r *Result) ApplyDefaults()

func (*Result) AsError

func (r *Result) AsError() error

AsError renders this result as an error interface

func (*Result) HasErrors

func (r *Result) HasErrors() bool

HasErrors returns true when this result is invalid

func (*Result) Inc

func (r *Result) Inc()

Inc increments the match count

func (*Result) IsValid

func (r *Result) IsValid() bool

IsValid returns true when this result is valid

func (*Result) Merge

func (r *Result) Merge(other *Result) *Result

Merge merges this result with the other one, preserving match counts etc

type SchemaValidator

type SchemaValidator struct {
	Path string

	Schema *spec.Schema

	Root         interface{}
	KnownFormats strfmt.Registry
	// contains filtered or unexported fields
}

SchemaValidator like param validator but for a full json schema

func NewSchemaValidator

func NewSchemaValidator(schema *spec.Schema, rootSchema interface{}, root string, formats strfmt.Registry) *SchemaValidator

NewSchemaValidator creates a new schema validator

func (*SchemaValidator) Applies

func (s *SchemaValidator) Applies(source interface{}, kind reflect.Kind) bool

Applies returns true when this schema validator applies

func (*SchemaValidator) SetPath

func (s *SchemaValidator) SetPath(path string)

SetPath sets the path for this schema valdiator

func (*SchemaValidator) Validate

func (s *SchemaValidator) Validate(data interface{}) *Result

Validate validates the data against the schema

type SpecValidator

type SpecValidator struct {
	KnownFormats strfmt.Registry
	// contains filtered or unexported fields
}

SpecValidator validates a swagger spec

func NewSpecValidator

func NewSpecValidator(schema *spec.Schema, formats strfmt.Registry) *SpecValidator

NewSpecValidator creates a new swagger spec validator instance

func (*SpecValidator) Validate

func (s *SpecValidator) Validate(data interface{}) (errs *Result, warnings *Result)

Validate validates the swagger spec

Jump to

Keyboard shortcuts

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