Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7016541
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T22:47:56+00:00 2026-05-27T22:47:56+00:00

I have a few nested records that I need to validate, and I wonder

  • 0

I have a few nested records that I need to validate, and I wonder what is an idiomatic Haskell way to do it.

To simplify:

data Record = Record {
  recordItemsA :: [ItemA],
  recordItemB :: ItemB
} deriving (Show)

data ItemA {
  itemAItemsC :: [ItemC]
} deriving (Show)

Requirements are:

  • Collect and return all validation errors
  • Some validations may be across items, e.g. ItemsA against ItemB
  • Strings are sufficient to represent errors

I currently have code that feels awkward:

type ErrorMsg = String

validate :: Record -> [ErrorMsg]
validate record =
  recordValidations ++ itemAValidations ++ itemBValidations
  where
    recordValidations :: [ErrorMsg]
    recordValidations = ensure (...) $
      "Invalid combination: " ++ (show $ recordItemsA record) ++ " and " ++ (show $ recordItemsB record)
    itemAValidations :: [ErrorMsg]
    itemAValidations = concat $ map validateItemA $ recordItemsA record
    validateItemA :: ItemA -> [ErrorMsg]
    validateItemA itemA = ensure (...) $
      "Invalid itemA: " ++ (show itemA)
    itemBValidations :: [ErrorMsg]
    itemBValidations = validateItemB $ recordItemB record
    validateItemB :: ItemB -> [ErroMsg]
    validateItemB itemB = ensure (...) $
      "Invalid itemB: " ++ (show itemB)

ensure :: Bool -> ErrorMsg -> [ErrorMsg]
ensure b msg = if b then [] else [msg]
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T22:47:57+00:00Added an answer on May 27, 2026 at 10:47 pm

    What you have already is basically fine, it just needs some clean-up:

    • The sub-validations should be top-level definitions, as they’re fairly involved. (By the way, type signatures on where clause definitions are usually omitted.)
    • Lack of consistent naming convention
    • Lots of (++)s in sequence can get ugly — use concat (or perhaps unwords) instead
    • Minor formatting quirks (there are some superfluous parentheses, concat . map f is concatMap f, etc.)

    The product of all this:

    validateRecord :: Record -> [ErrorMsg]
    validateRecord record = concat
      [ ensure (...) . concat $
          [ "Invalid combination: ", show (recordItemsA record)
          , " and ", show (recordItemB record)
          ]
      , concatMap validateItemA $ recordItemsA record
      , validateItemB $ recordItemB record
      ]
    
    validateItemA :: ItemA -> [ErrorMsg]
    validateItemA itemA = ensure (...) $ "Invalid itemA: " ++ show itemA
    
    validateItemB :: ItemB -> [ErrorMsg]
    validateItemB itemB = ensure (...) $ "Invalid itemB: " ++ show itemB
    

    I think that’s pretty good. If you don’t like the list notation, you can use the Writer [ErrorMsg] monad:

    validateRecord :: Record -> Writer [ErrorMsg] ()
    validateRecord record = do
      ensure (...) . concat $
        [ "Invalid combination: ", show (recordItemsA record)
        , " and ", show (recordItemB record)
        ]
      mapM_ validateItemA $ recordItemsA record
      validateItemB $ recordItemB record
    
    validateItemA :: ItemA -> Writer [ErrorMsg] ()
    validateItemA itemA = ensure (...) $ "Invalid itemA: " ++ show itemA
    
    validateItemB :: ItemB -> Writer [ErrorMsg] ()
    validateItemB itemB = ensure (...) $ "Invalid itemB: " ++ show itemB
    
    ensure :: Bool -> ErrorMsg -> Writer [ErrorMsg] ()
    ensure b msg = unless b $ tell [msg]
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have few text and excel file, from which I need to import data
I have a server-generated html like: <ul> <li><!-- few nested elements that form a
I'm struggling for a pattern here. I have quite a few tasks that need
I have few nested DIVs at page. I want to add event only for
I have a page (telerik:RadPage) containing few grids and some nested controls and I
I have few questions about using lock to protect my shared data structure. I
I have few nested DIV elements. For example: <div id='id1'><div id='id2'>content</div></div> I attach event
I have a few places in my code where I have nested objects, but
I have a few nested and I want to allow my deepest to scroll
I have a few questions relating to a Transaction object that I'm creating. Transaction

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.