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 ¶
- type Clause
- type Condition
- func Above(field, value any) Condition
- func Absent(field any) Condition
- func All(conds ...Condition) Condition
- func Any(conds ...Condition) Condition
- func AtLeast(field, value any) Condition
- func AtMost(field, value any) Condition
- func Below(field, value any) Condition
- func Equals(field, value any) Condition
- func IsFalse(field any) Condition
- func IsTrue(field any) Condition
- func Not(cond Condition) Condition
- func NotEquals(field, value any) Condition
- func OneOf(field any, values ...any) Condition
- func Present(field any) Condition
- type Constraint
- func AtLeastOneOf(fields ...any) Constraint
- func AtMostOneOf(fields ...any) Constraint
- func ExactlyOneOf(fields ...any) Constraint
- func ForbiddenWith(field any, forbids ...any) Constraint
- func Must(require ...Condition) Constraint
- func RequiredTogether(fields ...any) Constraint
- func RequiredWith(field any, requires ...any) Constraint
- type Kind
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 (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 ¶
Above holds when field is greater than value (literal or field). A null operand makes the condition pass.
func AtLeast ¶
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 ¶
AtMost holds when field is less than or equal to value (literal or field). A null operand makes the condition pass.
func Below ¶
Below holds when field is less than value (literal or field). A null operand makes the condition pass.
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 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("...").