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

[ bsd3, library, web ] [ Propose Tags ]

Please see the README on GitHub at https://github.com/iij-ii/wai-middleware-validation#readme

[Skip to Readme]
Versions [RSS] [faq],,
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 NixOS:
Downloads 104 total (13 in the last 30 days)
Rating (no votes yet) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Hackage Matrix CI
Docs available [build log]
Last success reported on 2021-03-17 [all 1 reports]


[Index] [Quick Jump]


Maintainer's Corner

For package maintainers and hackage trustees


Readme for wai-middleware-validation-

[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.


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,
+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 =


Copyright (c) IIJ Innovation Institute Inc.

Licensed under The 3-Clause BSD License.