schema

package module
v1.2.0 Latest Latest
Warning

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

Go to latest
Published: Nov 2, 2023 License: LGPL-3.0 Imports: 9 Imported by: 272

README

juju/schema

This package provides helpers for coercing dynamically typed data structures into known forms.

Documentation

Index

Constants

This section is empty.

Variables

View Source
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

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

func Any() Checker

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

func Bool

func Bool() Checker

Bool returns a Checker that accepts boolean values only.

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

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

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

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

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

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

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

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

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

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

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

func String() Checker

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

func StringMap

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

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

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.

func UUID

func UUID() Checker

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

func Uint

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

type Defaults map[string]interface{}

type Fields

type Fields map[string]Checker

Jump to

Keyboard shortcuts

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