HORIZON HASKELLDocslts/ghc-9.10.xc74966e2026-09-27Search names, modules, packages, or :: a typeCtrl K

GHC 9.10.3 · lts/ghc-9.10.x · c74966e · 2026-09-27

Moduleopenapi3-3.2.4Haskell2010

Data.OpenApi.Schema.Validation

Validate JSON values with Swagger Schema.

  • 1 type
  • 6 values
  • Packageopenapi3-3.2.4
  • Exports7
  • LanguageHaskell2010
  • LicenceBSD-3-Clause
  • SourceValidation.hs

How to use validation

0 declarations

This 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

Example1 expression
validateToJSON "hello"[]
Example1 expression
validateToJSON False[]
Example5 expressions
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:

Example3 expressions
validateToJSON (Nothing :: Maybe String)["expected JSON value of type OpenApiString"]validateToJSON ([Just "hello", Nothing] :: [Maybe String])["expected JSON value of type OpenApiString"]validateToJSON (123, Nothing :: Maybe String)["expected JSON value of type OpenApiString"]

However, when Maybe a is a type of a record field, validation takes required property of the Schema into account:

Example5 expressions
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 declaration

Using ToJSON and ToSchema

Pretty print validation errors together with actual JSON and Swagger Schema (using encodePretty).

Example10 expressions
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": "#/components/schemas/Phone"        }    },    "required": [        "name",        "phone"    ],    "type": "object"}Swagger Description Context:{    "Phone": {        "properties": {            "value": {                "type": "string"            }        },        "required": [            "value"        ],        "type": "object"    }}

Using Value and Schema