Skip to content
Snippets Groups Projects
Commit 6d1089cb authored by vlorentz's avatar vlorentz
Browse files

Add support for HAL-ID as identifier

parent a8cda2cb
No related branches found
No related tags found
No related merge requests found
...@@ -16,6 +16,7 @@ Suggested fields: ...@@ -16,6 +16,7 @@ Suggested fields:
import dataclasses import dataclasses
import functools import functools
import re
from typing import Dict, Iterator, Optional, Tuple, cast from typing import Dict, Iterator, Optional, Tuple, cast
import urllib import urllib
from xml.etree import ElementTree from xml.etree import ElementTree
...@@ -55,22 +56,33 @@ def extra_validator( ...@@ -55,22 +56,33 @@ def extra_validator(
# </xsd:simpleType> # </xsd:simpleType>
# However, this would give an unreadable error, so we implement it here # However, this would give an unreadable error, so we implement it here
# in Python instead. # in Python instead.
try: yield from absolute_uri_validator(element, xsd_element)
url = urllib.parse.urlparse(element.text) elif type_name == "{https://doi.org/10.5063/SCHEMA/CODEMETA-2.0}identifierType":
except ValueError: # Made-up type, that allows both absolute URIs and HAL-IDs
if not re.match("hal-[0-9]+", element.text or ""):
yield from absolute_uri_validator(element, xsd_element)
def absolute_uri_validator(
element: ElementTree.Element,
xsd_element: xmlschema.validators.elements.Xsd11Element,
) -> Iterator[xmlschema.XMLSchemaValidationError]:
try:
url = urllib.parse.urlparse(element.text)
except ValueError:
yield xmlschema.XMLSchemaValidationError(
xsd_element, element, f"{element.text!r} is not a valid URI",
)
else:
if not url.scheme or not url.netloc:
yield xmlschema.XMLSchemaValidationError(
xsd_element, element, f"{element.text!r} is not an absolute URI",
)
elif " " in url.netloc:
# urllib is a little too permissive...
yield xmlschema.XMLSchemaValidationError( yield xmlschema.XMLSchemaValidationError(
xsd_element, element, f"{element.text!r} is not a valid URI", xsd_element, element, f"{element.text!r} is not a valid URI",
) )
else:
if not url.scheme or not url.netloc:
yield xmlschema.XMLSchemaValidationError(
xsd_element, element, f"{element.text!r} is not an absolute URI",
)
elif " " in url.netloc:
# urllib is a little too permissive...
yield xmlschema.XMLSchemaValidationError(
xsd_element, element, f"{element.text!r} is not a valid URI",
)
@dataclasses.dataclass @dataclasses.dataclass
......
...@@ -146,6 +146,38 @@ _parameters1 = [ ...@@ -146,6 +146,38 @@ _parameters1 = [
</entry> </entry>
""", """,
), ),
(
"identifier-is-halid",
f"""\
<entry {XMLNS}>
<url>some url</url>
<codemeta:name>bar</codemeta:name>
<codemeta:author>
<codemeta:name>The Author</codemeta:name>
</codemeta:author>
<codemeta:identifier>hal-12345</codemeta:identifier>
{PROVENANCE_XML}
</entry>
""",
),
(
"identifier-is-propertyvalue",
f"""\
<entry {XMLNS}>
<url>some url</url>
<codemeta:name>bar</codemeta:name>
<codemeta:author>
<codemeta:name>The Author</codemeta:name>
</codemeta:author>
<schema:identifier>
<codemeta:type>schema:PropertyValue</codemeta:type>
<schema:propertyID>HAL-ID</schema:propertyID>
<schema:value>hal-02527911</schema:value>
</schema:identifier>
{PROVENANCE_XML}
</entry>
""",
),
( (
"codemeta-dates", "codemeta-dates",
f"""\ f"""\
......
...@@ -52,11 +52,23 @@ ...@@ -52,11 +52,23 @@
Therefore, more custom checks are implemented in swh/deposit/api/checks.py Therefore, more custom checks are implemented in swh/deposit/api/checks.py
in order to allow either. --> in order to allow either. -->
<xsd:simpleType name="halId">
<xsd:restriction base="xsd:string">
<xsd:pattern value="hal-[0-9]+"/>
</xsd:restriction>
</xsd:simpleType>
<xsd:simpleType name="identifierType">
<!-- CodeMeta only allows URIs, but we make an exception for HAL-IDs,
in order not to break backward-compatibility. -->
<xsd:union memberTypes="xsd:anyURI codemeta:halId"/>
</xsd:simpleType>
<xsd:element name="identifier" type="codemeta:identifierType" />
<xsd:element name="name" type="xsd:string" /> <xsd:element name="name" type="xsd:string" />
<xsd:element name="givenName" type="xsd:string" /> <xsd:element name="givenName" type="xsd:string" />
<xsd:element name="familyName" type="xsd:string" /> <xsd:element name="familyName" type="xsd:string" />
<xsd:element name="email" type="xsd:string" /> <xsd:element name="email" type="xsd:string" />
<xsd:element name="identifier" type="xsd:anyURI" />
<xsd:element name="applicationCategory" type="xsd:string" /> <xsd:element name="applicationCategory" type="xsd:string" />
<xsd:element name="applicationSubCategory" type="xsd:string" /> <xsd:element name="applicationSubCategory" type="xsd:string" />
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment