MyNixOS website logo
Description

Parse and validate forms in JSON format.

Forma

License BSD3 Hackage Stackage Nightly Stackage LTS CI

This library provides a tool for validation of forms in the JSON format. Sending forms in the 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 effects that form submission should initiate.

  • You get a chance to organize form input the way 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 a form parser using type-safe applicative notation with field labels stored on the type label which guards against typos and will force all your field labels to be always up to date.

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

  • Stop parsing immediately if a 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.

  • Collect validation errors from multiple branches of parsing (a branch per form field) in parallel, so that validation errors in one branch do not prevent us from collecting validation errors from other branches. This allows for 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 ad-hoc helpers.

  • 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 qualified Data.Text as T
import Web.Forma

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.

Metadata

Version

1.2.0

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows