Documentation
¶
Overview ¶
Package assert parses and evaluates `# @assert` expressions: `<selector> <op> <value>`, `<selector> exists` / `not exists`, a predicate such as `<selector> isInteger` or `not isEmpty`, `<selector> length <op> <n>` and `<selector> matchesSchema <file>`.
Index ¶
Constants ¶
This section is empty.
Variables ¶
var Operators = []string{"==", "!=", "<=", ">=", "<", ">", "contains", "matches", "startsWith", "endsWith", "matchesSchema", "exists", "not exists"}
Operators lists the supported comparison operators.
var Predicates = []string{"isString", "isNumber", "isInteger", "isBoolean", "isArray", "isObject", "isNull", "isEmpty"}
Predicates take no value: `body.$.id isInteger`, `body.$.items not isEmpty`. Each also has a `not` form.
Functions ¶
func ParseNumber ¶
ParseNumber reads a decimal number exactly, so large integers such as 9007199254740993 keep their value instead of rounding through float64, and values beyond float64's range (1e1000) still compare as numbers. Words, Inf, NaN, fractions like 1/2 and numbers with more than 4096 digits or an exponent beyond ±4096 are not numbers here and compare as text.
func Redact ¶
Redact keeps an assertion's selector and operator and replaces its value with mask. The selector is read the way Parse reads it, so a filter with spaces stays whole. What Parse cannot read is masked after its first two words, so a secret never shows because the expression around it was malformed.
func Schemas ¶
func Schemas(read func(path string) ([]byte, error)) func(path string) (*jsonschema.Resolved, error)
Schemas returns a Schema loader that reads each file with read, then parses and resolves it once: every later assertion naming the same file, and every retry of the request, gets the resolved schema (or the same error) without reading it again. It is not safe for concurrent use.
Types ¶
type Expr ¶
type Expr struct {
Selector string
// Op is the operator, a predicate (`isString`, `not isEmpty`) or
// `matchesSchema`, whose Value is the schema file.
Op string
Value string // raw right-hand side; may contain {{placeholders}}
// Length compares the selected value's length (characters, elements
// or keys) rather than the value: `body.$.items length == 3`.
Length bool
}
Expr is a parsed assertion.
type Options ¶
type Options struct {
// Schema returns the JSON Schema named by `matchesSchema`, read from
// a file resolved and confined the way the caller's files are; build
// it with Schemas. Nil refuses the operator.
Schema func(path string) (*jsonschema.Resolved, error)
}
Options carries what evaluation may need beyond the response.
type Result ¶
type Result struct {
Expr string `json:"expr"`
Pass bool `json:"pass"`
Actual string `json:"actual,omitempty"`
Expected string `json:"expected,omitempty"`
Error string `json:"error,omitempty"`
}
Result is the outcome of evaluating one assertion.