Documentation
¶
Index ¶
- Variables
- type Checker
- func Any() Checker
- func Bool() Checker
- func Const(value interface{}) Checker
- func FieldMap(fields Fields, defaults Defaults) Checker
- func FieldMapSet(selector string, maps []Checker) Checker
- func Float() Checker
- func ForceInt() Checker
- func ForceUint() Checker
- func Int() Checker
- func List(elem Checker) Checker
- func Map(key Checker, value Checker) Checker
- func Nil(valueLabel string) Checker
- func NonEmptyString(valueLabel string) Checker
- func OneOf(options ...Checker) Checker
- func SimpleRegexp() Checker
- func Size() Checker
- func StrictFieldMap(fields Fields, defaults Defaults) Checker
- func String() Checker
- func StringMap(value Checker) Checker
- func Stringified(checkers ...Checker) Checker
- func Time() Checker
- func TimeDuration() Checker
- func TimeDurationString() Checker
- func URL() Checker
- func UUID() Checker
- func Uint() Checker
- type Defaults
- type Fields
Constants ¶
This section is empty.
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.
Functions ¶
This section is empty.
Types ¶
type Checker ¶
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 ¶
func Any() Checker
Any returns a Checker that succeeds with any input value and results in the value itself unprocessed.
func Const ¶
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 ¶
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 ¶
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 ¶
func Float() Checker
Float returns a Checker that accepts any float value, and returns the same value consistently typed as a float64.
func ForceInt ¶
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 ¶
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 ¶
func Int() Checker
Int returns a Checker that accepts any integer value, and returns the same value consistently typed as an int64.
func List ¶
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 ¶
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 ¶
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 ¶
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 ¶
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 ¶
func SimpleRegexp() Checker
SimpleRegexp returns a checker that accepts a string value that is a valid regular expression and returns it unprocessed.
func Size ¶
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 ¶
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 ¶
func String() Checker
String returns a Checker that accepts a string value only and returns it unprocessed.
func StringMap ¶
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 ¶
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 ¶
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 ¶
func TimeDuration() Checker
TimeDuration returns a Checker that accepts a string or time.Duration value, and returns the parsed time.Duration value. Empty strings are considered empty time.Duration.
func TimeDurationString ¶ added in v1.2.0
func TimeDurationString() Checker
TimeDurationString returns a Checker that accepts a string or time.Duration value, and returns the time.Duration encoded as a string. The encoding uses the time.Duration.String() method. Empty strings are considered empty time.Duration.
func URL ¶
func URL() Checker
URL returns a Checker that accepts a string value that must be parseable as a URL, and returns a *net.URL.