validateEveryToJSON Verify that every type used with JSON content type in a servant API
has compatible ToJSON and ToSchema instances using validateToJSON.
NOTE: validateEveryToJSON does not perform string pattern validation.
See validateEveryToJSONWithPatternChecker.
validateEveryToJSON will produce one prop specification for every type in the API.
Each type only gets one test, even if it occurs multiple times in the API.
data User = User { name :: String, age :: Maybe Int } deriving (Show, Generic, Typeable)newtype UserId = UserId String deriving (Show, Generic, Typeable, ToJSON, Arbitrary)instance ToJSON Userinstance ToSchema Userinstance ToSchema UserIdinstance Arbitrary User where arbitrary = User <$> arbitrary <*> arbitrarytype UserAPI = (Capture "user_id" UserId :> Get '[JSON] User) :<|> (ReqBody '[JSON] User :> Post '[JSON] UserId)
hspec $ context "ToJSON matches ToSchema" $ validateEveryToJSON (Proxy :: Proxy UserAPI)ToJSON matches ToSchema User...... UserId......Finished in ... seconds...2 examples, 0 failures...
For the test to compile all body types should have the following instances:
Typeableis used to name the test for each type;Showis used to display value for whichToJSONdoes not satisfyToSchema.Arbitraryis used to arbitrarily generate values.
If any of the instances is missing, you'll get a descriptive type error:
data Contact = Contact { fullname :: String, phone :: Integer } deriving (Show, Generic)instance ToJSON Contactinstance ToSchema Contacttype ContactAPI = Get '[JSON] Contacthspec $ validateEveryToJSON (Proxy :: Proxy ContactAPI)......No instance for (Arbitrary Contact)... arising from a use of ‘validateEveryToJSON’...