Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 16 additions & 4 deletions src/Data/OpenApi/Internal/Schema/Validation.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import Prelude ()
import Prelude.Compat

import Control.Applicative
import Control.Lens hiding (allOf)
import Control.Lens hiding (allOf, anyOf)
import Control.Monad (forM, forM_, when)

import Data.Aeson hiding (Result)
Expand Down Expand Up @@ -480,13 +480,20 @@ inferParamSchemaTypes sch = concat
, has (pattern._Just) ] ]
]

-- compute number of passing variants and variants
countPassingVariants :: Value -> [Referenced Schema] -> Validation s Int
countPassingVariants val variants = do
res <- forM variants $ \var ->
True <$ validateWithSchemaRef var val <|> return False
pure $ length $ filter id res


validateSchemaType :: Value -> Validation Schema ()
validateSchemaType val = withSchema $ \sch ->
case sch of
(view oneOf -> Just variants) -> do
res <- forM variants $ \var ->
(True <$ validateWithSchemaRef var val) <|> (return False)
case length $ filter id res of
npassing <- countPassingVariants val variants
case npassing of
0 -> invalid $ "Value not valid under any of 'oneOf' schemas: " ++ show val
1 -> valid
_ -> invalid $ "Value matches more than one of 'oneOf' schemas: " ++ show val
Expand All @@ -495,6 +502,11 @@ validateSchemaType val = withSchema $ \sch ->
-- variant does not match.
forM_ variants $ \var ->
validateWithSchemaRef var val
(view anyOf -> Just variants) -> do
npassing <- countPassingVariants val variants
case npassing of
0 -> invalid $ "Value not valid under any of 'anyOf' schemas: " ++ show val
_ -> valid

_ ->
case (sch ^. type_, val) of
Expand Down