@@ -1,4 +1,5 @@
1{-# OPTIONS_GHC -Wno-orphans #-}
1{-# OPTIONS_GHC -Wno-orphans #-}
2module Data.OpenApi.Compare.Validate.Schema
3module Data.OpenApi.Compare.Validate.Schema
@@ -10,9 +11,9 @@ import Data.Coerce
10import Data.Foldable (for_, toList)
11import Data.Foldable (for_, toList)
11import Data.Functor
12import Data.Functor
12import Data.HList
13import Data.HList
14+import Data.List (genericIndex, genericLength, group)
13import qualified Data.List.NonEmpty as NE
15import qualified Data.List.NonEmpty as NE
14import qualified Data.Map as M
16import qualified Data.Map as M
15โimport Data.List (group, genericLength, genericIndex)
16import Data.Maybe
17import Data.Maybe
17import Data.OpenApi
18import Data.OpenApi
18import Data.OpenApi.Compare.Behavior
19import Data.OpenApi.Compare.Behavior
@@ -22,24 +23,24 @@ import Data.OpenApi.Compare.Subtree
22import Data.OpenApi.Compare.Validate.Schema.DNF
23import Data.OpenApi.Compare.Validate.Schema.DNF
23import Data.OpenApi.Compare.Validate.Schema.Issues
24import Data.OpenApi.Compare.Validate.Schema.Issues
24import Data.OpenApi.Compare.Validate.Schema.JsonFormula
25import Data.OpenApi.Compare.Validate.Schema.JsonFormula
25โimport Data.OpenApi.Compare.Validate.Schema.Traced
26โimport Data.OpenApi.Compare.Validate.Schema.TypedJson
27import Data.OpenApi.Compare.Validate.Schema.Partition
26import Data.OpenApi.Compare.Validate.Schema.Partition
28import Data.OpenApi.Compare.Validate.Schema.Process
27import Data.OpenApi.Compare.Validate.Schema.Process
28+import Data.OpenApi.Compare.Validate.Schema.Traced
29+import Data.OpenApi.Compare.Validate.Schema.TypedJson
29import Data.Ord
30import Data.Ord
30import Data.Ratio
31import Data.Ratio
31import Data.Semigroup
32import Data.Semigroup
32import qualified Data.Set as S
33import qualified Data.Set as S
33import Data.Text (Text)
34import Data.Text (Text)
35โcheckFormulas
36+checkFormulas ::
36โ :: (ReassembleHList xs (CheckEnv (Referenced Schema)))
37+ (ReassembleHList xs (CheckEnv (Referenced Schema))) =>
37โ => HList xs
38+ HList xs ->
38โ -> Behavior 'SchemaLevel
39+ Behavior 'SchemaLevel ->
39โ -> ProdCons (Trace Schema)
40+ ProdCons (Trace Schema) ->
40โ -> ProdCons (Traced (Definitions Schema))
41+ ProdCons (Traced (Definitions Schema)) ->
41โ -> ProdCons (ForeachType JsonFormula, P.PathsPrefixTree Behave AnIssue 'SchemaLevel)
42+ ProdCons (ForeachType JsonFormula, P.PathsPrefixTree Behave AnIssue 'SchemaLevel) ->
42โ -> SemanticCompatFormula ()
43+ SemanticCompatFormula ()
43checkFormulas env beh trs defs (ProdCons (fp, ep) (fc, ec)) =
44checkFormulas env beh trs defs (ProdCons (fp, ep) (fc, ec)) =
44 case P.toList ep ++ P.toList ec of
45 case P.toList ep ++ P.toList ec of
45 issues@(_ : _) -> for_ issues $ embedFormula beh . anItem
46 issues@(_ : _) -> for_ issues $ embedFormula beh . anItem
@@ -144,21 +145,21 @@ checkFormulas env beh trs defs (ProdCons (fp, ep) (fc, ec)) =
144 EnumDoesntSatisfy $ untypeValue e -- what does this look like when partitioned?
145 EnumDoesntSatisfy $ untypeValue e -- what does this look like when partitioned?
145 issueFromDisjunct mPart ps = NoMatchingCondition mPart $ SomeCondition <$> S.toList ps
146 issueFromDisjunct mPart ps = NoMatchingCondition mPart $ SomeCondition <$> S.toList ps
147โcheckContradiction
148+checkContradiction ::
148โ :: Behavior 'TypedSchemaLevel
149+ Behavior 'TypedSchemaLevel ->
149โ -> Maybe Partition
150+ Maybe Partition ->
150โ -> S.Set (Condition t)
151+ S.Set (Condition t) ->
151โ -> SemanticCompatFormula ()
152+ SemanticCompatFormula ()
152checkContradiction beh mPart _ = issueAt beh $ maybe TypeBecomesEmpty PartitionBecomesEmpty mPart -- TODO #70
153checkContradiction beh mPart _ = issueAt beh $ maybe TypeBecomesEmpty PartitionBecomesEmpty mPart -- TODO #70
154โcheckImplication
155+checkImplication ::
155โ :: (ReassembleHList xs (CheckEnv (Referenced Schema)))
156+ (ReassembleHList xs (CheckEnv (Referenced Schema))) =>
156โ => HList xs
157+ HList xs ->
157โ -> Behavior 'TypedSchemaLevel
158+ Behavior 'TypedSchemaLevel ->
158โ -> ProdCons (Trace Schema) -- the traces of the root schemas used in this comparison
159+ ProdCons (Trace Schema) -> -- the traces of the root schemas used in this comparison
159โ -> S.Set (Condition t)
160+ S.Set (Condition t) ->
160โ -> Condition t
161+ Condition t ->
161โ -> SemanticCompatFormula ()
162+ SemanticCompatFormula ()
162checkImplication env beh trs prods cons = case findExactly prods of
163checkImplication env beh trs prods cons = case findExactly prods of
164 | all (satisfiesTyped e) prods ->
165 | all (satisfiesTyped e) prods ->
@@ -166,124 +167,104 @@ checkImplication env beh trs prods cons = case findExactly prods of
166 then pure ()
167 then pure ()
167 else issueAt beh (EnumDoesntSatisfy $ untypeValue e)
168 else issueAt beh (EnumDoesntSatisfy $ untypeValue e)
168 | otherwise -> pure () -- vacuously true
169 | otherwise -> pure () -- vacuously true
170 Nothing -> case cons of
170 Nothing -> case cons of
171 -- the above code didn't catch it, so there's no Exactly condition on the lhs
171 -- the above code didn't catch it, so there's no Exactly condition on the lhs
172 Exactly e -> issueAt beh (NoMatchingEnum $ untypeValue e)
172 Exactly e -> issueAt beh (NoMatchingEnum $ untypeValue e)
174 Maximum m -> foldCheck min m NoMatchingMaximum MatchingMaximumWeak $ \case
173 Maximum m -> foldCheck min m NoMatchingMaximum MatchingMaximumWeak $ \case
175 Maximum m' -> Just m'
174 Maximum m' -> Just m'
176 _ -> Nothing
175 _ -> Nothing
178 Minimum m -> foldCheck max m (NoMatchingMinimum . coerce) (MatchingMinimumWeak . coerce) $ \case
176 Minimum m -> foldCheck max m (NoMatchingMinimum . coerce) (MatchingMinimumWeak . coerce) $ \case
179 Minimum m' -> Just m'
177 Minimum m' -> Just m'
180 _ -> Nothing
178 _ -> Nothing
182 MultipleOf m -> foldCheck lcmScientific m NoMatchingMultipleOf MatchingMultipleOfWeak $ \case
179 MultipleOf m -> foldCheck lcmScientific m NoMatchingMultipleOf MatchingMultipleOfWeak $ \case
183 MultipleOf m' -> Just m'
180 MultipleOf m' -> Just m'
184 _ -> Nothing
181 _ -> Nothing
186 NumberFormat f -> case flip any prods $ \case
182 NumberFormat f -> case flip any prods $ \case
187โ NumberFormat f' -> f == f'
183+ NumberFormat f' -> f == f'
188โ _ -> False
184+ _ -> False of
190 True -> pure ()
185 True -> pure ()
191 False -> issueAt beh (NoMatchingFormat f)
186 False -> issueAt beh (NoMatchingFormat f)
193 MaxLength m -> foldCheck min m NoMatchingMaxLength MatchingMaxLengthWeak $ \case
187 MaxLength m -> foldCheck min m NoMatchingMaxLength MatchingMaxLengthWeak $ \case
194 MaxLength m' -> Just m'
188 MaxLength m' -> Just m'
195 _ -> Nothing
189 _ -> Nothing
197 MinLength m -> foldCheck max m NoMatchingMinLength MatchingMinLengthWeak $ \case
190 MinLength m -> foldCheck max m NoMatchingMinLength MatchingMinLengthWeak $ \case
198 MinLength m' -> Just m'
191 MinLength m' -> Just m'
199 _ -> Nothing
192 _ -> Nothing
201 Pattern p -> case flip any prods $ \case
193 Pattern p -> case flip any prods $ \case
202โ Pattern p' -> p == p'
194+ Pattern p' -> p == p'
203โ _ -> False
195+ _ -> False of
205 True -> pure ()
196 True -> pure ()
206 False -> issueAt beh (NoMatchingPattern p) -- TODO: regex comparison #32
197 False -> issueAt beh (NoMatchingPattern p) -- TODO: regex comparison #32
208 StringFormat f -> case flip any prods $ \case
198 StringFormat f -> case flip any prods $ \case
209โ StringFormat f' -> f == f'
199+ StringFormat f' -> f == f'
210โ _ -> False
200+ _ -> False of
212 True -> pure ()
201 True -> pure ()
213 False -> issueAt beh (NoMatchingFormat f)
202 False -> issueAt beh (NoMatchingFormat f)
215 Items _ cons' -> case foldSome (<>) prods $ \case
203 Items _ cons' -> case foldSome (<>) prods $ \case
216โ Items _ rs -> Just (Just (rs NE.:| []), mempty)
204+ Items _ rs -> Just (Just (rs NE.:| []), mempty)
217โ TupleItems (map snd -> fs) -> Just (mempty, Just (fs NE.:| []))
205+ TupleItems (map snd -> fs) -> Just (mempty, Just (fs NE.:| []))
218โ _ -> Nothing
206+ _ -> Nothing of
220 Just (mItems, Just pfs)
207 Just (mItems, Just pfs)
221 | not $ allSame (length <$> pfs) -> pure () -- vacuously
208 | not $ allSame (length <$> pfs) -> pure () -- vacuously
222โ | let plen = genericLength (NE.head pfs)
209+ | let plen = genericLength (NE.head pfs) ->
223โ -> clarifyIssue (AnItem beh (anIssue TupleToArray)) $ for_ [0 .. plen - 1] $ \i -> do
210+ clarifyIssue (AnItem beh (anIssue TupleToArray)) $
211+ for_ [0 .. plen - 1] $ \i -> do
224โ let prod' = tracedConjunct $ case mItems of
212+ let prod' = tracedConjunct $ case mItems of
225โ Just prods' -> ((`genericIndex` i) <$> pfs) <> prods'
213+ Just prods' -> ((`genericIndex` i) <$> pfs) <> prods'
226โ Nothing -> (`genericIndex` i) <$> pfs
214+ Nothing -> (`genericIndex` i) <$> pfs
227โ checkCompatibility (beh >>> step (InItem i)) env $ ProdCons prod' cons'
215+ checkCompatibility (beh >>> step (InItem i)) env $ ProdCons prod' cons'
228 Just (Just prods', Nothing) -> do
216 Just (Just prods', Nothing) -> do
229 let prod' = tracedConjunct prods'
217 let prod' = tracedConjunct prods'
230 checkCompatibility (beh >>> step InItems) env $ ProdCons prod' cons'
218 checkCompatibility (beh >>> step InItems) env $ ProdCons prod' cons'
231 _ -> clarifyIssue (AnItem beh (anIssue NoMatchingItems)) $ do
219 _ -> clarifyIssue (AnItem beh (anIssue NoMatchingItems)) $ do
232 checkCompatibility (beh >>> step InItems) env $ ProdCons prodTopSchema cons'
220 checkCompatibility (beh >>> step InItems) env $ ProdCons prodTopSchema cons'
234 TupleItems (map snd -> fs) -> case foldSome (<>) prods $ \case
221 TupleItems (map snd -> fs) -> case foldSome (<>) prods $ \case
235โ TupleItems (map snd -> fs') -> Just (Just $ fs' NE.:| [], Just . Max $ genericLength fs', Just . Min $ genericLength fs', mempty)
222+ TupleItems (map snd -> fs') -> Just (Just $ fs' NE.:| [], Just . Max $ genericLength fs', Just . Min $ genericLength fs', mempty)
236โ MinItems m' -> Just (mempty, Just . Max $ m', mempty, mempty)
223+ MinItems m' -> Just (mempty, Just . Max $ m', mempty, mempty)
237โ MaxItems m' -> Just (mempty, mempty, Just . Min $ m', mempty)
224+ MaxItems m' -> Just (mempty, mempty, Just . Min $ m', mempty)
238โ Items _ rs -> Just (mempty, mempty, mempty, Just (rs NE.:| []))
225+ Items _ rs -> Just (mempty, mempty, mempty, Just (rs NE.:| []))
239โ _ -> Nothing
226+ _ -> Nothing of
241โ -- if the length constraints in the producer are contradictory:
227+ -- if the length constraints in the producer are contradictory:
242โ Just (_, Just (Max lowest), Just (Min highest), _) | lowest > highest -> pure ()
228+ Just (_, Just (Max lowest), Just (Min highest), _) | lowest > highest -> pure ()
243โ -- We have an explicit tuple items clause...
229+ -- We have an explicit tuple items clause...
244โ Just (Just pfs, Just (Max plen), _, _)
230+ Just (Just pfs, Just (Max plen), _, _)
245โ | plen /= genericLength fs -- ...of wrong length
231+ | plen /= genericLength fs -> -- ...of wrong length
246โ -> issueAt beh (TupleItemsLengthChanged ProdCons {producer = plen, consumer = genericLength fs})
232+ issueAt beh (TupleItemsLengthChanged ProdCons {producer = plen, consumer = genericLength fs})
247โ | otherwise
233+ | otherwise ->
248โ -> for_ [0 .. plen - 1] $ \i -> do
234+ for_ [0 .. plen - 1] $ \i -> do
249 checkCompatibility (beh >>> step (InItem i)) env $ ProdCons (tracedConjunct $ (`genericIndex` i) <$> pfs) (fs `genericIndex` i)
235 checkCompatibility (beh >>> step (InItem i)) env $ ProdCons (tracedConjunct $ (`genericIndex` i) <$> pfs) (fs `genericIndex` i)
250โ -- We have a fixed length array in the producer...
236+ -- We have a fixed length array in the producer...
251โ Just (Nothing, Just (Max plen), Just (Min plen'), mProd)
237+ Just (Nothing, Just (Max plen), Just (Min plen'), mProd)
252โ | plen == plen'
238+ | plen == plen' ->
253โ -> clarifyIssue (AnItem beh (anIssue ArrayToTuple)) $ case mProd of
239+ clarifyIssue (AnItem beh (anIssue ArrayToTuple)) $ case mProd of
254โ _ | plen /= genericLength fs -> -- ...of wrong length
241+ | plen /= genericLength fs -> -- ...of wrong length
255โ issueAt beh (TupleItemsLengthChanged ProdCons {producer = plen, consumer = genericLength fs})
242+ issueAt beh (TupleItemsLengthChanged ProdCons {producer = plen, consumer = genericLength fs})
256 Just rs -> for_ [0 .. plen - 1] $ \i -> do
243 Just rs -> for_ [0 .. plen - 1] $ \i -> do
257 checkCompatibility (beh >>> step (InItem i)) env $ ProdCons (tracedConjunct rs) (fs `genericIndex` i)
244 checkCompatibility (beh >>> step (InItem i)) env $ ProdCons (tracedConjunct rs) (fs `genericIndex` i)
258 -- ...and no "items" schema
245 -- ...and no "items" schema
259 Nothing -> clarifyIssue (AnItem beh (anIssue NoMatchingTupleItems)) $ do
246 Nothing -> clarifyIssue (AnItem beh (anIssue NoMatchingTupleItems)) $ do
260 for_ [0 .. plen - 1] $ \i -> do
247 for_ [0 .. plen - 1] $ \i -> do
261 checkCompatibility (beh >>> step (InItem i)) env $ ProdCons prodTopSchema (fs `genericIndex` i)
248 checkCompatibility (beh >>> step (InItem i)) env $ ProdCons prodTopSchema (fs `genericIndex` i)
262โ _ -> issueAt beh NoMatchingTupleItems
249+ _ -> issueAt beh NoMatchingTupleItems
264 MaxItems m -> foldCheck min m NoMatchingMaxItems MatchingMaxItemsWeak $ \case
250 MaxItems m -> foldCheck min m NoMatchingMaxItems MatchingMaxItemsWeak $ \case
265 MaxItems m' -> Just m'
251 MaxItems m' -> Just m'
266 TupleItems fs -> Just $ toInteger $ length fs
252 TupleItems fs -> Just $ toInteger $ length fs
267 _ -> Nothing
253 _ -> Nothing
269 MinItems m -> foldCheck max m NoMatchingMinItems MatchingMinItemsWeak $ \case
254 MinItems m -> foldCheck max m NoMatchingMinItems MatchingMinItemsWeak $ \case
270 MinItems m' -> Just m'
255 MinItems m' -> Just m'
271 TupleItems fs -> Just $ toInteger $ length fs
256 TupleItems fs -> Just $ toInteger $ length fs
272 _ -> Nothing
257 _ -> Nothing
274 UniqueItems -> case flip any prods $ \case
258 UniqueItems -> case flip any prods $ \case
275โ UniqueItems -> True
259+ UniqueItems -> True
276โ MaxItems 1 -> True
260+ MaxItems 1 -> True
277โ TupleItems fs | length fs == 1 -> True
261+ TupleItems fs | length fs == 1 -> True
278โ _ -> False
262+ _ -> False of
280 True -> pure ()
263 True -> pure ()
281 False -> issueAt beh NoMatchingUniqueItems
264 False -> issueAt beh NoMatchingUniqueItems
283 Properties props _ madd -> case foldSome (<>) prods $ \case
265 Properties props _ madd -> case foldSome (<>) prods $ \case
284โ Properties props' _ madd' -> Just $ (props', madd') NE.:| []
266+ Properties props' _ madd' -> Just $ (props', madd') NE.:| []
285โ _ -> Nothing
267+ _ -> Nothing of
287 Just pm ->
268 Just pm ->
288 anyOfAt beh NoMatchingProperties $ -- TODO: could first "concat" the lists
269 anyOfAt beh NoMatchingProperties $ -- TODO: could first "concat" the lists
289 NE.toList pm <&> \(props', madd') -> do
270 NE.toList pm <&> \(props', madd') -> do
@@ -314,11 +295,9 @@ checkImplication env beh trs prods cons = case findExactly prods of
314 (Just add', Just add) -> checkCompatibility (beh >>> step InAdditionalProperty) env (ProdCons add' add)
295 (Just add', Just add) -> checkCompatibility (beh >>> step InAdditionalProperty) env (ProdCons add' add)
316 Nothing -> issueAt beh NoMatchingProperties
297 Nothing -> issueAt beh NoMatchingProperties
318 MaxProperties m -> foldCheck min m NoMatchingMaxProperties MatchingMaxPropertiesWeak $ \case
298 MaxProperties m -> foldCheck min m NoMatchingMaxProperties MatchingMaxPropertiesWeak $ \case
319 MaxProperties m' -> Just m'
299 MaxProperties m' -> Just m'
320 _ -> Nothing
300 _ -> Nothing
322 MinProperties m -> foldCheck max m NoMatchingMinProperties MatchingMinPropertiesWeak $ \case
301 MinProperties m -> foldCheck max m NoMatchingMinProperties MatchingMinPropertiesWeak $ \case
323 MinProperties m' -> Just m'
302 MinProperties m' -> Just m'
324 _ -> Nothing
303 _ -> Nothing
@@ -326,14 +305,14 @@ checkImplication env beh trs prods cons = case findExactly prods of
326 lcmScientific (toRational -> a) (toRational -> b) =
305 lcmScientific (toRational -> a) (toRational -> b) =
327 fromRational $ lcm (numerator a) (numerator b) % gcd (denominator a) (denominator b)
306 fromRational $ lcm (numerator a) (numerator b) % gcd (denominator a) (denominator b)
329โ foldCheck
308+ foldCheck ::
330โ :: Eq a
309+ Eq a =>
331โ => (a -> a -> a)
310+ (a -> a -> a) ->
333โ -> (a -> Issue 'TypedSchemaLevel)
312+ (a -> Issue 'TypedSchemaLevel) ->
334โ -> (ProdCons a -> Issue 'TypedSchemaLevel)
313+ (ProdCons a -> Issue 'TypedSchemaLevel) ->
335โ -> (forall t. Condition t -> Maybe a)
314+ (forall t. Condition t -> Maybe a) ->
336โ -> SemanticCompatFormula ()
315+ SemanticCompatFormula ()
337 foldCheck f m missing weak extr = case foldSome f prods extr of
316 foldCheck f m missing weak extr = case foldSome f prods extr of
339 | f m' m == m' -> pure ()
318 | f m' m == m' -> pure ()