@@ -6,7 +6,6 @@
66module Network.API.Mandrill.Types where
77
88import Network.API.Mandrill.Utils
9−import Network.API.Mandrill.Orphans()
109import Test.QuickCheck
1110import Text.Email.Validate
1211import Data.Char
@@ -101,10 +100,23 @@ data MandrillRecipientTag = To | Cc | Bcc deriving Show
101100deriveJSON defaultOptions { constructorTagModifier = map toLower } ''MandrillRecipientTag
102101
103102
103+--------------------------------------------------------------------------------
104+newtype MandrillEmail = MandrillEmail EmailAddress deriving Show
105+
106+instance ToJSON MandrillEmail where
107+ toJSON (MandrillEmail e) = String . TL.decodeUtf8 . toByteString $ e
108+
109+instance FromJSON MandrillEmail where
110+ parseJSON (String s) = case validate (TL.encodeUtf8 s) of
111+ Left err -> fail err
112+ Right v -> return . MandrillEmail $ v
113+ parseJSON o = typeMismatch "Expecting a String for MandrillEmail." o
114+
115+
104116--------------------------------------------------------------------------------
105117-- | An array of recipient information.
106118data MandrillRecipient = MandrillRecipient {
107− _mrec_email :: EmailAddress
119+ _mrec_email :: MandrillEmail
108120 -- ^ The email address of the recipient
109121 , _mrec_name :: Maybe T.Text
110122 -- ^ The optional display name to use for the recipient
@@ -117,11 +129,11 @@ makeLenses ''MandrillRecipient
117129deriveJSON defaultOptions { fieldLabelModifier = drop 6 } ''MandrillRecipient
118130
119131newRecipient :: EmailAddress -> MandrillRecipient
120−newRecipient email = MandrillRecipient email Nothing Nothing
132+newRecipient email = MandrillRecipient (MandrillEmail email) Nothing Nothing
121133
122134instance Arbitrary MandrillRecipient where
123135 arbitrary = pure MandrillRecipient {
124− _mrec_email = fromJust (emailAddress "test@example.com")
136+ _mrec_email = MandrillEmail $ fromJust (emailAddress "test@example.com")
125137 , _mrec_name = Nothing
126138 , _mrec_type = Nothing
127139 }
@@ -218,7 +230,7 @@ data MandrillMessage = MandrillMessage {
218230 -- ^ Optional full text content to be sent
219231 , _mmsg_subject :: !T.Text
220232 -- ^ The message subject
221− , _mmsg_from_email :: EmailAddress
233+ , _mmsg_from_email :: MandrillEmail
222234 -- ^ The sender email address
223235 , _mmsg_from_name :: Maybe T.Text
224236 -- ^ Optional from name to be used
@@ -301,7 +313,7 @@ instance Arbitrary MandrillMessage where
301313 arbitrary = MandrillMessage <$> arbitrary
302314 <*> pure Nothing
303315 <*> pure "Test Subject"
304− <*> pure (fromJust $ emailAddress "sender@example.com")
316+ <*> pure (MandrillEmail . fromJust $ emailAddress "sender@example.com")
305317 <*> pure Nothing
306318 <*> resize 2 arbitrary
307319 <*> pure emptyObject