https://github.com/katydid/katydid-haskell
An Encoding Agnostic Validation Language
https://github.com/katydid/katydid-haskell
automata haskell nested-structures schema validation
Last synced: 9 months ago
JSON representation
An Encoding Agnostic Validation Language
- Host: GitHub
- URL: https://github.com/katydid/katydid-haskell
- Owner: katydid
- License: bsd-3-clause
- Created: 2016-12-31T12:42:30.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2018-10-14T17:33:28.000Z (about 7 years ago)
- Last Synced: 2025-03-23T20:37:20.442Z (9 months ago)
- Topics: automata, haskell, nested-structures, schema, validation
- Language: Haskell
- Homepage: https://katydid.github.io/katydid-haskell/
- Size: 1.3 MB
- Stars: 6
- Watchers: 3
- Forks: 0
- Open Issues: 3
-
Metadata Files:
- Readme: Readme.md
- Changelog: CHANGELOG.md
- License: LICENSE
Awesome Lists containing this project
README
# Katydid
[](https://travis-ci.org/katydid/katydid-haskell)
A Haskell implementation of Katydid.

This includes:
- [Relapse](https://katydid.github.io/katydid-haskell/Relapse.html): Validation Language
- Parsers: [JSON](https://katydid.github.io/katydid-haskell/Json.html) and [XML](https://katydid.github.io/katydid-haskell/Xml.html)
[Documentation for katydid](http://katydid.github.io/)
[Documentation for katydid-haskell](https://katydid.github.io/katydid-haskell/)
[Documentation for katydid-haskell/Relapse](https://katydid.github.io/katydid-haskell/Relapse.html)
All JSON and XML tests from [the language agnostic test suite](https://github.com/katydid/testsuite) [passes].
[Hackage](https://hackage.haskell.org/package/katydid)
## Example
Validating a single structure can be done using the validate function:
```haskell
validate :: Tree t => Grammar -> [t] -> Bool
```
, where a tree is a class in the [Parsers](https://katydid.github.io/katydid-haskell/Parsers.html) module:
```haskell
class Tree a where
getLabel :: a -> Label
getChildren :: a -> [a]
```
Here is an example that validates a single JSON tree:
```haskell
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.
```haskell
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:
```haskell
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:
```haskell
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)