hjsonschema-0.8.0.0: JSON Schema library

Safe HaskellNone
LanguageHaskell2010

Data.JsonSchema

Contents

Synopsis

Primary API

compile :: forall err. Spec err -> SchemaGraph -> 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 

Fields

_rsURI :: !(Maybe Text)

NOTE: Must not include a URI fragment.

_rsData :: !(HashMap Text Value)
 

Instances

type SchemaCache = HashMap Text (HashMap Text Value) Source

A set of RawSchemas, split into a HashMap.

Keys correspond to Schema _rsURIs. Values correspond to Schema _rsObjects.

data SchemaGraph Source

Constructors

SchemaGraph 

Fields

_startingSchema :: !RawSchema

The schema initially referenced by '#'.

_cachedSchemas :: !SchemaCache
 

Validators

type EmbeddedSchemas = Maybe Text -> Value -> [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 -> SchemaGraph -> 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.

data ValidationFailure err Source

Constructors

ValidationFailure 

Instances

Schema feching tools

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

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

fetchReferencedSchemas' :: forall m e t err. (MonadIO m, Functor m, MonadError t e, Traversable e, IsString t) => (Text -> m (e ByteString)) -> Spec err -> SchemaCache -> RawSchema -> m (e SchemaGraph) 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 URIs.

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 :: SchemaGraph -> 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.