wai-middleware-validation: WAI Middleware to validate the request and response bodies

[ bsd3, library, web ] [ Propose Tags ]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1.0.0, 0.1.0.1, 0.1.0.2
Change log ChangeLog.md
Dependencies aeson, base (>=4.7 && <5), bytestring, containers, filepath, http-types, insert-ordered-containers, lens, openapi3, text, wai [details]
License BSD-3-Clause
Copyright IIJ Innovation Institute Inc.
Author Kenzo Yotsuya
Maintainer kyotsuya@iij-ii.co.jp
Category Web
Home page https://github.com/iij-ii/wai-middleware-validation#readme
Source repo head: git clone https://github.com/iij-ii/wai-middleware-validation
Uploaded by KenzoYotsuya at 2021-03-17T10:28:19Z
Distributions
Downloads 294 total (13 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2021-03-17 [all 1 reports]

Readme for wai-middleware-validation-0.1.0.0

[back to package description]

wai-middleware-validation CircleCI

wai-middleware-validation is a WAI Middleware that automates the validation of request and response bodies. It validates JSON format bodies according to the schema defined in the OpenAPI document.

Usage

The following is an example of applying it to a Yesod application.

  1. Define the request and response specifications as an OpenAPI document file in JSON format and place it in an arbitrary path. (In this case, we will use "config/openapi.json".)
  2. Make the following modifications to Application.hs.
--- a/src/Application.hs
+++ b/src/Application.hs
@@ -37,6 +37,8 @@ import Network.Wai.Middleware.RequestLogger (Destination (Logger),
                                              mkRequestLogger, outputFormat)
 import System.Log.FastLogger                (defaultBufSize, newStdoutLoggerSet,
                                              toLogStr)
+import Network.Wai.Middleware.Validation    (mkValidator')
+import qualified Data.ByteString.Lazy as L

 -- Import all relevant handler modules here.
 -- Don't forget to add new modules to your cabal file!
@@ -94,7 +97,10 @@ makeApplication foundation = do
     logWare <- makeLogWare foundation
     -- Create the WAI application and apply middlewares
     appPlain <- toWaiAppPlain foundation
-    return $ logWare $ defaultMiddlewaresNoLogging appPlain
+    apiJson <- L.readFile "config/openapi.json"
+    let validator = fromMaybe (error "Invalid OpenAPI document") (mkValidator' apiJson)
+        app = validator appPlain
+    return $ logWare $ defaultMiddlewaresNoLogging app

 makeLogWare :: App -> IO Middleware
 makeLogWare foundation =

LICENCE

Copyright (c) IIJ Innovation Institute Inc.

Licensed under The 3-Clause BSD License.