An Encoding Agnostic Validation Language
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Failed to load latest commit information.
app
bench
docs
src/Data/Katydid
test protobuf repeated Oct 14, 2018
.gitignore ignore Nov 5, 2017
.travis.yml
CHANGELOG.md changelog name change Sep 14, 2018
LICENSE map Dec 30, 2016
Makefile initial work for supporting protobufs Oct 2, 2018
Readme.md move files into more idiomatic packages and replaced cabal file with … Sep 10, 2018
Setup.hs fmt with brittany Sep 14, 2018
katydid.cabal
package.yaml initial work for supporting protobufs Oct 2, 2018
stack.yaml move files into more idiomatic packages and replaced cabal file with … Sep 10, 2018

Readme.md

Katydid

Build Status

A Haskell implementation of Katydid.

Katydid Logo

This includes:

Documentation for katydid

Documentation for katydid-haskell

Documentation for katydid-haskell/Relapse

All JSON and XML tests from the language agnostic test suite [passes].

Hackage

Example

Validating a single structure can be done using the validate function:

validate :: Tree t => Grammar -> [t] -> Bool

, where a tree is a class in the Parsers module:

class Tree a where
    getLabel :: a -> Label
    getChildren :: a -> [a]

Here is an example that validates a single JSON tree:

main = either 
    (\err -> putStrLn $ "error:" ++ err) 
    (\valid -> if valid 
        then putStrLn "dragons exist" 
        else putStrLn "dragons are fictional"
    ) $
    Relapse.validate <$> 
        Relapse.parse ".DragonsExist == true" <*> 
        Json.decodeJSON "{\"DragonsExist\": false}"

Efficiency

If you want to validate multiple trees using the same grammar then the filter function does some internal memoization, which makes a huge difference.

filter :: Tree t => Grammar -> [[t]] -> [[t]]

User Defined Functions

If you want to create your own extra functions for operating on the leaves, then you can inject them into the parse function:

main = either
    (\err -> putStrLn $ "error:" ++ err)
    (\valid -> if valid
        then putStrLn "prime birthday !!!"
        else putStrLn "JOMO"
    ) $
    Relapse.validate <$>
        Relapse.parseWithUDFs userLib ".Survived->isPrime($int)" <*>
        Json.decodeJSON "{\"Survived\": 104743}"

Defining your own user library to inject is easy. The Expr library provides many useful helper functions:

import Data.Numbers.Primes (isPrime)
import Data.Katydid.Relapse.Expr

userLib :: String -> [AnyExpr] -> Either String AnyExpr
userLib "isPrime" args = mkIsPrime args
userLib n _ = throwError $ "undefined function: " ++ n

mkIsPrime :: [AnyExpr] -> Either String AnyExpr
mkIsPrime args = do {
    arg <- assertArgs1 "isPrime" args;
    mkBoolExpr . isPrimeExpr <$> assertInt arg;
}

isPrimeExpr :: Integral a => Expr a -> Expr Bool
isPrimeExpr numExpr = trimBool Expr {
    desc = mkDesc "isPrime" [desc numExpr]
    , eval = \fieldValue -> isPrime <$> eval numExpr fieldValue
}

Roadmap

  • Protobuf parser
  • Profile and Optimize (bring up to par with Go version)
  • Typed DSL (Combinator)