openapi3-3.2.4: OpenAPI 3.0 data model
Copyright(c) 2015 GetShopTV
LicenseBSD3
MaintainerNickolay Kudasov <nickolay@getshoptv.com>
Stabilityexperimental
Safe HaskellSafe-Inferred
LanguageHaskell2010

Data.OpenApi.Internal.Schema.Validation

Description

Validate JSON values with Swagger Schema.

Synopsis

Documentation

>>> import Data.OpenApi.Internal.Schema.Validation

validatePrettyToJSON :: forall a. (ToJSON a, ToSchema a) => a -> Maybe String Source #

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.

validateToJSON :: forall a. (ToJSON a, ToSchema a) => a -> [ValidationError] Source #

Variant of validatePrettyToJSON with typed output.

validateToJSONWithPatternChecker :: forall a. (ToJSON a, ToSchema a) => (Pattern -> Text -> Bool) -> a -> [ValidationError] Source #

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.

renderValidationErrors :: forall a. (ToJSON a, ToSchema a) => (a -> [ValidationError]) -> a -> Maybe String Source #

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

>>> import Data.Aeson as Aeson
>>> import Data.Foldable (traverse_)
>>> import GHC.Generics
>>> data 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 Phone
>>> instance ToSchema Person
>>> let person = Person { name = "John", phone = Phone "123456" }
>>> traverse_ putStrLn $ renderValidationErrors validateToJSON person
Validation 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"
    }
}

validateJSON :: Definitions Schema -> Schema -> Value -> [ValidationError] Source #

Validate JSON ExpressionOrValue against Swagger Schema.

validateJSON mempty (toSchema (Proxy :: Proxy Int)) (toJSON (x :: Int)) == []

NOTE: validateJSON does not perform string pattern validation. See validateJSONWithPatternChecker.

validateJSONWithPatternChecker :: (Pattern -> Text -> Bool) -> Definitions Schema -> Schema -> Value -> [ValidationError] Source #

Validate JSON ExpressionOrValue agains Swagger ToSchema for a given value and pattern checker.

For validation without patterns see validateJSON.

type ValidationError = String Source #

Validation error message.

data Result a Source #

Validation result type.

Constructors

Failed [ValidationError]

Validation failed with a list of error messages.

Passed a

Validation passed.

Instances

Instances details
Alternative Result Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

empty :: Result a Source #

(<|>) :: Result a -> Result a -> Result a Source #

some :: Result a -> Result [a] Source #

many :: Result a -> Result [a] Source #

Applicative Result Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

pure :: a -> Result a Source #

(<*>) :: Result (a -> b) -> Result a -> Result b Source #

liftA2 :: (a -> b -> c) -> Result a -> Result b -> Result c Source #

(*>) :: Result a -> Result b -> Result b Source #

(<*) :: Result a -> Result b -> Result a Source #

Functor Result Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

fmap :: (a -> b) -> Result a -> Result b Source #

(<$) :: a -> Result b -> Result a Source #

Monad Result Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

(>>=) :: Result a -> (a -> Result b) -> Result b Source #

(>>) :: Result a -> Result b -> Result b Source #

return :: a -> Result a Source #

Show a => Show (Result a) Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Eq a => Eq (Result a) Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

(==) :: Result a -> Result a -> Bool Source #

(/=) :: Result a -> Result a -> Bool Source #

data Config Source #

Validation configuration.

Constructors

Config 

Fields

defaultConfig :: Config Source #

Default Config:

defaultConfig = Config
  { configPatternChecker = \_pattern _str -> True
  , configDefinitions    = mempty
  }

newtype Validation s a Source #

Value validation.

Constructors

Validation 

Fields

Instances

Instances details
Choice Validation Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

left' :: Validation a b -> Validation (Either a c) (Either b c) Source #

right' :: Validation a b -> Validation (Either c a) (Either c b) Source #

Profunctor Validation Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

dimap :: (a -> b) -> (c -> d) -> Validation b c -> Validation a d Source #

lmap :: (a -> b) -> Validation b c -> Validation a c Source #

rmap :: (b -> c) -> Validation a b -> Validation a c Source #

(#.) :: forall a b c q. Coercible c b => q b c -> Validation a b -> Validation a c Source #

(.#) :: forall a b c q. Coercible b a => Validation b c -> q a b -> Validation a c Source #

Alternative (Validation schema) Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

empty :: Validation schema a Source #

(<|>) :: Validation schema a -> Validation schema a -> Validation schema a Source #

some :: Validation schema a -> Validation schema [a] Source #

many :: Validation schema a -> Validation schema [a] Source #

Applicative (Validation schema) Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

pure :: a -> Validation schema a Source #

(<*>) :: Validation schema (a -> b) -> Validation schema a -> Validation schema b Source #

liftA2 :: (a -> b -> c) -> Validation schema a -> Validation schema b -> Validation schema c Source #

(*>) :: Validation schema a -> Validation schema b -> Validation schema b Source #

(<*) :: Validation schema a -> Validation schema b -> Validation schema a Source #

Functor (Validation s) Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

fmap :: (a -> b) -> Validation s a -> Validation s b Source #

(<$) :: a -> Validation s b -> Validation s a Source #

Monad (Validation s) Source # 
Instance details

Defined in Data.OpenApi.Internal.Schema.Validation

Methods

(>>=) :: Validation s a -> (a -> Validation s b) -> Validation s b Source #

(>>) :: Validation s a -> Validation s b -> Validation s b Source #

return :: a -> Validation s a Source #

withSchema :: (s -> Validation s a) -> Validation s a Source #

invalid :: String -> Validation schema a Source #

Issue an error message.

valid :: Validation schema () Source #

Validation passed.

checkMissing :: Validation s () -> Lens' s (Maybe a) -> (a -> Validation s ()) -> Validation s () Source #

Validate schema's property given a lens into that property and property checker.

check :: Lens' s (Maybe a) -> (a -> Validation s ()) -> Validation s () Source #

Validate schema's property given a lens into that property and property checker. If property is missing in schema, consider it valid.

sub :: t -> Validation t a -> Validation s a Source #

Validate same value with different schema.

sub_ :: Getting a s a -> Validation a r -> Validation s r Source #

Validate same value with a part of the original schema.

withRef :: Reference -> (Schema -> Validation s a) -> Validation s a Source #

Validate value against a schema given schema reference and validation function.

inferSchemaTypes :: Schema -> [OpenApiType] Source #

Infer schema type based on used properties.

This is like inferParamSchemaTypes, but also works for objects:

>>> inferSchemaTypes <$> decode "{\"minProperties\": 1}"
Just [OpenApiObject]

inferParamSchemaTypes :: Schema -> [OpenApiType] Source #

Infer schema type based on used properties.

>>> inferSchemaTypes <$> decode "{\"minLength\": 2}"
Just [OpenApiString]
>>> inferSchemaTypes <$> decode "{\"maxItems\": 0}"
Just [OpenApiArray]

From numeric properties OpenApiInteger type is inferred. If you want OpenApiNumber instead, you must specify it explicitly.

>>> inferSchemaTypes <$> decode "{\"minimum\": 1}"
Just [OpenApiInteger]