allow `validateField` to work on Maybe values
amitaibu opened this issue · 2 comments
amitaibu commented
ihp/IHP/ValidationSupport/ValidateField.hs
Lines 62 to 69 in d065883
Right now validateField
assumes we have required fields. So if we have a Maybe Text
for the body it won't work.
So this won't work as is:
buildPost post = post
|> fill @'["title", "sourceUrl"]
|> validateField #name nonEmpty
|> validateField #sourceUrl isUrl
@mpscholten we could add validateMaybeField
, but I think validateField
"just" working would be nicer. Do you agree?
amitaibu commented
Maybe a simpler option is to add this
-- | Validate a Maybe field.
validateMaybe :: (val -> ValidatorResult) -> Maybe val -> ValidatorResult
validateMaybe _ Nothing = Success
validateMaybe validator (Just value) = validator value
So then we can have this working:
buildPost post = post
|> fill @'["title", "sourceUrl"]
|> validateField #name nonEmpty
|> validateField #sourceUrl (validateMaybe isUrl)
mpscholten commented
The validateMaybe
function looks great. Making validateField
work automatically with Maybe fields will add a lot of complexity, so I prefer how you solved it 👍