Documentation
¶
Overview ¶
Package schemavalidation validates the JSON bodies of the requests and responses of an eventhandler.Router against JSON Schemas, with the validation package: Powertools for AWS Lambda (Python)'s SchemaValidationMiddleware.
var orderSchema = validation.MustCompile(orderSchemaJSON)
app.Handle("POST /orders", createOrder,
eventhandler.Middlewares(schemavalidation.Middleware(orderSchema, nil)))
A request whose body breaks the inbound schema answers 400 with {"statusCode":400,"message":"Bad Request: <message>"}; with an outbound schema, a response that breaks it answers 400 "Bad Response: <message>", as in Python; a schema that does not compile answers 500 "Internal Server Error". An empty body is validated as null.
It lives apart from the eventhandler package so that functions that do not use it do not load a general JSON Schema engine, whose initialization costs milliseconds of every cold start. Typed operations validate their input without it.
Differences from Powertools Python ¶
Python validates the response only when outbound formats are given, and then against the inbound schema; this middleware validates it against the outbound schema whenever there is one. A body that is not JSON answers 400; Python fails with an unhandled error.
Index ¶
Examples ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func Middleware ¶
func Middleware(inbound, outbound any, opts ...validation.Option) eventhandler.Middleware
Middleware returns a middleware that validates the JSON body of every request against the inbound JSON Schema and, when outbound is not nil, the body of every response against outbound: Python's SchemaValidationMiddleware, on top of the validation package. Schemas are what validation.Compile takes (a *validation.Schema, raw JSON, a map[string]any); opts are the validation options (custom formats, loaders...).
A request that breaks the schema answers 400 "Bad Request: <message>", a response that does "Bad Response: <message>" (400, as in Python), and a schema that does not compile answers 500 "Internal Server Error". An empty body is validated as null, as Python validates a missing JSON body as None.
Python validates the response only when outbound formats are given, and then against the inbound schema; this middleware validates it against the outbound schema whenever there is one.
Example ¶
package main
import (
"context"
"fmt"
"net/http"
"strings"
"github.com/aws/aws-lambda-go/events"
"github.com/alessandrolattao/powertools-lambda-go/eventhandler"
"github.com/alessandrolattao/powertools-lambda-go/eventhandler/schemavalidation"
)
func main() {
schema := `{"type": "object", "properties": {"name": {"type": "string"}}, "required": ["name"]}`
app := eventhandler.New()
app.HandleFunc("POST /users", func(w http.ResponseWriter, _ *http.Request) {
w.WriteHeader(http.StatusCreated)
}, eventhandler.Middlewares(schemavalidation.Middleware(schema, nil)))
resp, err := eventhandler.APIGatewayREST(app)(context.Background(), events.APIGatewayProxyRequest{
HTTPMethod: "POST", Path: "/users", Body: `{}`,
})
if err != nil {
panic(err)
}
fmt.Println(resp.StatusCode, strings.HasPrefix(resp.Body, `{"statusCode":400,"message":"Bad Request: Failed schema validation.`))
}
Output: 400 true
Types ¶
This section is empty.