constraint

package
v0.13.2 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 0 Imported by: 4 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package constraint lets a Go library type declare cross-field constraints on its inputs in a type-safe, string-free way. A type lists them from a Constraints method, referring to its own fields directly:

func (v CertInput) Constraints() []constraint.Constraint {
	return []constraint.Constraint{
		constraint.ExactlyOneOf(v.SelfSigned, v.AcmArn, v.PemBundle),
		constraint.When(constraint.Equals(v.Tier, "prod")).
			Require(constraint.Present(v.ValidityDays)).
			Message("production certs need an explicit validity"),
	}
}

Because the fields are real struct fields, the Go compiler checks that they exist and a rename updates them, so there are no stale field-name strings. unobin reads these declarations from source at compile time (goschema) and resolves each field reference to its kebab input name, merging them with any constraints written in UB. The methods are not evaluated at runtime, so a returned value carries the rule's kind and message but not its field names, which the compiler supplies from source. The declarative vocabulary matches the one a UB constraints block uses.

Index

Constants

View Source
const (
	FormatRegex            = "regex"
	FormatIP               = "ip"
	FormatCIDR             = "cidr"
	FormatBase64           = "base64"
	FormatPositiveDuration = "positive-duration"
	FormatDateTime         = "date-time"
	FormatJSON             = "json"
	FormatInt64            = "int64"
)

Formats accepted by IsFormat.

Variables

This section is empty.

Functions

func ByteLength added in v0.13.0

func ByteLength(value any) any

ByteLength counts bytes in a string or byte value.

func CountWhere added in v0.13.0

func CountWhere[T any](collection any, predicate func(T) Condition) any

CountWhere counts the list elements or map entries that satisfy a condition.

func Length added in v0.13.0

func Length(value any) any

Length counts Unicode code points, list elements, or map entries.

func Lookup added in v0.13.0

func Lookup(collection, key any) any

Lookup reads a list index or map key. A missing element yields null.

func Project added in v0.13.0

func Project[T any](collection any, projection func(T) any) any

Project evaluates a projection for each list element or map entry.

func Remainder added in v0.13.0

func Remainder(value, divisor any) any

Remainder gives the integer remainder. A null operand yields null.

func Sum added in v0.13.0

func Sum(collection any) any

Sum adds the numbers in a list.

Types

type Binding added in v0.13.0

type Binding struct{}

Binding names a field available to an Expr predicate.

func Bind added in v0.13.0

func Bind(name string, field any) Binding

Bind makes a Go selector available as input.name within an Expr predicate.

type Clause

type Clause struct{}

Clause is a predicate whose condition has been stated with When; call Require on it to give the requirement that must hold when the condition does.

func When

func When(cond Condition) Clause

When begins a conditional predicate. Pair it with Require.

func (Clause) Require

func (Clause) Require(require ...Condition) Constraint

Require completes a When predicate: when the When condition holds, every require condition must hold too.

type Condition

type Condition struct{}

Condition is a boolean over a type's inputs, used to build predicates. Like Constraint it is opaque and read from source, not evaluated here.

func Above

func Above(field, value any) Condition

Above holds when field is greater than value (literal or field). A null operand makes the condition pass.

func Absent

func Absent(field any) Condition

Absent holds when field is unset (null).

func All

func All(conds ...Condition) Condition

All holds when every condition holds.

func Any

func Any(conds ...Condition) Condition

Any holds when at least one condition holds.

func AtLeast

func AtLeast(field, value any) Condition

AtLeast holds when field is greater than or equal to value, which may be a literal or another field. A null operand makes the condition pass.

func AtMost

func AtMost(field, value any) Condition

AtMost holds when field is less than or equal to value (literal or field). A null operand makes the condition pass.

func Below

func Below(field, value any) Condition

Below holds when field is less than value (literal or field). A null operand makes the condition pass.

func Equals

func Equals(field, value any) Condition

Equals holds when field equals value.

func Expr added in v0.13.0

func Expr(source string, bindings ...Binding) Condition

Expr declares an Unobin Boolean expression over explicitly bound input fields.

func HasItem added in v0.13.0

func HasItem(collection, value any) Condition

HasItem tests whether a list contains a scalar value.

func HasKey added in v0.13.0

func HasKey(collection, key any) Condition

HasKey tests whether a map contains a key, including keys with null values.

