rule

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Nov 27, 2026 License: MIT Imports: 7 Imported by: 0 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 rule is what a validate tag means in a spec. The endpoint DSL and the behavior DSL both read a field's validate tag through it, so each one puts the same constraints in its spec, and each refuses a rule that the spec cannot show.

Index

Constants

View Source
const (
	Requires = "Each input field has a validate tag: its rules, or omitempty for an optional field with none. " +
		"The spec carries the whole tag, and the server answers 400 to a value that breaks it."
	Fix = `Add validate:"..." to the field, such as validate:"required,min=1,max=255", ` +
		`or validate:"omitempty" for an optional field with no other rule.`
)

Requires and Fix are the check that each DSL runs on its inputs, in the same words, so endpoints and behaviors hold the same rule.

View Source
const (
	TagName = `validate:"..."`
	TagDoes = "The rules the server checks before the method runs: a value that breaks one is a 400 " +
		`with {"error": "..."}. required makes the field required, enum gives the enum of a type ` +
		"with Values and Valid, such as types.ContentType, oneof an enum on a field with no such " +
		"type, min, max and " +
		"len the length of a string, the items of an array or the bounds of a number, startswith a " +
		"pattern, and uuid, email, hostname, ip, ipv4, ipv6, cidr, url and uri a format. The spec " +
		"carries these, and the whole tag in x-oapi-codegen-extra-tags. A custom rule, such as " +
		"resource_name, needs its schema from rule.Register; a rule with none fails the spec."
	TagWhen = `On each input field. A field with no limit is validate:"omitempty"; validate refuses ` +
		"an input field with no tag."

	FailsRequires = "Each rule of an input field has a .Fails input that breaks it, which the API " +
		"refuses with 400. An endpoint's required needs one, with the field empty; a behavior's " +
		"needs none, as its generated tests leave the field out. A path field's required, and a min " +
		"of 1 on a string that is required, optional or in the path, need none: their only bad " +
		"value is empty."
	FailsWhen = "For each rule of each input field, as validate requires, and for each input " +
		"that the method refuses for another reason."
	FailsFix = `Add .Fails("<what is wrong>", <Input>{...}) to the method, with the other fields valid ` +
		`and the field's value breaking the rule that the message names, such as a name of 256 ` +
		`characters for max=255.`

	RegisterName = `rule.Register("tag", rule.Pattern(re))`
	RegisterDoes = "Declares what a custom validate rule means in the spec: rule.Pattern(re), " +
		"rule.Format(f), or rule.None for a rule that the spec cannot show, such as a target the " +
		"controller resolves."
	RegisterWhen = "Beside the validator of a new custom rule, in internal/validation."
)

The words of the validate tag and of Register in the spec of each DSL, osapi dsl endpoint spec get and osapi dsl behavior spec get, so an author of either reads the same rule.

View Source
const Enum = "enum"

Enum is the validate rule of a field whose type is a closed set: a defined string type with Values and Valid, such as types.ContentType. The server calls Valid, and the spec has the values of Values as the enum.

View Source
const Extension = "x-oapi-codegen-extra-tags"

Extension is the OpenAPI extension that carries a field's whole validate tag. oapi-codegen copies it onto the generated field, so a Go client made from the spec validates as the server does.

Variables

This section is empty.

Functions

func Apply

func Apply(
	tag string,
	typ reflect.Type,
	t Target,
) (bool, error)

Apply sets the constraints of a validate tag on t, the schema of a field of type typ, and reports whether the field is required. A rule with no schema is an error, so the spec never checks less than the server does. enum is the rule of a type with Values, and oneof of a field with no such type, so a closed set has one list: the type's.

func Declare

func Declare(
	c Custom,
)

Declare registers a custom rule: its schema here, and its check with the validator through OnDeclare.

func Format

func Format(
	format string,
) func(t Target)

Format is a schema that sets a JSON Schema format.

func Has

func Has(
	tag string,
	name string,
) bool

Has reports whether the validate tag has the rule.

func None

func None(Target)

None is the schema of a rule that the spec cannot show more of than the field's type, such as a target that the controller resolves.

func OnDeclare

func OnDeclare(
	fn func(c Custom),
)

OnDeclare has fn called for every custom rule: those declared already, then each one after. The validation package calls it once, to register each rule with the validator the server uses, so a rule can be declared before or after that package starts.

func Options

func Options(
	tag string,
	typ reflect.Type,
) []string

Options are the values that a field of type typ with the validate tag may have, as the spec's enum has them: the Values of a closed set with enum, or the values of oneof. They are nil for a field with neither, or with a tag that Apply refuses. The rules after dive are the items', so a list's options are those of each item.

func Pattern

func Pattern(
	re string,
) func(t Target)

Pattern is a schema that sets a regular expression.

func Register

func Register(
	tag string,
	apply func(t Target),
)

Register declares what a custom validate tag means in a spec. A custom validator is a Go function, and nothing can read a schema out of it, so its schema is registered beside it:

rule.Register("resource_name", rule.Pattern(`^[a-zA-Z0-9_-]+$`))

A rule that the spec cannot show more of than its type registers rule.None. Apply refuses a tag that is neither standard nor registered.

func Untagged

func Untagged(
	t reflect.Type,
) []string

Untagged is the name of each input field of t that has no validate tag: an exported field that JSON reads, and each field of a struct that is the whole body (in:"body").

func Values

func Values(
	typ reflect.Type,
) ([]string, bool)

Values are the values of typ, a closed set, as strings: a defined string type with a method Values that gives each value of the type and a method Valid. ok is false for each other type.

Types

type Custom

type Custom struct {
	// Tag is the name used in validate tags, such as resource_name.
	Tag string
	// Check reports whether a field's value, as a string, passes.
	Check func(value string) bool
	// Schema is what the tag means in the spec.
	Schema func(t Target)
	// Message follows the failing value in the error, such as "is not a
	// valid sysctl key".
	Message string
}

Custom is a validate tag that an author declares: the check the server runs, what the tag means in the spec, and what a caller is told when a value fails it.

type Need

type Need struct {
	// Field is the Go path of the field below the input, such as "Name" or
	// "Body.Target".
	Field string
	// Rule is the rule as the tag has it, such as "max=255".
	Rule string
}

Need is a rule of an input field that a method must show it refuses, with an input that breaks it.

func Needs

func Needs(
	t reflect.Type,
	withRequired bool,
) []Need

Needs is each rule of t's input fields that needs a bad input: each rule but omitempty and dive. A min of 1 or less on a string that is optional, in the path or required is not one: its only bad value is empty, which the tag lets through, the route cannot carry, or required refuses first. A path field's required is not one either: the route cannot carry an empty key. Required is one only when withRequired: the behavior DSL's generated tests leave each required field out, and the endpoint DSL has no such tests.

func (Need) Key

func (n Need) Key() string

Key is the need as validation.Breaks reports a broken rule.

type Target

type Target interface {
	// Enum sets the values the field may have.
	Enum(values []string)
	// Pattern sets the regular expression the value matches.
	Pattern(re string)
	// Format sets the JSON Schema format of the value.
	Format(format string)
	// Bound sets min, max or len: the length of a string, the items of an
	// array, or the value of a number.
	Bound(rule string, n float64)
	// Items is the target of each item of an array, which the rules after
	// dive apply to.
	Items() Target
}

Target is a schema that takes the constraints of a validate tag.

Jump to

Keyboard shortcuts

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