Validation

Validators are provided for validating a SpaceTx fileset against the JSON Schemas.

Validators

class starfish.core.spacetx_format.util.SpaceTxValidator(schema)[source]

Methods

validate_file(target_file)

validate a target file, returning True if valid and False otherwise

validate_object(target_object[, target_file])

validate a loaded json object, returning True if valid, and False otherwise

load_json

validate_file(target_file)[source]

validate a target file, returning True if valid and False otherwise

Parameters
target_filestr

path or URL to a target json object to be validated against the schema passed to this object’s constructor

Returns
bool :

True, if object valid, else False

Examples

Validate a codebook file:

>>> from pkg_resources import resource_filename
>>> from starfish.core.spacetx_format.util import SpaceTxValidator
>>> schema_path = resource_filename(
        "starfish", "spacetx_format/schema/codebook/codebook.json")
>>> validator = SpaceTxValidator(schema_path)
>>> if not validator.validate_file(your_codebook_filename):
>>>     raise Exception("invalid")
Return type

bool

validate_object(target_object, target_file=None)[source]

validate a loaded json object, returning True if valid, and False otherwise

Parameters
target_objectDict

loaded json object to be validated against the schema passed to this object’s constructor

target_filestr

informational string regarding the source file of the given object

Returns
bool :

True, if object valid, else False

Examples

Validate an experiment json string

>>> from pkg_resources import resource_filename
>>> from starfish.core.spacetx_format.util import SpaceTxValidator
>>> schema_path = resource_filename("starfish", "spacetx_format/schema/experiment.json")
>>> validator = SpaceTxValidator(schema_path)
>>> if not validator.validate_object(your_experiment_object):
>>>     raise Exception("invalid")
Return type

bool

Helpers

In addition, the starfish.spacetx_format.validate_sptx module contains helpers to simplify iterating over the tree of json files and their respective schemas.

starfish.core.spacetx_format.validate_sptx.validate(experiment_json, fuzz=False)[source]

validate a spaceTx formatted experiment. Accepts local filepaths or files hosted at http links. Loads configuration from StarfishConfig.

Parameters
experiment_jsonstr

path or URL to a target json object to be validated against the schema passed to this object’s constructor

fuzzbool

whether or not to perform element-by-element fuzzing. If true, will return true and will not use warnings.

Returns
bool :

True, if object valid or fuzz=True, else False

Examples

The following will read the experiment json file provided, downloading it if necessary, and begin recursively validating it and all referenced json files (e.g. codebook.json):

>>> from starfish.core.spacetx_format import validate_sptx
>>> valid = validate_sptx.validate(json_url)
Return type

bool

starfish.core.spacetx_format.validate_sptx.validate_file(file, schema, fuzz=False, backend=None, output=None)[source]

validate a spaceTx formatted file with a given schema. Accepts local filepaths or files hosted at http links.

Parameters
filestr

path or URL to a target json object to be validated against the schema passed

schemastr

resource path to the schema

backendslicedimage.backends._base.Backend

backend previously loaded from a file path or URL, or potentially None if a new backend should be loaded.

fuzzbool

whether or not to perform element-by-element fuzzing. If true, will return true and will not use warnings.

outputDict

dictionary into which the output object can be stored

Returns
bool :

True, if object valid or fuzz=True, else False

Examples

The following will read the codebook json file provided, downloading it if necessary:

>>> from starfish.core.spacetx_format.validate_sptx import validate_file
>>> valid = validate_sptx.validate_file(json_url, "codebook/codebook.json")
Return type

bool

Error messages

Descriptive error messages are printed as warnings while validation takes place. For example:

starfish/starfish/core/spacetx_format/util.py:82: UserWarning:
 'contents' is a required property
        Schema:                 https://github.com/spacetx/starfish/starfish/file-format/schema/fov-manifest.json
        Subschema level:        0
        Path to error:          required
        Filename:               ../field_of_view/field_of_view.json

  warnings.warn(message)

This message tells you which schema has failed validation (fov-manifest.json), what type of error has been encountered (a required field is missing), and the name of the file which is invalid (field_of_view.json) if it has been provided. Validation of a json object will simply omit the Filename: field.