Skip to content

Commit 5c2d4b1

Browse files
authored
Merge pull request #68 from input-output-hk/js/validator
Implement validator for CBOR
2 parents 74ab360 + e6cecaf commit 5c2d4b1

File tree

3 files changed

+1077
-7
lines changed

3 files changed

+1077
-7
lines changed

bin/Main.hs

Lines changed: 61 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
module Main (main) where
44

55
import Codec.CBOR.Cuddle.CBOR.Gen (generateCBORTerm)
6+
import Codec.CBOR.Cuddle.CBOR.Validator
67
import Codec.CBOR.Cuddle.CDDL (Name (..), sortCDDL)
78
import Codec.CBOR.Cuddle.CDDL.Prelude (prependPrelude)
89
import Codec.CBOR.Cuddle.CDDL.Resolve (
@@ -28,12 +29,21 @@ import Text.Megaparsec (ParseErrorBundle, Parsec, errorBundlePretty, runParser)
2829

2930
data Opts = Opts Command String
3031

31-
newtype ValidateOpts = ValidateOpts {vNoPrelude :: Bool}
32-
3332
data Command
3433
= Format FormatOpts
3534
| Validate ValidateOpts
3635
| GenerateCBOR GenOpts
36+
| ValidateCBOR ValidateCBOROpts
37+
38+
newtype ValidateOpts = ValidateOpts {vNoPrelude :: Bool}
39+
40+
pValidateOpts :: Parser ValidateOpts
41+
pValidateOpts =
42+
ValidateOpts
43+
<$> switch
44+
( long "no-prelude"
45+
<> help "Do not include the CDDL prelude."
46+
)
3747

3848
-- | Various formats for outputtting CBOR
3949
data CBOROutputFormat
@@ -53,6 +63,7 @@ pCBOROutputFormat = eitherReader $ \case
5363
data GenOpts = GenOpts
5464
{ itemName :: T.Text
5565
, outputFormat :: CBOROutputFormat
66+
, outputTo :: Maybe String
5667
, gNoPrelude :: Bool
5768
}
5869

@@ -72,6 +83,13 @@ pGenOpts =
7283
<> help "Output format"
7384
<> value AsCBOR
7485
)
86+
<*> optional
87+
( strOption
88+
( long "out-file"
89+
<> short 'o'
90+
<> help "Write to"
91+
)
92+
)
7593
<*> switch
7694
( long "no-prelude"
7795
<> help "Do not include the CDDL prelude."
@@ -88,10 +106,27 @@ pFormatOpts =
88106
<> help "Sort the CDDL rule definitions before printing."
89107
)
90108

91-
pValidateOpts :: Parser ValidateOpts
92-
pValidateOpts =
93-
ValidateOpts
94-
<$> switch
109+
data ValidateCBOROpts = ValidateCBOROpts
110+
{ vcItemName :: T.Text
111+
, vcInput :: FilePath
112+
, vcNoPrelude :: Bool
113+
}
114+
115+
pValidateCBOROpts :: Parser ValidateCBOROpts
116+
pValidateCBOROpts =
117+
ValidateCBOROpts
118+
<$> strOption
119+
( long "rule"
120+
<> short 'r'
121+
<> metavar "RULE"
122+
<> help "Name of the CDDL rule to validate this file with"
123+
)
124+
<*> strOption
125+
( long "cbor"
126+
<> short 'c'
127+
<> help "CBOR file"
128+
)
129+
<*> switch
95130
( long "no-prelude"
96131
<> help "Do not include the CDDL prelude."
97132
)
@@ -118,6 +153,12 @@ opts =
118153
(GenerateCBOR <$> pGenOpts <**> helper)
119154
(progDesc "Generate a CBOR term matching the schema")
120155
)
156+
<> command
157+
"validate-cbor"
158+
( info
159+
(ValidateCBOR <$> pValidateCBOROpts <**> helper)
160+
(progDesc "Validate a CBOR file against a schema")
161+
)
121162
)
122163
<*> argument str (metavar "CDDL_FILE")
123164

@@ -171,8 +212,21 @@ run (Opts cmd cddlFile) = do
171212
in case outputFormat gOpts of
172213
AsTerm -> print term
173214
AsFlatTerm -> print $ toFlatTerm (encodeTerm term)
174-
AsCBOR -> BSC.putStrLn . Base16.encode . toStrictByteString $ encodeTerm term
215+
AsCBOR -> case outputTo gOpts of
216+
Nothing -> BSC.putStrLn . Base16.encode . toStrictByteString $ encodeTerm term
217+
Just out -> BSC.writeFile out $ toStrictByteString $ encodeTerm term
175218
AsPrettyCBOR -> putStrLn . prettyHexEnc $ encodeTerm term
219+
ValidateCBOR vcOpts ->
220+
let
221+
res'
222+
| vcNoPrelude vcOpts = res
223+
| otherwise = prependPrelude res
224+
in
225+
case fullResolveCDDL res' of
226+
Left err -> putStrLnErr (show err) >> exitFailure
227+
Right mt -> do
228+
cbor <- BSC.readFile (vcInput vcOpts)
229+
validateCBOR cbor (Name (vcItemName vcOpts) mempty) mt
176230

177231
putStrLnErr :: String -> IO ()
178232
putStrLnErr = hPutStrLn stderr

cuddle.cabal

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ library
2323
import: warnings
2424
exposed-modules:
2525
Codec.CBOR.Cuddle.CBOR.Gen
26+
Codec.CBOR.Cuddle.CBOR.Validator
2627
Codec.CBOR.Cuddle.CDDL
2728
Codec.CBOR.Cuddle.CDDL.CtlOp
2829
Codec.CBOR.Cuddle.CDDL.CTree
@@ -53,6 +54,7 @@ library
5354
, data-default-class
5455
, foldable1-classes-compat
5556
, generic-optics
57+
, regex-tdfa
5658
, hashable
5759
, megaparsec
5860
, mtl
@@ -102,6 +104,7 @@ executable cuddle
102104
, optparse-applicative
103105
, prettyprinter
104106
, random
107+
, mtl
105108
, text
106109

107110
test-suite cuddle-test

0 commit comments

Comments
 (0)