Validation error message.
Moduleswagger2-2.8.9Haskell2010
Data.Swagger.Schema.Validation
Validate JSON values with Swagger Schema.
- 1 type
- 6 values
- Packageswagger2-2.8.9
- Exports7
- LanguageHaskell2010
- LicenceBSD-3-Clause
- SourceValidation.hs
How to use validation
0 declarationsThis module provides helpful functions for JSON validation. These functions are meant to be used in test suites for your application to ensure that JSON respresentation for your data corresponds to schemas you're using for the Swagger specification.
It is recommended to use validation functions as QuickCheck properties (see http://hackage.haskell.org/package/QuickCheck).
Examples
validateToJSON "hello"[]
validateToJSON False[]
newtype Nat = Nat Integer deriving Genericinstance ToJSON Nat where toJSON (Nat n) = toJSON ninstance ToSchema Nat where declareNamedSchema proxy = genericDeclareNamedSchema defaultSchemaOptions proxy & mapped.minimum_ ?~ 0validateToJSON (Nat 10)[]validateToJSON (Nat (-5))["value -5.0 falls below minimum (should be >=0.0)"]
Validating Maybe
Because Maybe a has the same schema as a, validation
generally fails for null JSON:
validateToJSON (Nothing :: Maybe String)["expected JSON value of type SwaggerString\n with context:\n SwaggerType: Just SwaggerString\n Aeson Value: Null\n Schema title: Nothing\n"]validateToJSON ([Just "hello", Nothing] :: [Maybe String])["expected JSON value of type SwaggerString\n with context:\n SwaggerType: Just SwaggerString\n Aeson Value: Null\n Schema title: Nothing\n"]validateToJSON (123, Nothing :: Maybe String)["expected JSON value of type SwaggerString\n with context:\n SwaggerType: Just SwaggerString\n Aeson Value: Null\n Schema title: Nothing\n"]
However, when Maybe a is a type of a record field,
validation takes property of the required
into account:Schema
data Person = Person { name :: String, age :: Maybe Int } deriving Genericinstance ToJSON Personinstance ToSchema PersonvalidateToJSON (Person "Nick" (Just 24))[]validateToJSON (Person "Nick" Nothing)[]
JSON validation
1 declarationUsing ToJSON and ToSchema
Validate ToJSON instance matches ToSchema for a given value.
This can be used with QuickCheck to ensure those instances are coherent:
validateToJSON (x :: Int) == []NOTE: validateToJSON does not perform string pattern validation.
See validateToJSONWithPatternChecker.
See renderValidationErrors on how the output is structured.
Variant of validatePrettyToJSON with typed output.
Validate ToJSON instance matches ToSchema for a given value and pattern checker.
This can be used with QuickCheck to ensure those instances are coherent.
For validation without patterns see validateToJSON. See also:
renderValidationErrors.
Pretty print validation errors together with actual JSON and Swagger Schema (using encodePretty).
import Data.Aeson as Aesonimport Data.Foldable (traverse_)import GHC.Genericsdata Phone = Phone { value :: String } deriving (Generic)data Person = Person { name :: String, phone :: Phone } deriving (Generic)instance ToJSON Person where toJSON p = object [ "name" Aeson..= name p ]instance ToSchema Phoneinstance ToSchema Personlet person = Person { name = "John", phone = Phone "123456" }traverse_ putStrLn $ renderValidationErrors validateToJSON personValidation against the schema fails: * property "phone" is required, but not found in "{\"name\":\"John\"}"JSON value:{ "name": "John"}Swagger Schema:{ "properties": { "name": { "type": "string" }, "phone": { "$ref": "#/definitions/Phone" } }, "required": [ "name", "phone" ], "type": "object"}Swagger Description Context:{ "Phone": { "properties": { "value": { "type": "string" } }, "required": [ "value" ], "type": "object" }}
Using Value and Schema
Validate JSON Value against Swagger Schema.
validateJSON mempty (toSchema (Proxy :: Proxy Int)) (toJSON (x :: Int)) == []NOTE: validateJSON does not perform string pattern validation.
See validateJSONWithPatternChecker.
Validate JSON Value agains Swagger ToSchema for a given value and pattern checker.
For validation without patterns see validateJSON.