Run digestive-functors
forms against JSON documents (via aeson
).
ocharles / digestive-functors-aeson Goto Github PK
View Code? Open in Web Editor NEWRun digestive-functors against a JSON document
License: GNU General Public License v3.0
Run digestive-functors against a JSON document
License: GNU General Public License v3.0
The code:
data Item =
{ itemComment :: Data.Text.Text
, itemAmount :: Int }
itemForm :: Form Data.Text.Text IO Item
itemForm = Item
<$> "commment" .: text Nothing
<*> "amount" .: stringRead "amount must be integer" Nothing
createItem :: String -> IO String
createItem input = case Attoparsec.maybeResult . Attoparsec.parse Aeson.json . pack $ input of
Just parsedJson -> do
print parsedJson
(view, result) <- digestJSON Forms.itemForm parsedJson
case result of
Just b -> do
db . insert_ $ b
return "Success"
Nothing -> return $ "Form error: " ++ (unpack $ encode $ jsonErrors view)
Nothing -> return "Parse error"
JSON input:
{
"comment":"blasfsdf",
"amount":1
}
I get following output:
Form error: {"amount":"amount must be integer"}
I checked the contents of parsedJson
to be:
Object (fromList [("amount",Number 1.0),("comment",String "bla")])
Am I correct to assume that the 1
in the JSON input turned into Number 1.0
and is henceforth treated as Double
, causing the error? How to properly parse and digest integer values with digestive-functors-aeson
?
Or do I have a simple error elsewhere? All help is appreciated!
Hello!
First, thank you very much for this library. I'm incorporating it into my "how do I Servant" repo. (I'm very much a Haskell beginner).
Secondly, I noticed that if I have a value of type View
that does not actually have any errors, and apply jsonErrors
to that value, it will raise an exception.
I was wondering if there is a minimially-intrusive way to increase the type safety of jsonErrors
, and what I can do to help with that effort.
digestive-functors
is created for html forms there is no common way to define optional non-text form (or I can't find it?).
Is there any way to define, for example, optionalBool
, optionalInteger
in digestive-functors-aeson
? If no, consider it as feature request :)
I'm struggling with validating an incoming JSON (newbie alert!).
""
as the first argument to digestJSON
Form v m a
is being constructed using the record constructor and not the tuple constructor?Hi, there have been several updates to aeson. Including better error reporting.
I wonder, could you bump dep ver? Seems no major updates been done.
Check: https://hackage.haskell.org/package/aeson-0.9.0.1/changelog
Cheers,
Viktor.
Gack, lens keeps on getting updated. Now at 4.11.
Due to the upper bound in the cabal file.
Hi! Have you tried digestive-functors-aeson with base-4.8.0? I wonder if it will work, there been a bit of changes https://hackage.haskell.org/package/base-4.8.0.0/changelog.
> let form = "testBool" .: bool (Just True)
> let mValue = decode "{}" :: Maybe Value
> let Just v = mValue
> (view, result) <- digestJSON form v
> result
Just False
The result should be Just True
.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.