Documentation
¶
Overview ¶
Package validate is the runtime that code generated by kanna-validate calls, and what the code inspecting its result works with.
A generated Validate method checks each field with plain Go and collects what broke into Errors: every broken rule rather than the first, each under the path its JSON names spell. The codes and params follow Rails' ActiveModel::Validations (blank, too_long with a count, ...), and Errors encodes as Rails encodes errors.details.
The generator itself is the command in cmd/kanna-validate.
Index ¶
- Constants
- func DecodeJSON(r io.Reader, v any) error
- func Index(field string, i int) string
- func Key[K comparable](field string, key K) string
- type Coded
- type Error
- func Blank() *Error
- func Confirmation(attribute string) *Error
- func EqualTo[N number](count N) *Error
- func Fail(code string) *Error
- func FailWith(code string, params map[string]any) *Error
- func GreaterThan[N number](count N) *Error
- func GreaterThanOrEqualTo[N number](count N) *Error
- func Inclusion() *Error
- func Invalid() *Error
- func LessThan[N number](count N) *Error
- func LessThanOrEqualTo[N number](count N) *Error
- func OtherThan[N number](count N) *Error
- func TooLong(count int) *Error
- func TooShort(count int) *Error
- func WrongLength(count int) *Error
- type Errors
- type FieldError
Constants ¶
const Base = "base"
Base is the field of an error that concerns the struct as a whole rather than one of its fields, as in Rails.
Variables ¶
This section is empty.
Functions ¶
func DecodeJSON ¶
DecodeJSON decodes the JSON value r holds into v, which must be a non-nil pointer, and reports everything wrong with it at once.
A value whose type rejects it with a Coded error, as the value types of kanna-value do, does not stop the decoding: the error is recorded at the value's path and decoding goes on. The rejection may come from any method encoding/json/v2 decodes a type with: UnmarshalJSONFrom, UnmarshalJSON, or UnmarshalText. Then, when v has a Validate method, what it finds is added, except at and under the paths decoding rejected. Those values were never set, and checking them would only report the absence of a value that was in fact sent.
The result is the Errors of both, or nil. Anything else fails with encoding/json/v2's own error: JSON that is malformed, holds more than one value, or does not fit the Go types, and a rejection that is not Coded or that falls on a map key. Such a request cannot be read, rather than breaking a rule.
Decoding follows encoding/json/v2, which, unlike encoding/json, matches names case-sensitively and rejects duplicate names.
func Key ¶
func Key[K comparable](field string, key K) string
Key returns the path to the value under key in the map at field, such as "labels.primary": a map is a JSON object, so its keys are spelled as the names of an object are, as DecodeJSON reports them.
Types ¶
type Coded ¶
Coded is an error that names the rule a value broke.
The code is stable enough to key a translated message or an API response, and the params carry what such a message needs. *rule.Error from github.com/go-kanna/value/rule is Coded too, which is how a rule held in a value type and one checked by a tag end up in the same Errors without either package importing the other.
type Error ¶
type Error struct {
// contains filtered or unexported fields
}
Error is the Coded error the built-in rules report. A validate hook reports its own through Fail and FailWith.
Error is used through a pointer: its params make it incomparable, so comparing two Error values with == would panic, while pointers compare by identity.
func Confirmation ¶
Confirmation reports a value that does not match the field it confirms, whose JSON name is attribute.
func FailWith ¶
FailWith is Fail with params for the message. params is copied, so the caller may go on to change it.
func GreaterThan ¶
func GreaterThan[N number](count N) *Error
GreaterThan reports a number that is not greater than count.
func GreaterThanOrEqualTo ¶
func GreaterThanOrEqualTo[N number](count N) *Error
GreaterThanOrEqualTo reports a number below count.
func LessThan ¶
func LessThan[N number](count N) *Error
LessThan reports a number that is not less than count.
func LessThanOrEqualTo ¶
func LessThanOrEqualTo[N number](count N) *Error
LessThanOrEqualTo reports a number above count.
func OtherThan ¶
func OtherThan[N number](count N) *Error
OtherThan reports a number equal to count.
func WrongLength ¶
WrongLength reports a length other than count.
type Errors ¶
type Errors []FieldError
Errors collects every rule broken while validating a value, in the order they were found. A generated Validate method returns it through Err.
func (*Errors) Add ¶
Add records err as broken on field. A nil err is ignored, so that the result of a rule can be passed as it is.
func (Errors) Err ¶
Err returns e, or nil when nothing broke. Returning e itself would hand the caller a non-nil error holding nothing.
func (Errors) Error ¶
Error lists every broken rule, such as "validate: name: blank; tags[2]: too_long (count=20)".
func (Errors) MarshalJSON ¶
MarshalJSON is MarshalJSONTo for an encoder that knows only json.Marshaler. encoding/json itself calls MarshalJSONTo.
func (Errors) MarshalJSONTo ¶
MarshalJSONTo encodes e as Rails encodes errors.details: an object keyed by field, in the order the fields first broke, each holding a list with the code under "error" and the params beside it, in key order.
{"name":[{"error":"blank"}],"tags[2]":[{"error":"too_long","count":20}]}
A param named "error" would hide the code, so it is left out.
func (*Errors) Nest ¶
Nest records what validating the value of field reported. The fields of Errors move under field ("address" and "zip" make "address.zip"), and an empty field leaves them where they are, as for an embedded struct. Any other error is recorded on field itself, or on Base when field is empty. A nil err is ignored.
type FieldError ¶
type FieldError struct {
// Field is the path to the field, spelled with JSON names, such as
// "address.zip" or "tags[2]"; or Base.
Field string
// Err is the rule's error. It is usually Coded; one that is not reports the
// code "invalid", Rails' catch-all.
Err error
}
FieldError is a broken rule together with the field it broke on.
func (FieldError) Code ¶
func (e FieldError) Code() string
Code returns the code of Err, or "invalid" when Err carries none.
func (FieldError) Error ¶
func (e FieldError) Error() string
Error formats the field followed by the code and params, such as "tags[2]: too_long (count=20)". An error that is not Coded contributes its own text instead.
func (FieldError) Params ¶
func (e FieldError) Params() map[string]any
Params returns the params of Err, or nil when Err carries none.
func (FieldError) Path ¶ added in v0.0.5
func (e FieldError) Path() string
Path returns Field. It is the method go-kanna/i18n renders an error by, together with Code and Params, to name the field in a message.
Directories
¶
| Path | Synopsis |
|---|---|
|
cmd
|
|
|
kanna-validate
command
Command kanna-validate generates Validate methods from the validate tags of structs marked //kanna:validate.
|
Command kanna-validate generates Validate methods from the validate tags of structs marked //kanna:validate. |
|
internal
|
|
|
gen
Package gen generates Validate methods from the validate tags of structs marked //kanna:validate.
|
Package gen generates Validate methods from the validate tags of structs marked //kanna:validate. |