Documentation
¶
Overview ¶
Package validonce evaluates and builds validonce rule sets (rule format v1).
Rules are data: the same JSON that the C#, Python and TypeScript implementations export under "x-validonce". Evaluate runs them against the wire form of an object (a decoded JSON map).
Index ¶
- Constants
- func CanonicalJSON(data []byte) (string, error)
- func HashToByteSequence(hash string) (string, error)
- func ParseRulesHeader(value string) []string
- func ParseUntil(until string) (time.Time, error)
- func RuleSetHash(data []byte) (string, error)
- func RuleSetsHashes(document []byte) (map[string]string, error)
- func RulesHeaderValue(hashes ...string) (string, error)
- func RulesStatusValue(current string) (string, error)
- func ValidateRules(rules []Rule) error
- type AcceptHash
- type Builder
- type CmpBuilder
- func (c CmpBuilder) Eq(r Operand, opts ...Option) Builder
- func (c CmpBuilder) Ge(r Operand, opts ...Option) Builder
- func (c CmpBuilder) Gt(r Operand, opts ...Option) Builder
- func (c CmpBuilder) Le(r Operand, opts ...Option) Builder
- func (c CmpBuilder) Lt(r Operand, opts ...Option) Builder
- func (c CmpBuilder) Ne(r Operand, opts ...Option) Builder
- type Cond
- type Condition
- type FieldRef
- type FieldValue
- type FieldValues
- type Issue
- type Kind
- type Message
- type Operand
- type Option
- type Problem
- type Rule
- type RuleSet
- type Rules
- func (r *Rules) Check(v any) ([]Issue, error)
- func (r *Rules) CheckSide(v any, side Side) ([]Issue, error)
- func (r *Rules) Evaluate(data map[string]any, side Side) []Issue
- func (r *Rules) Model() string
- func (r *Rules) Problem(v any) error
- func (r *Rules) Rule(id string) (Rule, bool)
- func (r *Rules) Set() RuleSet
- type RulesCheck
- type RulesMode
- type RulesOutcome
- type Side
- type StaleRulesDetails
- type StaleRulesProblem
- type WhenBuilder
Constants ¶
const ( RulesHeader = "Validonce-Rules" RulesStatusHeader = "Validonce-Rules-Status" RulesCurrentHeader = "Validonce-Rules-Current" StaleRulesType = "https://schemas.metatoy.com/validonce/problems/stale-rules" )
Header names and the problem type of the 412 response.
const DefaultProblemStatus = 422
DefaultProblemStatus is the status a failing rule set is reported with.
const Version = 1
Version is the rule format version this package implements.
Variables ¶
This section is empty.
Functions ¶
func CanonicalJSON ¶ added in v0.6.0
CanonicalJSON returns the RFC 8785 form of a JSON text: keys sorted by UTF-16 code units, no whitespace, ECMAScript string and number forms. Every number is an IEEE 754 double, so 1.0 is 1 and -0 is 0.
func HashToByteSequence ¶ added in v0.6.0
HashToByteSequence writes sha256-<base64> as a Structured Field byte sequence, :<base64>:.
func ParseRulesHeader ¶ added in v0.6.0
ParseRulesHeader parses a Validonce-Rules value (an RFC 9651 List of byte sequences) into sha256-<base64> hashes. It returns nil when the value is empty or does not parse.
func ParseUntil ¶ added in v0.6.0
ParseUntil parses an accepted hash's expiry: an RFC 3339 datetime with an offset.
func RuleSetHash ¶ added in v0.6.0
RuleSetHash returns the identity of the rule set in data (the x-validonce JSON). The rule set must be valid (it is parsed first); it is hashed exactly as given, so hash the value as it is published.
func RuleSetsHashes ¶ added in v0.6.0
RuleSetsHashes returns the hash of every rule set in an OpenAPI 3.x document, keyed by the name of the component schema that carries it.
func RulesHeaderValue ¶ added in v0.6.0
RulesHeaderValue is the Validonce-Rules value a client sends.
func RulesStatusValue ¶ added in v0.6.0
RulesStatusValue is the Validonce-Rules-Status value for a stale request.
func ValidateRules ¶
ValidateRules checks the structure of rules (kind-specific members, ops, types, conditions).
Types ¶
type AcceptHash ¶ added in v0.6.0
AcceptHash is an extra hash treated as current (grace after a deploy), until Until when it is set.
type Builder ¶
type Builder interface {
// contains filtered or unexported methods
}
Builder is one rule under construction. Create them with When, Cmp, AtLeastOneOf and friends.
func AtLeastOneOf ¶
AtLeastOneOf fails when every field is blank.
func ExactlyOneOf ¶
ExactlyOneOf fails unless exactly one field is not blank.
type CmpBuilder ¶
type CmpBuilder struct {
// contains filtered or unexported fields
}
CmpBuilder is the first half of a compare rule.
func (CmpBuilder) Ge ¶
func (c CmpBuilder) Ge(r Operand, opts ...Option) Builder
Ge is "greater than or equal".
func (CmpBuilder) Gt ¶
func (c CmpBuilder) Gt(r Operand, opts ...Option) Builder
Gt is "greater than".
func (CmpBuilder) Le ¶
func (c CmpBuilder) Le(r Operand, opts ...Option) Builder
Le is "less than or equal".
type Cond ¶
type Cond struct {
// contains filtered or unexported fields
}
Cond is a condition under construction.
type Condition ¶
type Condition struct {
Eq *FieldValue `json:"eq,omitempty"`
Ne *FieldValue `json:"ne,omitempty"`
In *FieldValues `json:"in,omitempty"`
Present *FieldRef `json:"present,omitempty"`
Absent *FieldRef `json:"absent,omitempty"`
All []Condition `json:"all,omitempty"`
Any []Condition `json:"any,omitempty"`
Not *Condition `json:"not,omitempty"`
}
Condition is a condition tree; exactly one member is set.
type FieldValue ¶
FieldValue is the {field, value} pair used by eq, ne and in conditions.
type FieldValues ¶
FieldValues is the {field, values} pair used by "in".
type Issue ¶
type Issue struct {
Path []string `json:"path"`
RuleID string `json:"ruleId"`
Key string `json:"key"`
Params map[string]any `json:"params"`
}
Issue is one failing rule, in the shape every implementation shares.
type Operand ¶
Operand is one side of a compare: a field reference or a constant.
func (Operand) MarshalJSON ¶
MarshalJSON writes {"field": ...} or {"value": ...}.
func (*Operand) UnmarshalJSON ¶
UnmarshalJSON reads {"field": ...} or {"value": ...}.
type Option ¶
type Option func(*draft)
Option customises a rule.
func As ¶
As sets the compare type ("number", "string", "date" or "datetime") when it cannot be inferred from the Go field type.
type Problem ¶
type Problem struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Detail string `json:"detail,omitempty"`
Validonce []Issue `json:"validonce"`
}
Problem is the RFC 9457 problem details body validonce servers return when rules fail: the issue list sits in a top-level member named "validonce" (see "Transport" in rule-format-v1). It is also an error, so a handler can return it directly.
func NewProblem ¶
NewProblem builds the problem for issues with the given status (0 means 422).
func (*Problem) ContentType ¶
ContentType maps a JSON content type to application/problem+json, as problem details require.
type Rule ¶
type Rule struct {
ID string `json:"id"`
Kind Kind `json:"kind"`
Path string `json:"path,omitempty"`
Field string `json:"field,omitempty"`
When *Condition `json:"when,omitempty"`
Left *Operand `json:"left,omitempty"`
Op string `json:"op,omitempty"`
Right *Operand `json:"right,omitempty"`
Type string `json:"type,omitempty"`
Fields []string `json:"fields,omitempty"`
Message Message `json:"message"`
ServerOnly bool `json:"server_only,omitempty"`
}
Rule is one rule of any kind. Only the members of its kind are used.
type RuleSet ¶
RuleSet is the value of "x-validonce".
func ParseRuleSet ¶
ParseRuleSet decodes and validates a rule set. Unknown versions, kinds and malformed rules are rejected, never skipped.
type Rules ¶
type Rules struct {
// contains filtered or unexported fields
}
Rules is a validated rule set bound to one Go struct type. Field names in the rules are the JSON names of the struct's fields (its `json` tags), the wire names the format uses.
func Build ¶
Build turns builders into a validated Rules for struct type T. It checks, by reflection, that every field a rule names is a JSON field of T, that compare types are supported and match the Go field types, and that rule ids are unique. Call it once at startup; every problem is reported together.
func (*Rules) Check ¶
Check marshals v to its wire form with encoding/json and evaluates the rules on the server side.
func (*Rules) Evaluate ¶
Evaluate runs the rules on the wire form of an object (a decoded JSON map).
func (*Rules) Problem ¶
Problem evaluates r on v (server side) and returns a *Problem when any rule fails, else nil. A value that cannot be encoded is returned as an ordinary error.
type RulesCheck ¶ added in v0.6.0
type RulesCheck struct {
Outcome RulesOutcome
Current string
Submitted []string
}
RulesCheck is the result of CheckRules.
func CheckRules ¶ added in v0.6.0
func CheckRules(header, current string, accept []AcceptHash, now time.Time) RulesCheck
CheckRules decides whether a request's rules are current. header is the Validonce-Rules value ("" when missing) and current the server's hash.
type RulesMode ¶ added in v0.6.0
type RulesMode string
RulesMode says how a server treats the Validonce-Rules header.
type RulesOutcome ¶ added in v0.6.0
type RulesOutcome string
RulesOutcome is the decision for one request.
const ( RulesAbsent RulesOutcome = "absent" RulesCurrent RulesOutcome = "current" RulesStale RulesOutcome = "stale" )
Absent: no usable header. Current: a submitted hash is current or accepted. Stale: none is.
type Side ¶
type Side int
Side selects which rules run: the server runs every rule, a client skips server_only rules.
type StaleRulesDetails ¶ added in v0.6.0
type StaleRulesDetails struct {
Current string `json:"current"`
Submitted []string `json:"submitted"`
}
StaleRulesDetails is the validonce member of the 412 body.
type StaleRulesProblem ¶ added in v0.6.0
type StaleRulesProblem struct {
Type string `json:"type"`
Title string `json:"title"`
Status int `json:"status"`
Validonce StaleRulesDetails `json:"validonce"`
Rules *RuleSet `json:"rules,omitempty"`
OpenAPI string `json:"openapi,omitempty"`
}
StaleRulesProblem is the 412 body (application/problem+json) for a stale request in enforce mode.
func NewStaleRulesProblem ¶ added in v0.6.0
func NewStaleRulesProblem(check RulesCheck) StaleRulesProblem
NewStaleRulesProblem builds the 412 body for a check.
type WhenBuilder ¶
type WhenBuilder struct {
// contains filtered or unexported fields
}
WhenBuilder is the first half of a required_when rule.
Source Files
¶
Directories
¶
| Path | Synopsis |
|---|---|
|
Package humaonce connects validonce rules to Huma (github.com/danielgtaylor/huma/v2): it adds x-validonce to a type's OpenAPI component schema and turns failing rules into a 422 RFC 9457 problem details response with a top-level "validonce" member.
|
Package humaonce connects validonce rules to Huma (github.com/danielgtaylor/huma/v2): it adds x-validonce to a type's OpenAPI component schema and turns failing rules into a 422 RFC 9457 problem details response with a top-level "validonce" member. |
|
Package playground runs validonce rules inside go-playground/validator, so they execute with the validation an application already has (validate.Struct, Echo, Gin, ...).
|
Package playground runs validonce rules inside go-playground/validator, so they execute with the validation an application already has (validate.Struct, Echo, Gin, ...). |