Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Newbie questions #10

Open
saurabhnanda opened this issue Jan 3, 2017 · 9 comments
Open

Newbie questions #10

saurabhnanda opened this issue Jan 3, 2017 · 9 comments

Comments

@saurabhnanda
Copy link

saurabhnanda commented Jan 3, 2017

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?
@ocharles
Copy link
Owner

ocharles commented Feb 7, 2017

  1. That indeed looks like a typo
  2. Like this?
pokeForm :: Monad m => Form Text m Pokemon
pokeForm = liftA2 (\name number -> Pokemon { pokemonName = name, pokemonNumber = number}) 
                  ("name" .: nonEmptyText)
                  ("number" .: parseInteger)
  where
    nonEmptyText = check "Name cannot be empty" (not . T.null) $ text Nothing

@saurabhnanda
Copy link
Author

Something like that. What's an easier way to write it?

do
  pokemonName <- "name" .: nonEmptyText
  pokemonNumber <- "number" .: parseInteger
  return Pokemon{..}

@ocharles
Copy link
Owner

ocharles commented Feb 8, 2017

You can do exactly that if you enable {-# LANGUAGE ApplicativeDo #-} and use the latest version of GHC.

@saurabhnanda
Copy link
Author

Does that mean I can't do a monadic operation like uniqueness checks against a DB in this validation layer?

@ocharles
Copy link
Owner

ocharles commented Feb 8, 2017

There is no Monad instance for Form, but you can still lift any m computation into it. Check the docs for digestive-functors. The lack of Monad just means the validation for one field can't depend on the contents of another field

@saurabhnanda
Copy link
Author

So, would it be this:

monadic $ do
  pokemonName <- "name" .: nonEmptyText
  pokemonNumber <- "number" .: parseInteger
  return Pokemon{..}

@ocharles
Copy link
Owner

ocharles commented Feb 8, 2017

No, because monadic lifts an m operation, but "name" .: nonEmptyText is a Form operation. Also, I think you might have better luck with questions of what you're actually trying to do, and will probably get more responses on Haskell StackOverflow or the mailing lists.

@saurabhnanda
Copy link
Author

I actually want to raise a PR to make the docs better with examples of common use-cases.

@ocharles
Copy link
Owner

ocharles commented Feb 8, 2017 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants