GithubHelp home page GithubHelp logo

ocharles / digestive-functors-aeson Goto Github PK

View Code? Open in Web Editor NEW
34.0 5.0 9.0 55 KB

Run digestive-functors against a JSON document

License: GNU General Public License v3.0

Haskell 85.00% Nix 15.00%

digestive-functors-aeson's Introduction

digestive-functors-aeson

Run digestive-functors forms against JSON documents (via aeson).

digestive-functors-aeson's People

Contributors

3noch avatar gelisam avatar k0001 avatar nieled avatar ocharles avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar

digestive-functors-aeson's Issues

Integer 1 end up as Number 1.0, causing trouble

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!

jsonErrors and Type Safety

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.

Optional primitives

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 :)

Newbie questions

I'm struggling with validating an incoming JSON (newbie alert!).

  1. Is there a typo in this example in the docs? Do we really mean to pass "" as the first argument to digestJSON
  2. Is it possible to get a slightly longer code snippet where the Form v m a is being constructed using the record constructor and not the tuple constructor?

Default bool value ignored

> 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.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.