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

checks: Add type annotation to extra_validator

parent df04906e
No related branches found
No related tags found
No related merge requests found
...@@ -16,7 +16,7 @@ Suggested fields: ...@@ -16,7 +16,7 @@ Suggested fields:
import dataclasses import dataclasses
import functools import functools
from typing import Dict, Optional, Tuple from typing import Dict, Iterator, Optional, Tuple, cast
import urllib import urllib
from xml.etree import ElementTree from xml.etree import ElementTree
...@@ -35,7 +35,10 @@ METADATA_PROVENANCE_KEY = "swh:metadata-provenance" ...@@ -35,7 +35,10 @@ METADATA_PROVENANCE_KEY = "swh:metadata-provenance"
AFFILIATION_NO_NAME = "Reason: affiliation does not have a <codemeta:name> element" AFFILIATION_NO_NAME = "Reason: affiliation does not have a <codemeta:name> element"
def extra_validator(element, xsd_element): def extra_validator(
element: ElementTree.Element,
xsd_element: xmlschema.validators.elements.Xsd11Element,
) -> Optional[Iterator[xmlschema.XMLSchemaValidationError]]:
"""Performs extra checks on Atom elements that cannot be implemented purely """Performs extra checks on Atom elements that cannot be implemented purely
within XML Schema. within XML Schema.
...@@ -55,17 +58,17 @@ def extra_validator(element, xsd_element): ...@@ -55,17 +58,17 @@ def extra_validator(element, xsd_element):
try: try:
url = urllib.parse.urlparse(element.text) url = urllib.parse.urlparse(element.text)
except ValueError: except ValueError:
raise 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",
) from None )
else: else:
if not url.scheme or not url.netloc: if not url.scheme or not url.netloc:
raise xmlschema.XMLSchemaValidationError( yield xmlschema.XMLSchemaValidationError(
xsd_element, element, f"{element.text!r} is not an absolute URI", xsd_element, element, f"{element.text!r} is not an absolute URI",
) )
elif " " in url.netloc: elif " " in url.netloc:
# urllib is a little too permissive... # urllib is a little too permissive...
raise 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",
) )
...@@ -128,7 +131,17 @@ def check_metadata(metadata: ElementTree.Element) -> Tuple[bool, Optional[Dict]] ...@@ -128,7 +131,17 @@ def check_metadata(metadata: ElementTree.Element) -> Tuple[bool, Optional[Dict]]
deposit_elt = metadata.find("swh:deposit", namespaces=NAMESPACES) deposit_elt = metadata.find("swh:deposit", namespaces=NAMESPACES)
if deposit_elt: if deposit_elt:
try: try:
schemas().swh.validate(deposit_elt, extra_validator=extra_validator) schemas().swh.validate(
deposit_elt,
extra_validator=cast(
# ExtraValidatorType is a callable with "SchemaType" as second
# argument, but extra_validator() is actually passed Xsd11Element
# as second argument
# https://github.com/sissaschool/xmlschema/issues/291
xmlschema.aliases.ExtraValidatorType,
extra_validator,
),
)
except xmlschema.exceptions.XMLSchemaException as e: except xmlschema.exceptions.XMLSchemaException as e:
return False, {"metadata": [{"fields": ["swh:deposit"], "summary": str(e)}]} return False, {"metadata": [{"fields": ["swh:deposit"], "summary": str(e)}]}
...@@ -141,7 +154,17 @@ def check_metadata(metadata: ElementTree.Element) -> Tuple[bool, Optional[Dict]] ...@@ -141,7 +154,17 @@ def check_metadata(metadata: ElementTree.Element) -> Tuple[bool, Optional[Dict]]
# Tag is not specified in the schema, don't validate it # Tag is not specified in the schema, don't validate it
continue continue
try: try:
schemas().codemeta.validate(child, extra_validator=extra_validator) schemas().codemeta.validate(
child,
extra_validator=cast(
# ExtraValidatorType is a callable with "SchemaType" as second
# argument, but extra_validator() is actually passed Xsd11Element
# as second argument
# https://github.com/sissaschool/xmlschema/issues/291
xmlschema.aliases.ExtraValidatorType,
extra_validator,
),
)
except xmlschema.exceptions.XMLSchemaException as e: except xmlschema.exceptions.XMLSchemaException as e:
detail.append({"fields": [schema_element.prefixed_name], "summary": str(e)}) detail.append({"fields": [schema_element.prefixed_name], "summary": str(e)})
else: else:
......
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