Skip to content
GitLab
Explore
Sign in
Register
Primary navigation
Search or go to…
Project
S
swh-deposit
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Antoine Lambert
swh-deposit
Commits
bb5c6b55
Commit
bb5c6b55
authored
3 years ago
by
vlorentz
Browse files
Options
Downloads
Patches
Plain Diff
checks: Add type annotation to extra_validator
parent
df04906e
No related branches found
Branches containing commit
No related tags found
Tags containing commit
No related merge requests found
Changes
1
Hide whitespace changes
Inline
Side-by-side
Showing
1 changed file
swh/deposit/api/checks.py
+31
-8
31 additions, 8 deletions
swh/deposit/api/checks.py
with
31 additions
and
8 deletions
swh/deposit/api/checks.py
+
31
−
8
View file @
bb5c6b55
...
...
@@ -16,7 +16,7 @@ Suggested fields:
import
dataclasses
import
functools
from
typing
import
Dict
,
Optional
,
Tuple
from
typing
import
Dict
,
Iterator
,
Optional
,
Tuple
,
cast
import
urllib
from
xml.etree
import
ElementTree
...
...
@@ -35,7 +35,10 @@ METADATA_PROVENANCE_KEY = "swh:metadata-provenance"
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
within XML Schema.
...
...
@@ -55,17 +58,17 @@ def extra_validator(element, xsd_element):
try
:
url
=
urllib
.
parse
.
urlparse
(
element
.
text
)
except
ValueError
:
raise
xmlschema
.
XMLSchemaValidationError
(
yield
xmlschema
.
XMLSchemaValidationError
(
xsd_element
,
element
,
f
"
{
element
.
text
!r}
is not a valid URI
"
,
)
from
None
)
else
:
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
"
,
)
elif
"
"
in
url
.
netloc
:
# urllib is a little too permissive...
raise
xmlschema
.
XMLSchemaValidationError
(
yield
xmlschema
.
XMLSchemaValidationError
(
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]]
deposit_elt
=
metadata
.
find
(
"
swh:deposit
"
,
namespaces
=
NAMESPACES
)
if
deposit_elt
:
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
:
return
False
,
{
"
metadata
"
:
[{
"
fields
"
:
[
"
swh:deposit
"
],
"
summary
"
:
str
(
e
)}]}
...
...
@@ -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
continue
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
:
detail
.
append
({
"
fields
"
:
[
schema_element
.
prefixed_name
],
"
summary
"
:
str
(
e
)})
else
:
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment