validation - How to validate JSON with schema in Haskell? -
i want validate json schema. hjsonschema seemed choice new , supports latest draft. plotly json schema gives me valid responses.
i may misunderstanding here should not valid json
bad.json
{ "fjsdklj" : 5 }
even though considered valid following code
module main import control.applicative import data.aeson import data.hashmap.strict (hashmap) import qualified data.hashmap.strict h import data.monoid import qualified data.bytestring.lazy b import qualified data.jsonschema js import data.maybe main :: io () main = schemajson <- (fromjust . decode) <$> b.readfile "simple-schema.json" bad <- (fromjust . decode) <$> b.readfile "bad.json" let schemadata = js.rawschema { js._rsuri = nothing, js._rsdata = schemajson } schema <- compileschema (js.schemagraph schemadata h.empty) schemadata checkresults (js.validate schema bad) compileschema :: js.schemagraph -> js.rawschema -> io (js.schema js.draft4failure) compileschema graph rs = case js.compiledraft4 graph rs of left failure -> error $ "not valid schema: " <> show failure right schema -> return schema checkresults :: [js.validationfailure js.draft4failure] -> io () checkresults [] = putstrln "just fine" checkresults x = putstrln $ "error: " ++ show x
simple-schema.json
plotly schema , bad.json
snippet posted above.
it's nothing haskell.
your schema doesn't have required
property @ top level empty json object acceptable.
also contains no "additionalproperties": false
property not fit 1 of defined patterns ignored.
btw doubt whether it's valid draft4 json schema. passed validation of draft4 meta-schema syntax little bit different, maybe python-specific. you'd better run test suites came along hjsonscheme
package see if works fine.
Comments
Post a Comment