schematic 0.1.0.0 → 0.1.1.0
raw patch · 4 files changed
+47/−11 lines, 4 filesdep +bytestringdep −deriving-compatPVP ok
version bump matches the API change (PVP)
Dependencies added: bytestring
Dependencies removed: deriving-compat
API changes (from Hackage documentation)
+ Data.Schematic: DecodingError :: Text -> ParseResult a
+ Data.Schematic: Valid :: a -> ParseResult a
+ Data.Schematic: ValidationError :: ErrorMap -> ParseResult a
+ Data.Schematic: data ParseResult a
+ Data.Schematic: decodeAndValidateJson :: forall schema. (FromJSON (JsonRepr schema), TopLevel schema, Known (Sing schema)) => ByteString -> ParseResult (JsonRepr schema)
+ Data.Schematic: isDecodingError :: ParseResult a -> Bool
+ Data.Schematic: isValid :: ParseResult a -> Bool
+ Data.Schematic: isValidationError :: ParseResult a -> Bool
+ Data.Schematic.Validation: isDecodingError :: ParseResult a -> Bool
+ Data.Schematic.Validation: isValid :: ParseResult a -> Bool
+ Data.Schematic.Validation: isValidationError :: ParseResult a -> Bool
Files
- schematic.cabal +3/−2
- src/Data/Schematic.hs +15/−0
- src/Data/Schematic/Validation.hs +12/−0
- test/SchemaSpec.hs +17/−9
schematic.cabal view
@@ -2,7 +2,7 @@ -- documentation, see http://haskell.org/cabal/users-guide/ name: schematic-version: 0.1.0.0+version: 0.1.1.0 synopsis: JSON-biased spec and validation tool -- description: license: BSD3@@ -56,8 +56,8 @@ , TypeSynonymInstances , UndecidableInstances build-depends: base >=4.9 && <4.10+ , bytestring , aeson >= 1- , deriving-compat , regex-compat , scientific , singletons@@ -105,6 +105,7 @@ build-depends: HUnit , aeson >= 1 , base >=4.9 && <4.10+ , bytestring , hspec >= 2.2.0 , hspec-core , hspec-discover
src/Data/Schematic.hs view
@@ -1,12 +1,18 @@ module Data.Schematic ( module Data.Schematic.Schema , module Data.Schematic.Utils+ , decodeAndValidateJson , parseAndValidateJson+ , isValid+ , isDecodingError+ , isValidationError+ , ParseResult(..) ) where import Control.Monad.Validation import Data.Aeson as J import Data.Aeson.Types as J+import Data.ByteString.Lazy as BL import Data.Functor.Identity import Data.Schematic.Schema import Data.Schematic.Utils@@ -29,3 +35,12 @@ in case res of Left em -> ValidationError em Right () -> Valid jsonRepr++decodeAndValidateJson+ :: forall schema+ . (J.FromJSON (JsonRepr schema), TopLevel schema, Known (Sing schema))+ => BL.ByteString+ -> ParseResult (JsonRepr schema)+decodeAndValidateJson bs = case decode bs of+ Nothing -> DecodingError "invalid json"+ Just x -> parseAndValidateJson x
src/Data/Schematic/Validation.hs view
@@ -28,6 +28,18 @@ | ValidationError ErrorMap deriving (Show, Eq, Functor) +isValid :: ParseResult a -> Bool+isValid (Valid _) = True+isValid _ = False++isDecodingError :: ParseResult a -> Bool+isDecodingError (DecodingError _) = True+isDecodingError _ = False++isValidationError :: ParseResult a -> Bool+isValidationError (ValidationError _) = True+isValidationError _ = False+ validateTextConstraint :: JSONPath -> Text
test/SchemaSpec.hs view
@@ -1,6 +1,7 @@ module SchemaSpec (spec, main) where import Control.Monad+import Data.ByteString.Lazy import Data.Aeson import Data.Proxy import Data.Schematic@@ -15,7 +16,7 @@ type SchemaExample = SchemaObject '[ '("foo", SchemaArray '[AEq 1] (SchemaNumber '[NGt 10]))- , '("bar", SchemaOptional (SchemaText '[TRegex "\\w+"]))]+ , '("bar", SchemaOptional (SchemaText '[TRegex "\\w+", TEnum '["foo", "bar"]]))] exampleTest :: JsonRepr (SchemaOptional (SchemaText '[TEq 3])) exampleTest = ReprOptional (Just (ReprText "lil"))@@ -26,26 +27,33 @@ exampleArray :: JsonRepr (SchemaArray '[AEq 1] (SchemaNumber '[NGt 10])) exampleArray = ReprArray [exampleNumber] -exampleObject :: JsonRepr SchemaExample-exampleObject = ReprObject $ FieldRepr exampleArray- :& FieldRepr (ReprOptional (Just (ReprText "barval")))- :& RNil- jsonExample :: JsonRepr SchemaExample jsonExample = ReprObject $ FieldRepr (ReprArray [ReprNumber 12])- :& FieldRepr (ReprOptional (Just (ReprText "tes")))+ :& FieldRepr (ReprOptional (Just (ReprText "bar"))) :& RNil --- schemaExample :: Sing SchemaExample--- schemaExample = known+schemaJson :: ByteString+schemaJson = "{\"foo\": [13], \"bar\": null}" +schemaJson2 :: ByteString+schemaJson2 = "{\"foo\": [3], \"bar\": null}"+ spec :: Spec spec = do -- it "show/read JsonRepr properly" $ -- read (show example) == example it "decode/encode JsonRepr properly" $ decode (encode jsonExample) == Just jsonExample+ it "validates correct representation" $+ ((decodeAndValidateJson schemaJson) :: ParseResult (JsonRepr SchemaExample))+ `shouldSatisfy` isValid+ it "returns decoding error on structurally incorrect input" $+ ((decodeAndValidateJson "{}") :: ParseResult (JsonRepr SchemaExample))+ `shouldSatisfy` isDecodingError+ it "validates incorrect representation" $+ ((decodeAndValidateJson schemaJson2) :: ParseResult (JsonRepr SchemaExample))+ `shouldSatisfy` isValidationError main :: IO () main = hspec spec