hjsonschema-0.7.0.0: JSON Schema library

Safe HaskellNone
LanguageHaskell2010

Data.JsonSchema

Contents

Synopsis

Primary API

compile :: forall err. Spec err -> Graph -> RawSchema -> Schema err Source

Schemas

newtype Spec err Source

Constructors

Spec 

Fields

_unSpec :: HashMap Text (ValSpec err)
 

newtype Schema err Source

Constructors

Schema 

Fields

_unSchema :: [Value -> [ValidationFailure err]]
 

data RawSchema Source

Constructors

RawSchema 

type Graph = HashMap Text (HashMap Text Value) Source

A mapping of URLs to schemas.

Each key/value pair provides the components of a RawSchema.

Validators

type EmbeddedSchemas = Text -> Value -> Vector RawSchema Source

Return a schema's immediate subschemas.

This is used by fetchRefs to find all the subschemas in a document. This allows it to process only "$ref"s and "id"s that are actual schema keywords.

type ValidatorConstructor schemaErr valErr = Spec schemaErr -> Graph -> RawSchema -> Value -> Maybe (Value -> valErr) Source

This is what's used to write most validators in practice.

Its important that particular validators don't know about the error sum type of the Spec they're going to be used in. That way they can be included in other Specs later without encouraging partial functions.

This means that a properly written ValidatorConstructor will need its error type modified for use in a Spec. Data.JsonSchema.Helpers provides giveName and modifyName for this purpose.

Schema feching tools

fetchReferencedSchemas :: Spec err -> RawSchema -> Graph -> IO (Either Text Graph) Source

Take a schema. Retrieve every document either it or its subschemas include via the "$ref" keyword. Load a Graph out with them.

TODO: This function's URL processing is hacky and needs improvement.

fetchReferencedSchemas' :: forall t e m err. (IsString t, MonadError t e, Traversable e, MonadIO m) => (Text -> m (e ByteString)) -> Spec err -> RawSchema -> Graph -> m (e Graph) Source

A version of fetchReferencedSchemas where the function to make requests is provided by the user. This allows restrictions to be added, e.g. rejecting non-local URLs.

Draft 4 specific

isValidSchema :: RawSchema -> [ValidationFailure Draft4Failure] Source

Check if a RawSchema is valid Draft 4 schema.

This is just a convenience function built by preloading validate with the spec schema that describes valid Draft 4 schemas.

NOTE: It's not actually required to run isValidSchema on prospective draft 4 schemas at all. However, it's a good way to catch unintentional mistakes in schema documents.

compileDraft4 :: Graph -> RawSchema -> Either [ValidationFailure Draft4Failure] (Schema Draft4Failure) Source

Check that a RawSchema conforms to the JSON Schema Draft 4 master schema document. Compile it if it does.

This is just a convenience function built by combining isValidSchema and compile.

NOTE: It's not actually required to run isValidSchema on prospective draft 4 schemas at all.