#middleware #serverless 

GoIntercept: v0.3.1 Released

Version 0.3.1 of GoIntercept is here!

Elegant and modular middleware for AWS Lambdas with Golang

Elegant and modular middleware for AWS Lambdas with Golang

This release contains the following updates:

  • Interceptors AddHeader and AddSecurityHeaders now add HTTP headers only when a proper API Gateway Response has been previously created
  • Interceptor CreateAPIGatewayProxyResponse now gracefully handles uncaught HTTP errors (a new type HTTPError was added!) and creates a proper API Gateway Proxy Response for them. To trigger this new behavior, it is recommended to generate errors using the new type HTTPError
  • A new interceptor to provide JSON schema validation. More on this below!

About JSON Schema Validation

This release includes a new interceptor to validate HTTP requests with a JSON body. When used in combination with the ParseInput interceptor, a valid JSON body can be converted into an object. Additionally, validation errors are gracefully handled by throwing an instance of the new HTTPError with the corresponding 422 (Unprocessable Entity) error.

This new interceptor uses the powerful library *JSONSCHEMA* provided by qri.io. Go take a look!

The snippet below shows how this new interceptor can be used to validate a JSON body in the context of GoIntercept:

	const (
			schema = `{
			"$id": "https://qri.io/schema/",
			"$comment" : "sample comment",
			"title": "Input",
			"type": "object",
			"properties": {
				"content": {
					"type": "string"
				},
				"value": {
					"description": "The Value",
					"type": "integer",
					"minimum": 0,
					"maximum": 2
				}
			},
			"required": ["content", "value"]
		}`
	)

	lambda.Start(gointercept.This(SampleFunction).With(
		interceptors.CreateAPIGatewayProxyResponse(&interceptors.DefaultStatusCodes{Success: 200, Error: 400}),
		interceptors.ValidateJSONSchema(schema),
		interceptors.ParseInput(&Input{}, false)),
	))

First, we define a custom JSON schema, specifying rules such as attribute type, mandatoriness, lower and upper limits, description, etc. As mentioned previously, the ValidateJSONSchema interceptor gets along very well with the ParseInput and CreateAPIGatewayProxyResponse interceptors. The former benefits from a solid validation step before it tries to parse and convert its input. The latter provides a graceful response to the user in the event of a validation failure.

All in all, the GoIntercept project is here to stay. Its benefits and advantages will become more evident as new interceptors are added.

The road ahead is bright and exciting. Stay tuned!

Image by WikiImages from Pixabay