validate

package
v0.0.0-...-d88c8b5 Latest Latest
Warning

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

Go to latest
Published: Feb 9, 2021 License: Apache-2.0 Imports: 16 Imported by: 0

Documentation

Index

Constants

View Source
const (
	// ArrayDoesNotAllowAdditionalItemsError when an additionalItems construct is not verified by the array values provided.
	//
	// TODO: should move to package go-openapi/errors
	ArrayDoesNotAllowAdditionalItemsError = "array doesn't allow for additional items"

	// HasDependencyError indicates that a dependencies construct was not verified
	HasDependencyError = "%q has a dependency on %s"

	// InvalidTypeConversionError indicates that a numerical conversion for the given type could not be carried on
	InvalidTypeConversionError = "invalid type conversion in %s: %v "

	// MustValidateAtLeastOneSchemaError indicates that in a AnyOf construct, none of the schema constraints specified were verified
	MustValidateAtLeastOneSchemaError = "%q must validate at least one schema (anyOf)"

	// MustValidateOnlyOneSchemaError indicates that in a OneOf construct, either none of the schema constraints specified were verified, or several were
	MustValidateOnlyOneSchemaError = "%q must validate one and only one schema (oneOf). %s"

	// MustValidateAllSchemasError indicates that in a AllOf construct, at least one of the schema constraints specified were not verified
	//
	// TODO: punctuation in message
	MustValidateAllSchemasError = "%q must validate all the schemas (allOf)%s"

	// MustNotValidateSchemaError indicates that in a Not construct, the schema constraint specified was verified
	MustNotValidateSchemaError = "%q must not validate the schema (not)"
)

Error messages related to schema validation and returned as results.

Variables

View Source
var (
	// Debug is true when the SWAGGER_DEBUG env var is not empty.
	// It enables a more verbose logging of validators.
	Debug = os.Getenv("SWAGGER_DEBUG") != ""
)

Functions

func AgainstSchema

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

AgainstSchema validates the specified data against the provided schema, using a registry of supported formats.

When no pre-parsed *spec.Schema structure is provided, it uses a JSON schema as default. See example.

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 IsValueValidAgainstRange

func IsValueValidAgainstRange(val interface{}, typeName, format, prefix, path string) error

IsValueValidAgainstRange checks that a numeric value is compatible with the range defined by Type and Format, that is, may be converted without loss.

NOTE: this check is about type capacity and not formal verification such as: 1.0 != 1L

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 MaximumNativeType

func MaximumNativeType(path, in string, val interface{}, max float64, exclusive bool) *errors.Validation

MaximumNativeType provides native type constraint validation as a facade to various numeric types versions of Maximum constraint check.

Assumes that any possible loss conversion during conversion has been checked beforehand.

NOTE: currently, the max value is marshalled as a float64, no matter what, which means there may be a loss during conversions (e.g. for very large integers)

TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free

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 MinimumNativeType

func MinimumNativeType(path, in string, val interface{}, min float64, exclusive bool) *errors.Validation

MinimumNativeType provides native type constraint validation as a facade to various numeric types versions of Minimum constraint check.

Assumes that any possible loss conversion during conversion has been checked beforehand.

NOTE: currently, the min value is marshalled as a float64, no matter what, which means there may be a loss during conversions (e.g. for very large integers)

TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free

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 MultipleOfInt

func MultipleOfInt(path, in string, data int64, factor int64) *errors.Validation

MultipleOfInt validates if the provided integer is a multiple of the factor

func MultipleOfNativeType

func MultipleOfNativeType(path, in string, val interface{}, multipleOf float64) *errors.Validation

MultipleOfNativeType provides native type constraint validation as a facade to various numeric types version of MultipleOf constraint check.

Assumes that any possible loss conversion during conversion has been checked beforehand.

NOTE: currently, the multipleOf factor is marshalled as a float64, no matter what, which means there may be a loss during conversions (e.g. for very large integers)

TODO: Normally, a JSON MAX_SAFE_INTEGER check would ensure conversion remains loss-free

func MultipleOfUint

func MultipleOfUint(path, in string, data, factor uint64) *errors.Validation

MultipleOfUint validates if the provided unsigned integer 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 SetContinueOnErrors

func SetContinueOnErrors(c bool)

SetContinueOnErrors sets global default behavior regarding spec validation errors reporting.

For extended error reporting, you most likely want to set it to true. For faster validation, it's better to give up early when a spec is detected as invalid: set it to false (this is the default).

Setting this mode does NOT affect the validation status.

NOTE: this method affects global defaults. It is not suitable for a concurrent usage.

func UniqueItems

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

UniqueItems validates that the provided slice has unique elements

Types

type EntityValidator

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

An EntityValidator is an interface for things that can validate entities

type Option

type Option func(*SchemaValidatorOptions)

Option sets optional rules for schema validation

type Opts

type Opts struct {
	ContinueOnErrors bool // true: continue reporting errors, even if spec is invalid
}

Opts specifies validation options for a SpecValidator.

NOTE: other options might be needed, for example a go-swagger specific mode.

type Result

type Result struct {
	Errors     []error
	Warnings   []error
	MatchCount int
}

Result represents a validation result set, composed of errors and warnings.

It is used to keep track of all detected errors and warnings during the validation of a specification.

Matchcount is used to determine which errors are relevant in the case of AnyOf, OneOf schema validation. Results from the validation branch with most matches get eventually selected.

TODO: keep path of key originating the error

func (*Result) AddErrors

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

AddErrors adds errors to this validation result (if not already reported).

Since the same check may be passed several times while exploring the spec structure (via $ref, ...) reported messages are kept unique.

func (*Result) AddWarnings

func (r *Result) AddWarnings(warnings ...error)

AddWarnings adds warnings to this validation result (if not already reported).

func (*Result) AsError

func (r *Result) AsError() error

AsError renders this result as an error interface

TODO: reporting / pretty print with path ordered and indented

func (*Result) HasErrors

func (r *Result) HasErrors() bool

HasErrors returns true when this result is invalid.

Returns false on a nil *Result.

func (*Result) HasErrorsOrWarnings

func (r *Result) HasErrorsOrWarnings() bool

HasErrorsOrWarnings returns true when this result contains either errors or warnings.

Returns false on a nil *Result.

func (*Result) HasWarnings

func (r *Result) HasWarnings() bool

HasWarnings returns true when this result contains warnings.

Returns false on a nil *Result.

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.

Returns true on a nil *Result.

func (*Result) Merge

func (r *Result) Merge(others ...*Result) *Result

Merge merges this result with the other one(s), preserving match counts etc.

func (*Result) MergeAsErrors

func (r *Result) MergeAsErrors(others ...*Result) *Result

MergeAsErrors merges this result with the other one(s), preserving match counts etc.

Warnings from input are merged as Errors in the returned merged Result.

func (*Result) MergeAsWarnings

func (r *Result) MergeAsWarnings(others ...*Result) *Result

MergeAsWarnings merges this result with the other one(s), preserving match counts etc.

Errors from input are merged as Warnings in the returned merged Result.

type SchemaValidator

type SchemaValidator struct {
	Path string

	Schema *spec.Schema

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

SchemaValidator validates data against a JSON schema

func NewSchemaValidator

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

NewSchemaValidator creates a new schema validator.

Panics if the provided schema is invalid.

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 SchemaValidatorOptions

type SchemaValidatorOptions struct {
}

SchemaValidatorOptions defines optional rules for schema validation

func (SchemaValidatorOptions) Options

func (svo SchemaValidatorOptions) Options() []Option

Options returns current options

Notes

Bugs

  • succeededOnce is always false

  • can't get to here. Should remove dead code (commented out).

Jump to

Keyboard shortcuts

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