English 中文(简体)
使用含有消化器和吸附器及吸附器的修道院验证
原标题:Using monadic validation with Digestive Functors and Snap

我试图在相当长的一段时间里 环绕着我的大脑 如何在消化式杀菌剂形式字段中使用验证, 这需要访问另一个monad 。 简略地说, 我有一个< hackage. hasskell. org/ packages/archives/ archive/ digestive- functors/ 0. 4. 0.0/ doc/ html/ Text- Digestive. html" rel= “ nofolpol” >digestive form like this

studentRegistrationForm :: Monad m => Form Text m StudentRegistrationData
studentRegistrationForm = StudentRegistrationData
    <$> "school"    .: choice schools Nothing
    <*> "studentId" .: check studentIdErrMsg (not . T.null) (text Nothing)
    <*> "firstName" .: check firstNameErrMsg (not . T.null) (text Nothing)
    <*> "lastName"  .: check lastNameErrMsg  (not . T.null) (text Nothing)
    <*> "email"     .: check emailErrMsg (E.isValid . T.unpack) (text Nothing)

(学生Id基本上是用户名)

并使用usernameexists < a href="http://hackage.haskell.org/packages/archives/archive/snap/ 0.7/doc/html/Snap-Snaplet-Auth.html" rel=“nofollow”>Snap.Snaplet.Auth 的函数,检查输入用户名是否独一无二。

为了完整起见,这里是相应的数据类型:

data StudentRegistrationData = StudentRegistrationData
  { school    :: School  -- ^ school the student is enroled
  , studentId :: Text    -- ^ matriculation number of the student
  , firstName :: Text    -- ^ first name of the student
  , lastName  :: Text    -- ^ last name of the student
  , email     :: Text    -- ^ email for sending password
  } deriving (Show)

我创造我的形式 在一个处理器像:

studentRegistrationHandler :: AppHandler ()
studentRegistrationHandler = do
    (view, registrationData) <- runForm "form" SRF.studentRegistrationForm
    maybe (showForm "registration" view) createUser registrationData

showForm :: String -> View Text -> AppHandler ()
showForm name view =
    heistLocal (bindDigestiveSplices view) $ render template
  where
    template = BS.pack $ "student-" ++ name ++ "-form"

所以,我现在的问题是如何理解 如何在窗体中访问 auuth spnillet 状态。 它已经通过还是我必须亲自通过? checkM 分别在 validateM < a href=" http://hackage. haskell.org/packages/archages/archive/dicistive-functors/0.4.0.0/doc/ html/ Text- Digestive- Form.html" rel="no follow" > Text. Digestive. Form < / a > 能帮助我吗?

我发现了一些如何使用消化杀菌剂和折断授体和会话的例子,例如:

但没有人能显示Snap.Snaplet.Adex和消化带菌者直接一起工作,

我可以在刺青上上传一个独立的例子, 说明我的问题, 如果有助于说明这一点的话。 任何提示、 指示和建议都是非常欢迎的!!!!

汉内斯

add on: I created an example application demonstrating basic authentication functionality, you may have a look here: digestive-functors-snap-auth-example enjoy!

最佳回答

我还没有尝试过这个来查看是否所有类型检查, 但这里是一般的想法。 您想使用检查M 或验证M 来验证您的monadic 。 检查M 的类型签名是信息化的 :

checkM :: Monad m => v -> (a -> m Bool) -> Form v m a -> Form v m a

这告诉我们,验证功能需要具备类型 (a - & gt; m Bool), < strong > 和 , m 必须和窗体中的 m 相同。 这意味着您需要将您的窗体类型更改为类似类型 :

studentRegistrationForm :: Form Text AppHandler StudentRegistrationData

现在让我们写下验证符。 既然我们计划在我们的验证符中使用用户名Exists 函数, 我们需要查看该类型签名 :

usernameExists :: Text -> Handler b (AuthManager b) Bool

这实际上看起来与我们需要的 (a- & gt; m Bool) 类型签名非常相似。 事实上, 它是一个精确匹配, 因为 handler b (AuthManager b) 是一monad。 但即使它符合 (a- > m Bool) 模式, 也并不表示我们已经完成了。 当您运行您的窗体时, 您会重新进入 AppHandler monad, 它可能只是 handler App 的别名, 其中 App 是您的应用程序的顶级松动状态类型。 所以我们需要将 Handler b (Authmanger b) 转换为 (a- handler b b b 将和 < code > 统一到 < had> 。 。 < a hest_ smaskages_ sqn > a 函数: smapple. smappage/ a.

validUser :: Text -> Handler App App Bool
validUser = liftM not . with auth . usernameExists

您可以使用 checkM usernameErrMsg 有效用户 就像您在上述代码中使用的检查一样 。

问题回答

暂无回答




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?