feat(api): add invitation, terms version, acceptance, and contact grant contract
This commit is contained in:
1 parent
b856d4ff54
commit
d2c6f1375a
140 files changed
+39767
-303
No files matched your search
@@ -3,11 +3,13 @@ package problem
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
"regexp"
|
||||
"strings"
|
||||
|
||||
faster "github.com/go-faster/errors"
|
||||
ht "github.com/ogen-go/ogen/http"
|
||||
"github.com/ogen-go/ogen/ogenerrors"
|
||||
"github.com/ogen-go/ogen/openapi"
|
||||
@@ -32,6 +34,18 @@ const (
|
||||
wholeBodyPointer = ""
|
||||
)
|
||||
|
||||
// decodeFieldMessage matches the message of the wrapper that generated
|
||||
// decoders put around the error of one object field, for example
|
||||
// `decode field "terms"`. The name is the JSON property name, which in this
|
||||
// contract never contains a quote or a backslash.
|
||||
var decodeFieldMessage = regexp.MustCompile(`^decode field "([^"\\]+)"$`)
|
||||
|
||||
// arrayElementMessage is the message of the wrapper that jx puts around the
|
||||
// error of a callback. Directly under a field wrapper it marks an array
|
||||
// element, because an object value is always wrapped first by its own
|
||||
// `decode <Type>` message.
|
||||
const arrayElementMessage = "callback"
|
||||
|
||||
// arrayIndexName matches the "[index]" names that generated validators use
|
||||
// for array items.
|
||||
var arrayIndexName = regexp.MustCompile(`^\[(\d+)\]$`)
|
||||
@@ -184,12 +198,7 @@ func bodyFieldErrors(err error) []api.ProblemFieldError {
|
||||
|
||||
var fields []api.ProblemFieldError
|
||||
if decodeErr, ok := errors.AsType[*ogenerrors.DecodeBodyError](err); ok {
|
||||
// Only a validation error that is the direct decoding cause names
|
||||
// top-level fields. A wrapped one comes from a nested object whose
|
||||
// path the decoder does not record, so it maps to the whole body.
|
||||
if topLevel, ok := decodeErr.Err.(*validate.Error); ok { //nolint:errorlint // the unwrapped cause alone identifies top-level fields
|
||||
fields = appendSchemaFieldErrors(fields, topLevel, wholeBodyPointer)
|
||||
}
|
||||
fields = decodeFieldErrors(decodeErr.Err)
|
||||
} else if validationErr, ok := errors.AsType[*validate.Error](err); ok {
|
||||
// Schema validation after decoding nests field errors by path.
|
||||
fields = appendSchemaFieldErrors(fields, validationErr, wholeBodyPointer)
|
||||
@@ -200,6 +209,71 @@ func bodyFieldErrors(err error) []api.ProblemFieldError {
|
||||
return fields
|
||||
}
|
||||
|
||||
// decodeFieldErrors maps an error of the generated JSON decoder to field
|
||||
// errors. The decoder records no path of its own; it wraps the error of each
|
||||
// object field with `decode field "<name>"` through go-faster/errors. The
|
||||
// pointer is rebuilt from those wrappers, read through the typed
|
||||
// errors.Formatter of each link rather than from the joined error text:
|
||||
// - a *validate.Error lists the missing required fields of the object at the
|
||||
// pointer, which become `required` with their own pointer;
|
||||
// - an array element has no index in the chain, so the pointer stops at the
|
||||
// array with `invalid`;
|
||||
// - any other failure, such as a wrong JSON type, an unknown property, an
|
||||
// unknown oneOf variant, or a malformed value, is `invalid` at the pointer
|
||||
// of the value whose decoding failed;
|
||||
// - a link that is neither a go-faster wrapper nor a leaf ends the walk with
|
||||
// `invalid` at the pointer reached so far, which is the whole body when no
|
||||
// field wrapper was recognized.
|
||||
func decodeFieldErrors(err error) []api.ProblemFieldError {
|
||||
pointer := wholeBodyPointer
|
||||
afterField := false
|
||||
for err != nil {
|
||||
if validationErr, ok := err.(*validate.Error); ok { //nolint:errorlint // the walk inspects each link of the chain itself
|
||||
return appendSchemaFieldErrors(nil, validationErr, pointer)
|
||||
}
|
||||
formatter, ok := err.(faster.Formatter) //nolint:errorlint // the walk inspects each link of the chain itself
|
||||
if !ok {
|
||||
break
|
||||
}
|
||||
message, next := linkMessage(formatter)
|
||||
switch match := decodeFieldMessage.FindStringSubmatch(message); {
|
||||
case match != nil:
|
||||
pointer += "/" + escapePointerToken(match[1])
|
||||
afterField = true
|
||||
case message == arrayElementMessage && afterField:
|
||||
return []api.ProblemFieldError{fieldError(api.ProblemFieldLocationBody, pointer, fieldCodeInvalid)}
|
||||
default:
|
||||
afterField = false
|
||||
}
|
||||
err = next
|
||||
}
|
||||
return []api.ProblemFieldError{fieldError(api.ProblemFieldLocationBody, pointer, fieldCodeInvalid)}
|
||||
}
|
||||
|
||||
// linkMessage returns the message of one link of a go-faster/errors chain
|
||||
// without the messages of the links it wraps, and the next link.
|
||||
func linkMessage(link faster.Formatter) (string, error) {
|
||||
printer := &messagePrinter{}
|
||||
next := link.FormatError(printer)
|
||||
return printer.message.String(), next
|
||||
}
|
||||
|
||||
// messagePrinter collects the message of one error link. It never asks for
|
||||
// details, so no stack frame is printed.
|
||||
type messagePrinter struct {
|
||||
message strings.Builder
|
||||
}
|
||||
|
||||
func (printer *messagePrinter) Print(args ...any) {
|
||||
_, _ = fmt.Fprint(&printer.message, args...)
|
||||
}
|
||||
|
||||
func (printer *messagePrinter) Printf(format string, args ...any) {
|
||||
_, _ = fmt.Fprintf(&printer.message, format, args...)
|
||||
}
|
||||
|
||||
func (*messagePrinter) Detail() bool { return false }
|
||||
|
||||
func appendSchemaFieldErrors(fields []api.ProblemFieldError, err *validate.Error, prefix string) []api.ProblemFieldError {
|
||||
for _, field := range err.Fields {
|
||||
if len(fields) >= maxFieldErrors {
|
||||
|
||||
Reference in new issue
Block a user