func IsFalse

func IsFalse(field any) Condition

IsFalse holds when the boolean field is false.

func IsFormat added in v0.13.0

func IsFormat(value any, format string) Condition

IsFormat checks the syntax of a string according to a named format. Null passes. Use a Format constant to select the format.

func IsMatch added in v0.13.0

func IsMatch(value any, pattern string) Condition

IsMatch tests a string against a Go regular expression. Null passes.

func IsTrue

func IsTrue(field any) Condition

IsTrue holds when the boolean field is true.

func IsUnique added in v0.13.0

func IsUnique(collection any) Condition

IsUnique tests whether the scalar values in a list are distinct.

func MaxItems added in v0.6.0

func MaxItems(field any, n int) Condition

MaxItems holds when field has at most n elements. A null field passes, since presence is Present's job.

func MinItems added in v0.6.0

func MinItems(field any, n int) Condition

MinItems holds when field has at least n elements. A null field passes, since presence is Present's job.

func Not

func Not(cond Condition) Condition

Not holds when the condition does not hold.

func NotEmpty added in v0.6.0

func NotEmpty(field any) Condition

NotEmpty holds when field is set and has at least one element, so an explicitly empty list fails it. Strings and maps count the same way.

func NotEquals

func NotEquals(field, value any) Condition

NotEquals holds when field does not equal value.

func OneOf

func OneOf(field any, values ...any) Condition

OneOf holds when field's value is one of the given values.

func Present

func Present(field any) Condition

Present holds when field is set (not null).

type Constraint

type Constraint struct {
	// contains filtered or unexported fields
}

Constraint is one declared rule, built by one of the constructors below. It is opaque: an author builds it and returns it; unobin reads the rule from source rather than from this value.

func AtLeastOneOf

func AtLeastOneOf(fields ...any) Constraint

AtLeastOneOf requires that at least one of the fields is set.

func AtMostOneOf

func AtMostOneOf(fields ...any) Constraint

AtMostOneOf requires that no more than one of the fields is set.

func ExactlyOneOf

func ExactlyOneOf(fields ...any) Constraint

ExactlyOneOf requires that exactly one of the fields is set.

func ForEach added in v0.6.0

func ForEach[T any](collection any, body func(T) []Constraint) Constraint

ForEach applies per-element rules to a list or map field, mirroring the @for-each a UB constraint uses. For lists the callback takes the element type; for maps it takes MapEntry[T], where T is the map's value type. Plain, named, and pointer collections are supported. Null collections have no entries. Map iteration supports predicates, including inside nested iterations; set constraints require lists of structs.

As with every constructor in this package, ForEach is read from source and never called: the body declares rules and must be a single return of a constraint list.

constraint.ForEach(v.Replicas, func(r Replica) []constraint.Constraint {
	return []constraint.Constraint{
		constraint.ExactlyOneOf(r.Inline, r.FromFile),
		constraint.RequiredWith(r.TLS, v.CACert),
	}
})

func ForbiddenWith

func ForbiddenWith(field any, forbids ...any) Constraint

ForbiddenWith requires that when field is set, no forbids field is set.

func Must

func Must(require ...Condition) Constraint

Must is an unconditional predicate: every condition must hold.

func RequiredTogether

func RequiredTogether(fields ...any) Constraint

RequiredTogether requires that the fields are all set or all unset.

func RequiredWith

func RequiredWith(field any, requires ...any) Constraint

RequiredWith requires that when field is set, every requires field is set.

func (Constraint) Message

func (c Constraint) Message(text string) Constraint

Message attaches a custom failure message and returns the updated rule, for chaining after a predicate: When(...).Require(...).Message("...").

type Kind

type Kind int

Kind classifies a constraint. The set kinds (everything but predicate) are about which fields are set; a predicate is a when/require condition.

const (
	KindExactlyOneOf Kind = iota + 1
	KindAtLeastOneOf
	KindAtMostOneOf
	KindRequiredTogether
	KindRequiredWith
	KindForbiddenWith
	KindPredicate
	KindForEach
)

type MapEntry added in v0.13.0

type MapEntry[T any] struct {
	Key   string
	Value T
}

MapEntry is the callback parameter for ForEach over a map. Key is the string key and Value has the map's value type.

Jump to

Keyboard shortcuts

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