Skip to content

Commit

Permalink
chore: XML validation: resource's iri and ark must be unique, resourc…
Browse files Browse the repository at this point in the history
…e's label must not be empty (DEV-2154) (#361)

In the XML file, it is not allowed anymore to have a resource with 
 - label=""
 - ark/iri attribute that are not unique in the document
  • Loading branch information
jnussbaum committed May 12, 2023
1 parent a93cb86 commit 62ec04b
Show file tree
Hide file tree
Showing 6 changed files with 123 additions and 2 deletions.
17 changes: 15 additions & 2 deletions src/dsp_tools/resources/schema/data.xsd
Expand Up @@ -407,7 +407,13 @@
<xs:element name="boolean-prop" type="booleanprop_type" minOccurs="0" maxOccurs="unbounded"/>
</xs:choice>
</xs:sequence>
<xs:attribute name="label" type="xs:string" use="required"/>
<xs:attribute name="label" use="required">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
</xs:restriction>
</xs:simpleType>
</xs:attribute>
<xs:attribute name="restype" type="xs:string" use="required"/>
<xs:attribute name="id" type="xs:ID" use="required"/>
<xs:attribute name="permissions" type="xs:NCName" use="optional"/>
Expand Down Expand Up @@ -538,6 +544,13 @@
<xs:selector xpath="resource"/>
<xs:field xpath="@permissions"/>
</xs:keyref>
<xs:unique name="IRI_attribute_of_resource_must_be_unique">
<xs:selector xpath=".//*"/>
<xs:field xpath="@iri"/>
</xs:unique>
<xs:unique name="ARK_attribute_of_resource_must_be_unique">
<xs:selector xpath=".//*"/>
<xs:field xpath="@ark"/>
</xs:unique>
</xs:element>
</xs:schema>

18 changes: 18 additions & 0 deletions test/unittests/test_shared.py
Expand Up @@ -36,6 +36,24 @@ def test_validate_xml_against_schema(self) -> None:
):
shared.validate_xml_against_schema(input_file="testdata/invalid-testdata/xml-data/utf8-text-with-xml-tags.xml")

with self.assertRaisesRegex(
UserError,
"Line 19: Element 'resource': Duplicate key-sequence .+ in unique identity-constraint 'IRI_attribute_of_resource_must_be_unique'"
):
shared.validate_xml_against_schema(input_file="testdata/invalid-testdata/xml-data/duplicate-iri.xml")

with self.assertRaisesRegex(
UserError,
"Line 19: Element 'resource': Duplicate key-sequence .+ in unique identity-constraint 'ARK_attribute_of_resource_must_be_unique'"
):
shared.validate_xml_against_schema(input_file="testdata/invalid-testdata/xml-data/duplicate-ark.xml")

with self.assertRaisesRegex(
UserError,
"Line 11: Element 'resource', attribute 'label': .+ The value '' has a length of '0'; this underruns the allowed minimum length of '1'"
):
shared.validate_xml_against_schema(input_file="testdata/invalid-testdata/xml-data/empty-label.xml")


def test_validate_xml_tags_in_text_properties(self) -> None:
utf8_texts_with_allowed_html_escapes = [
Expand Down
23 changes: 23 additions & 0 deletions testdata/invalid-testdata/xml-data/duplicate-ark.xml
@@ -0,0 +1,23 @@
<?xml version='1.0' encoding='utf-8'?>

<knora xmlns="https://dasch.swiss/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://dasch.swiss/schema ../../../src/dsp_tools/resources/schema/data.xsd"
shortcode="4124"
default-ontology="testonto">

<resource label="TestthingWithArk1"
restype=":TestThing2"
id="test_thing_with_ark_1"
ark="ark:/72163/4123-31ec6eab334-a.2022829">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

<resource label="TestthingWithArk2"
restype=":TestThing2"
id="test_thing_with_ark_2"
ark="ark:/72163/4123-31ec6eab334-a.2022829">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

</knora>
23 changes: 23 additions & 0 deletions testdata/invalid-testdata/xml-data/duplicate-iri.xml
@@ -0,0 +1,23 @@
<?xml version='1.0' encoding='utf-8'?>

<knora xmlns="https://dasch.swiss/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://dasch.swiss/schema ../../../src/dsp_tools/resources/schema/data.xsd"
shortcode="4124"
default-ontology="testonto">

<resource label="TestthingWithIri1"
restype=":TestThing2"
id="test_thing_with_iri_1"
iri="http://rdfh.ch/4123/54SYvWF0QUW6a">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

<resource label="TestthingWithIri2"
restype=":TestThing2"
id="test_thing_with_iri_2"
iri="http://rdfh.ch/4123/54SYvWF0QUW6a">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

</knora>
15 changes: 15 additions & 0 deletions testdata/invalid-testdata/xml-data/empty-label.xml
@@ -0,0 +1,15 @@
<?xml version='1.0' encoding='utf-8'?>

<knora xmlns="https://dasch.swiss/schema"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="https://dasch.swiss/schema ../../../src/dsp_tools/resources/schema/data.xsd"
shortcode="4124"
default-ontology="testonto">

<resource label=""
restype=":TestThing2"
id="test_thing_with_empty_label">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

</knora>
29 changes: 29 additions & 0 deletions testdata/xml-data/test-data-systematic.xml
Expand Up @@ -804,4 +804,33 @@
<text encoding="utf8">Hello!</text>
</text-prop>
</resource>

<resource label="TestthingWithArk1"
restype=":TestThing2"
id="test_thing_with_ark_1"
ark="ark:/72163/4123-43ec6evb134-a.2022828">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

<resource label="TestthingWithArk2"
restype=":TestThing2"
id="test_thing_with_ark_2"
ark="ark:/72163/4123-43xc6ivb931-a.2022829">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

<resource label="TestthingWithIri1"
restype=":TestThing2"
id="test_thing_with_iri_1"
iri="http://rdfh.ch/4123/DiAmYQzQSzC7cdTo6OJMYA">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

<resource label="TestthingWithIri2"
restype=":TestThing2"
id="test_thing_with_iri_2"
iri="http://rdfh.ch/4123/TqAnYQzrSzC2ctT06OJMYB">
<text-prop name=":hasSimpleText"><text encoding="utf8">Hello</text></text-prop>
</resource>

</knora>

0 comments on commit 62ec04b

Please sign in to comment.