schema: github.com/juju/schema Index | Files

package schema

import "github.com/juju/schema"

Index

Package Files

checker.go const.go errors.go fieldmap.go lists.go maps.go numeric.go size.go strings.go time.go time_duration.go

Variables

var Omit omit

Omit is a marker for FieldMap and StructFieldMap defaults parameter. If a field is not present in the map and defaults to Omit, the missing field will be ommitted from the coerced map as well.

type Checker Uses

type Checker interface {
    Coerce(v interface{}, path []string) (newv interface{}, err error)
}

The Coerce method of the Checker interface is called recursively when v is being validated. If err is nil, newv is used as the new value at the recursion point. If err is non-nil, v is taken as invalid and may be either ignored or error out depending on where in the schema checking process the error happened. Checkers like OneOf may continue with an alternative, for instance.

func Any Uses

func Any() Checker

Any returns a Checker that succeeds with any input value and results in the value itself unprocessed.

func Bool Uses

func Bool() Checker

Bool returns a Checker that accepts boolean values only.

func Const Uses

func Const(value interface{}) Checker

Const returns a Checker that only succeeds if the input matches value exactly. The value is compared with reflect.DeepEqual.

func FieldMap Uses

func FieldMap(fields Fields, defaults Defaults) Checker

FieldMap returns a Checker that accepts a map value with defined string keys. Every key has an independent checker associated, and processing will only succeed if all the values succeed individually. If a field fails to be processed, processing stops and returns with the underlying error.

Fields in defaults will be set to the provided value if not present in the coerced map. If the default value is schema.Omit, the missing field will be omitted from the coerced map.

The coerced output value has type map[string]interface{}.

func FieldMapSet Uses

func FieldMapSet(selector string, maps []Checker) Checker

FieldMapSet returns a Checker that accepts a map value checked against one of several FieldMap checkers. The actual checker used is the first one whose checker associated with the selector field processes the map correctly. If no checker processes the selector value correctly, an error is returned.

The coerced output value has type map[string]interface{}.

func Float Uses

func Float() Checker

Float returns a Checker that accepts any float value, and returns the same value consistently typed as a float64.

func ForceInt Uses

func ForceInt() Checker

ForceInt returns a Checker that accepts any integer or float value, and returns the same value consistently typed as an int. This is required in order to handle the interface{}/float64 type conversion performed by the JSON serializer used as part of the API infrastructure.

func ForceUint Uses

func ForceUint() Checker

ForceUint returns a Checker that accepts any integer or float value, and returns the same value consistently typed as an uint64. This is required in order to handle the interface{}/float64 type conversion performed by the JSON serializer used as part of the API infrastructure. If the integer value is negative an error is raised.

func Int Uses

func Int() Checker

Int returns a Checker that accepts any integer value, and returns the same value consistently typed as an int64.

func List Uses

func List(elem Checker) Checker

List returns a Checker that accepts a slice value with values that are processed with the elem checker. If any element of the provided slice value fails to be processed, processing will stop and return with the obtained error.

The coerced output value has type []interface{}.

func Map Uses

func Map(key Checker, value Checker) Checker

Map returns a Checker that accepts a map value. Every key and value in the map are processed with the respective checker, and if any value fails to be coerced, processing stops and returns with the underlying error.

The coerced output value has type map[interface{}]interface{}.

func Nil Uses

func Nil(valueLabel string) Checker

Nil returns a Checker that only succeeds if the input is nil. To tweak the error message, valueLabel can contain a label of the value being checked to be empty, e.g. "my special name". If valueLabel is "", "value" will be used as a label instead.

Example 1: schema.Nil("widget").Coerce(42, nil) will return an error message like `expected empty widget, got int(42)`.

Example 2: schema.Nil("").Coerce("", nil) will return an error message like `expected empty value, got string("")`.

func NonEmptyString Uses

func NonEmptyString(valueLabel string) Checker

NonEmptyString returns a Checker that only accepts non-empty strings. To tweak the error message, valueLabel can contain a label of the value being checked, e.g. "my special name". If valueLabel is "", "string" will be used as a label instead.

Example 1: schema.NonEmptyString("widget").Coerce("", nil) will return an error message like `expected non-empty widget, got string("")`.

Example 2: schema.NonEmptyString("").Coerce("", nil) will return an error message like `expected non-empty string, got string("")`.

func OneOf Uses

func OneOf(options ...Checker) Checker

OneOf returns a Checker that attempts to Coerce the value with each of the provided checkers. The value returned by the first checker that succeeds will be returned by the OneOf checker itself. If no checker succeeds, OneOf will return an error on coercion.

func SimpleRegexp Uses

func SimpleRegexp() Checker

SimpleRegexp returns a checker that accepts a string value that is a valid regular expression and returns it unprocessed.

func Size Uses

func Size() Checker

Size returns a Checker that accepts a string value, and returns the parsed string as a size in mebibytes see: https://godoc.org/github.com/juju/utils#ParseSize

func StrictFieldMap Uses

func StrictFieldMap(fields Fields, defaults Defaults) Checker

StrictFieldMap returns a Checker that acts as the one returned by FieldMap, but the Checker returns an error if it encounters an unknown key.

func String Uses

func String() Checker

String returns a Checker that accepts a string value only and returns it unprocessed.

func StringMap Uses

func StringMap(value Checker) Checker

StringMap returns a Checker that accepts a map value. Every key in the map must be a string, and every value in the map are processed with the provided checker. If any value fails to be coerced, processing stops and returns with the underlying error.

The coerced output value has type map[string]interface{}.

func Stringified Uses

func Stringified(checkers ...Checker) Checker

Stringified returns a checker that accepts a bool/int/float/string value and returns its string. Other value types may be supported by passing in their checkers.

func Time Uses

func Time() Checker

Time returns a Checker that accepts a string value, and returns the parsed time.Time value. Emtpy strings are considered empty times.

func TimeDuration Uses

func TimeDuration() Checker

TimeDuration returns a Checker that accepts a string value, and returns the parsed time.Duration value. Emtpy strings are considered empty time.Duration

func URL Uses

func URL() Checker

URL returns a Checker that accepts a string value that must be parseable as a URL, and returns a *net.URL.

func UUID Uses

func UUID() Checker

UUID returns a Checker that accepts a string value only and returns it unprocessed.

func Uint Uses

func Uint() Checker

Uint returns a Checker that accepts any integer or unsigned value, and returns the same value consistently typed as an uint64. If the integer value is negative an error is raised.

type Defaults Uses

type Defaults map[string]interface{}

type Fields Uses

type Fields map[string]Checker

Package schema imports 9 packages (graph) and is imported by 718 packages. Updated 2019-08-25. Refresh now. Tools for package owners.