forma

Parse and validate forms in JSON format

https://github.com/mrkkrp/forma

Version on this page:1.1.3@rev:1
LTS Haskell 23.0:1.2.0@rev:3
Stackage Nightly 2024-12-09:1.2.0@rev:3
Latest on Hackage:1.2.0@rev:3

See all snapshots forma appears in

BSD-3-Clause licensed and maintained by Mark Karpov
This version can be pinned in stack with:forma-1.1.3@sha256:c86bfef1f41ee9470de2afa2f5426dd8fca83b4fe42251fd854e3464c0ee0c5d,2280

Module documentation for 1.1.3

Forma

License BSD3 Hackage Stackage Nightly Stackage LTS Build Status

This module provides a tool for validation of forms that are represented in the JSON format. Sending forms in JSON format via an AJAX request instead of traditional submitting of forms has a number of advantages:

  • Smoother user experience: no need to reload the whole page.

  • Form rendering is separated and lives only in GET handler, POST (or whatever method you deem appropriate for your use case) handler only handles validation and actual effects that form submission should initiate.

  • You get a chance to organize form input just like you want.

The task of validation of a form in the JSON format may seem simple, but it’s not trivial to get it right. The library allows you to:

  • Define form parser using type-safe applicative notation with field labels being stored on the type label which guards against typos and will force all your field labels be always up to date.

  • Parse JSON Value according to the definition of form you created.

  • Stop parsing immediately if given form is malformed and cannot be processed.

  • Validate forms using any number of composable checkers that you write for your specific problem domain. Once you have a vocabulary of checkers, creation of new forms is just a matter of combining them, and yes they do combine nicely.

  • Collect validation errors from multiple branches of parsing (one branch per form field) in parallel, so validation errors in one branch do not prevent us from collecting validation errors from other branches. This allows for a better user experience as the user can see all validation errors at the same time.

  • Use optional and (<|>) from Control.Applicative in your form definitions instead of ugly ad-hoc stuff.

  • Perform validation using several form fields at once. You choose which “sub-region” of your form a given check will have access to, see withCheck.

Example of use

Here is a complete working example:

{-# LANGUAGE DataKinds         #-}
{-# LANGUAGE OverloadedLabels  #-}
{-# LANGUAGE OverloadedStrings #-}

module Main (main) where

import Control.Monad.Except
import Data.Aeson
import Data.Text (Text)
import Web.Forma
import qualified Data.Text as T

type LoginFields = '["username", "password", "remember_me"]

data LoginForm = LoginForm
  { loginUsername   :: Text
  , loginPassword   :: Text
  , loginRememberMe :: Bool
  } deriving (Show)

loginForm :: Monad m => FormParser LoginFields Text m LoginForm
loginForm = LoginForm
  <$> field #username notEmpty
  <*> field #password notEmpty
  <*> field' #remember_me

notEmpty :: Monad m => Text -> ExceptT Text m Text
notEmpty txt =
  if T.null txt
    then throwError "This field cannot be empty."
    else return txt

myInput :: Value
myInput = object
  [ "username"    .= ("Bob" :: Text)
  , "password"    .= ("123" :: Text)
  , "remember_me" .= True
  ]

main :: IO ()
main = runForm loginForm myInput >>= print

You may want to play with it a bit before writing serious code.

Contribution

Issues, bugs, and questions may be reported in the GitHub issue tracker for this project.

Pull requests are also welcome.

License

Copyright © 2017–present Mark Karpov

Distributed under BSD 3 clause license.

Changes

Forma 1.1.3

  • Test suite passes with aeson-1.4.6.0.

  • Dropped support for GHC 8.2 and older.

Forma 1.1.2

  • Fixed the test suite so it passes with aeson-1.4.3.0.

Forma 1.1.1

  • Fixed a bug which caused withCheck (and functions using it such as field) report incorrect location of element for which validation fails when it’s nested in subParser wrappers.

Forma 1.1.0

  • Added runFormPure.

Forma 1.0.0

  • The library has been completely redesigned and rewritten.

Forma 0.2.0

  • Added withCheck.

Forma 0.1.0

  • Initial release.