Skip to content
Snippets Groups Projects
Verified Commit f8574222 authored by Daniel Hornung's avatar Daniel Hornung
Browse files

ENH: Validates if the input is a Json Schema.

parent d757f2b7
No related branches found
No related tags found
2 merge requests!39Release 0.4.0,!33F json schema datamodel
# This file is a part of the CaosDB Project.
#
# Copyright (C) 2022 IndiScale GmbH <info@indiscale.com>
# Copyright (C) 2022 Florian Spreckelsen <f.spreckelsen@indiscale.com>
# Copyright (C) 2022 Daniel Hornung <d.hornung@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <https://www.gnu.org/licenses/>.
"""
This module (and script) provides methods to read a DataModel from a YAML file.
......@@ -23,6 +42,7 @@ import yaml
from typing import List
import jsonschema
import caosdb as db
from .data_model import DataModel
......@@ -513,6 +533,12 @@ class JsonSchemaParser(Parser):
raise JsonSchemaDefinitionError(f"Object {ii+1} is lacking the `title` key word")
if "type" not in elt:
raise JsonSchemaDefinitionError(f"Object {ii+1} is lacking the `type` key word")
# Check if this is a valid Json Schema
try:
jsonschema.Draft202012Validator.check_schema(elt)
except jsonschema.SchemaError as err:
raise JsonSchemaDefinitionError(
f"Json Schema error in {elt['title']}:\n{str(err)}") from err
name = self._stringify(elt["title"], context=elt)
self._treat_element(elt, name)
......
{
"title": "Dataset",
"description": "",
"type": "object",
"required": "Dataset"
}
......@@ -6,7 +6,8 @@
"properties": {
"title": { "type": "string", "description": "full dataset title" },
"campaign": { "type": "string", "description": "FIXME" },
"method": { "type": "string", "description": "FIXME" }
"method": { "type": "string", "description": "FIXME" },
"titled": { "title": "The title", "type": "string", "description": "None" }
},
"required": ["title"]
......
......@@ -3,6 +3,7 @@
#
# Copyright (C) 2022 IndiScale GmbH <info@indiscale.com>
# Copyright (C) 2022 Florian Spreckelsen <f.spreckelsen@indiscale.com>
# Copyright (C) 2022 Daniel Hornung <d.hornung@indiscale.com>
#
# This program is free software: you can redistribute it and/or modify it under
# the terms of the GNU Affero General Public License as published by the Free
......@@ -17,10 +18,15 @@
# You should have received a copy of the GNU Affero General Public License along
# with this program. If not, see <https://www.gnu.org/licenses/>.
#
# @review Daniel Hornung 2022-02-18
import os
import pytest
import caosdb as db
from caosadvancedtools.models.parser import parse_model_from_json_schema
from caosadvancedtools.models.parser import (parse_model_from_json_schema,
JsonSchemaDefinitionError)
FILEPATH = os.path.join(os.path.dirname(
os.path.abspath(__file__)), 'json-schema-models')
......@@ -39,12 +45,15 @@ def test_rt_with_string_properties():
assert isinstance(dataset_rt, db.RecordType)
assert dataset_rt.name == "Dataset"
assert dataset_rt.description == ""
assert len(dataset_rt.get_properties()) == 3
assert len(dataset_rt.get_properties()) == 4
assert dataset_rt.get_property("title") is not None
assert dataset_rt.get_property("campaign") is not None
assert dataset_rt.get_property("method") is not None
assert dataset_rt.get_property("The title") is not None
assert dataset_rt.get_property("titled") is None
title_prop = dataset_rt.get_property("title")
assert title_prop.datatype == db.TEXT
assert dataset_rt.get_importance(title_prop.name) == db.OBLIGATORY
......@@ -105,3 +114,15 @@ def test_datamodel_with_atomic_properties():
bool_prop = rt2.get_property("boolean")
assert bool_prop.datatype == db.BOOLEAN
def test_required_no_list():
"""Exception must be raised when "required" is not a list."""
# @author Daniel Hornung
# @date 2022-02-18
with pytest.raises(JsonSchemaDefinitionError) as err:
parse_model_from_json_schema(
os.path.join(FILEPATH,
"datamodel_required_no_list.schema.json"))
assert "'Dataset' is not of type 'array'" in str(err.value)
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment