Validate datasets
With a multitude of metadata possibilities, it is important to manage the consistency of datasets within a scope. To this end, a schema can be added to a scope which can be used to validate datasets. This page explains how to create a schema, validate a dataset against it, and eventually setting it as the default schema for a scope, enabling automatic validation.
Note
Creating schemas and setting the default schema for a scope requires scope admin privileges. Dataset validation can be done by users as well.
Defining a schema
A schema adds requirements on top of the basic QDL dataset requirements. It uses the JSON schema vocabulary to define the required dataset structure json-schema.org. A simple schema is given below.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"title": "measurement dataset",
"properties": {
"name": {
"type": "string",
"minLength": 10,
"maxLength": 50
},
"attributes": {
"additionalProperties": false
}
}
}
The above schema, with title "Measurement dataset", requires all datasets to have a name with at least ten characters, and have at maximum fifty characters and does not accept additional attributes. Note that collections, sequences, and files are exempted from schema validation, these can always be added.
A schema can also define how a string should look like, which attributes should be attached to a dataset, and which of
them are required or not. Note that by defining attributes within the schema, these attributes are then expected to be
added to a dataset. If a user does not comply, dataset creation or updates will be rejected.
Secondly, schemas allow for multiple variations of a dataset which will be accepted. Using this combined with enforced attributes, a schema can quickly become more complex, as shown below.
{
"$schema": "http://json-schema.org/draft-07/schema#",
"type": "object",
"oneOf": [
{
"title": "measurement dataset",
"properties": {
"attributes": {
"type": "object",
"properties": {
"dataset_type": { "const": "measurement" },
"sample": {
"type": "string",
"minLength": 4,
"maxLength": 20
},
"setup": {
"type": "string",
"enum": ["bluefors", "greenpower", "yellowdrive"]
},
"acquisition_software": {
"type": "string"
},
"measuredVariables": {
"type": "array",
"items": {
"type": "string",
"minLength": 2,
"maxLength": 10
}
}
},
"required": ["sample", "setup"],
"additionalProperties": false
},
"name": {
"type": "string",
"minLength": 4,
"maxLength": 20
}
},
"required": ["attributes"]
},
{
"title": "Analysis dataset",
"properties": {
"attributes": {
"dataset_type": { "const": "analysis" },
"additionalProperties": false,
"required": ["dataset_type"]
}
}
},
{
"title": "Simulation dataset",
"properties": {
"name": { "type": "string" },
"attributes": {
"type": "object",
"properties": {
"dataset_type": { "const": "simulation" },
"simulation_code": {
"type": "string"
}
},
"required": ["simulation_code"],
"additionalProperties": false
}
},
"required": ["attributes"]
}
]
}
In above schema, multiple dataset types are defined. Each dataset type has different required attributes and different requirements for each property. Note that such a large complex schema is also error-prone.
Creating a schema
The recommended way of storing a schema is in a JSON file, which can be loaded into Python with the json module.
It is possible to store the schema as a dictionary variable, but as schemas can quickly become rather large, as
demonstrated with the example above, this practice is not recommended.
schema.json
{
"$schema": "http://json-schema.org/draft-07/schema#",
"title": "measurement dataset",
"properties": {
"name": {
"type": "string",
"minLength": 4,
"maxLength": 40
},
"attributes": {
"type": "object",
"properties": {
"compiles": {
"type": "string",
"minLength": 4,
"maxLength": 20
},
"describes": {
"type": "string",
"enum": [
"option_1",
"option_2",
"option_3"
]
}
},
"required": [
"compiles"
],
"additionalProperties": false
}
}
}
To create a schema, an example dataset which complies with it must be provided as well. The same recommendations for the schema apply for the example. Using the schema defined above, a complying example is given below.
example.json
{
"name": "dataset",
"scope": "scope",
"collected": "Wed Aug 23 11:56:27 CEST 2023",
"uid": "123456789",
"uuid": "25DAE302-C503-4634-903E-46422B52AFFC",
"rank": 2,
"attributes": {
"compiles": "value",
"describes": "option_1"
}
}
With the definitions in place, the schema can be added to a scope using the SDK. Keep in mind that this action requires a scope admin to be logged in, setting a token is not sufficient.
from json import load
from qdl import Schema
scope = "<SCOPE_UID>"
name = "<SCHEMA_UID>"
path_schema = "<PATH/TO/SCHEMA>"
path_example = "<PATH/TO/EXAMPLE>"
with open(file=path_schema, mode="r", encoding="utf-8") as file:
dict_schema = load(file)
with open(file=path_example, mode="r", encoding="utf-8") as file:
dict_example = load(file)
schema = Schema(scope_uid=scope, name=name, schema=dict_schema, example=dict_example)
schema.create()
Validate a dataset
A dataset can be validated in three ways:
- Before creating the dataset, the local version can be validated using the
validatemethod with thelocal_validationflag set to true. - The version as it is currently stored in QDL can be validated, in this case the
local_validationflag must be set to false. - Automatically validate the dataset when attempting to update it. This can be done by adding the schema to the
dataset with the
add_schemamethod. Keep in mind that:- It can be overridden with the default schema if it is set.
- It cannot be removed from the dataset in QDL once it is added.
The code snippet below gives examples for all options, with the result is printed as a boolean indicating schema compliance.
from qdl import Dataset
scope_uid = "<SCOPE_UID>"
dataset_uid = "<DATASET_UID>"
schema_uid = "<SCHEMA_UID>"
dataset = Dataset(scope_uid=scope_uid, name=dataset_uid)
print(dataset.validate(schema=schema_uid, local_validation=True))
dataset = Dataset.get(scope_uid=scope_uid, name=dataset_uid)
print(dataset.validate(schema=schema_uid, local_validation=False))
dataset.add_schema(schema_uid)
dataset.update()
print(dataset.schema_validation_result)
Setting a default schema
Validating datasets manually can be convenient when starting with a new schema. However, to ensure that all datasets are validated against the schema, it should be set as the default schema for the scope. With a default schema, different validation levels are possible, with each level being increasingly stricter than the previous one.
Level 0
This is the default level for a scope when it is created. It means that even though the schema is defined, no automatic validation will take place when the dataset is created or updated.
Level 1
Each dataset creation and update will always be validated against the default schema. If the validation is not successful, the dataset creation or update will still be executed. From this level onwards, datasets will no longer be automatically validated against schemas manually added to them.
Level 2
All datasets need to be successfully validated against the default schema. If the validation is not successful, the dataset creation or update will be rejected.
Add a default schema and increase the validation level
from qdl import Scope
scope_uid = "<SCOPE_UID>"
schema_uid = "<SCHEMA_UID>"
scope = Scope.get(scope_uid)
scope.add_default_schema(schema_uid)
scope.schema_validation_level = 1
scope.update()
Lower the validation level and remove the default schema
Once a schema validation level is set, the default schema can no longer be removed. If this is desired, the
schema validation level needs to be lowered to 0 first, before removing the default schema.