realworld-hs-0.1.0.0: Scotty impl of https://github.com/gothinkster/realworld
Safe HaskellSafe-Inferred
LanguageGHC2021

Conduit.Validation

Synopsis

Documentation

class Validation property on where Source #

A simple validation class using typeclass-metaprogramming (TMP) to help catch and generate Conduit-spec-abiding validation errors Intended for use on Aeson Parsers (in manual FromJSON instances) See also: NotBlank (example), (<!<), and (<?!<)

data IsRed = IsRed

instance Validation IsRed Text where
  validate = (== "red")
  errMsg = "must be red"
newtype Test = Test Text

instance FromJSON Test where
  parseJSON = withObject "Test" $ \v -> Test
    <$> v .: "test" <!< IsRed <!< NotBlank -- assurances are evaluated R->L
>>> eitherDecode @Test "{ \"test\": \"\" }"
Left "Error in $.test: can't be blank"
>>> eitherDecode @Test "{ \"test\": \"abc\" }"
Left "Error in $.test: must be red"

Instances

Instances details
Validation NotBlank UnsafePassword Source # 
Instance details

Defined in Conduit.Identity.Password

Validation NotBlank Text Source # 
Instance details

Defined in Conduit.Validation

data NotBlank Source #

A property ensuing that the given json field isn't blank (intended for String/Text-like objects).

Constructors

NotBlank 

Instances

Instances details
Validation NotBlank UnsafePassword Source # 
Instance details

Defined in Conduit.Identity.Password

Validation NotBlank Text Source # 
Instance details

Defined in Conduit.Validation

newtype ValErrs Source #

Represents errors as the spec describes.

Constructors

ValErrs [(Text, Text)] 

Instances

Instances details
ToJSON ValErrs Source # 
Instance details

Defined in Conduit.Validation

Show ValErrs Source # 
Instance details

Defined in Conduit.Validation

parseJsonBody :: forall a m. (MonadUnliftIO m, FromJSON a) => ActionT m a Source #

Extracts and decodes an expected json body, intended for use with Validations. Returns the appropriate unprocessable errors the Conduit spec expects.

newtype Test = Test Text
  deriving (Show)

instance FromJSON Test where
  parseJSON = withObject "Test" $ \v -> Test
    <$> v .: "test" <!< NotBlank

endpoint = post "/" $ do
  result <- parseJsonBody @Test
  print text

inErrMsgObj :: obj -> InObj obj Source #

(<?!<) :: Parser (Maybe (Assurance prop on)) -> prop -> Parser (Maybe on) Source #

Validates an optional json field *if it exists*.

(<!<) :: Parser (Assurance prop on) -> prop -> Parser on Source #

Validates a json field.