Skip to content

Commit

Permalink
Enhance schema validation and processing
Browse files Browse the repository at this point in the history
  • Loading branch information
tmorrell committed Jul 4, 2024
1 parent d4182d0 commit e2cb020
Show file tree
Hide file tree
Showing 6 changed files with 119 additions and 91 deletions.
25 changes: 13 additions & 12 deletions datacite/schema45.py
Original file line number Diff line number Diff line change
Expand Up @@ -414,19 +414,20 @@ def geolocations(path, values):
elem.append(E.northBoundLatitude(str(box["northBoundLatitude"])))
element.append(elem)

polygons = value.get("geoLocationPolygons", [])
for polygon in polygons:
polygon = value.get("geoLocationPolygon")
if polygon:
elem = E.geoLocationPolygon()
points = polygon["polygonPoints"]
for p in points:
e = E.polygonPoint()
geopoint(e, p)
elem.append(e)
inPoint = polygon.get("inPolygonPoint")
if inPoint:
e = E.inPolygonPoint()
geopoint(e, inPoint)
elem.append(e)
for point in polygon:
plainPoint = point.get("polygonPoint")
if plainPoint:
e = E.polygonPoint()
geopoint(e, plainPoint)
elem.append(e)
inPoint = point.get("inPolygonPoint")
if inPoint:
e = E.inPolygonPoint()
geopoint(e, inPoint)
elem.append(e)
element.append(elem)

root.append(element)
Expand Down
56 changes: 38 additions & 18 deletions datacite/schemas/datacite-v4.5.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"$schema": "http://json-schema.org/draft-07/schema#",
"$schema": "http://json-schema.org/2019-09/schema#",
"id": "datacite-v4.5.json",
"title": "DataCite v4.5",
"description": "JSON representation of the DataCite v4.5 schema.",
Expand All @@ -16,6 +16,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"nameIdentifier": {"type": "string"},
"nameIdentifierScheme": {"type": "string"},
Expand All @@ -29,6 +30,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {"type": "string"},
"affiliationIdentifier": {"type": "string"},
Expand All @@ -39,7 +41,7 @@
},
"uniqueItems": true
},
"creator": {
"person": {
"type": "object",
"properties": {
"name": {"type": "string"},
Expand All @@ -52,9 +54,15 @@
},
"required": ["name"]
},
"creator": {
"type": "object",
"allOf": [{ "$ref": "#/definitions/person" }],
"unevaluatedProperties": false
},
"contributor": {
"type": "object",
"allOf": [{ "$ref": "#/definitions/creator" }],
"allOf": [{ "$ref": "#/definitions/person" }],
"unevaluatedProperties": false,
"properties": {
"contributorType": {"$ref": "#/definitions/contributorType"}
},
Expand Down Expand Up @@ -271,6 +279,7 @@
},
"geoLocationPoint": {
"type": "object",
"additionalProperties": false,
"properties": {
"pointLongitude": {"$ref": "#/definitions/longitude"},
"pointLatitude": {"$ref": "#/definitions/latitude"}
Expand All @@ -294,9 +303,9 @@
},
"type": "object",
"properties": {
"doi": {"type": "string", "pattern" : "^10.\\d{4,9}/[-._;()/:a-z0-9A-Z]+$"},
"prefix":{"type": "string", "pattern": "^10.\\d{4,9}$"},
"suffix":{"type": "string", "pattern": "^[-._;()/:a-z0-9A-Z]+$"},
"doi": {"type": "string", "pattern" : "^10[.][0-9]{4,9}[/][^\\s]+$"},
"prefix":{"type": "string", "pattern": "^10[.][0-9]{4,9}$"},
"suffix":{"type": "string", "pattern": "^[^\\s]+$"},
"event" : {
"type": "string",
"enum": [
Expand All @@ -308,6 +317,7 @@
"url": {"type": "string", "format": "uri"},
"types": {
"type": "object",
"additionalProperties": false,
"properties": {
"resourceType": {"type": "string"},
"resourceTypeGeneral": {"$ref": "#/definitions/resourceTypeGeneral"}
Expand All @@ -327,6 +337,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"title": {"type": "string"},
"titleType": {"$ref": "#/definitions/titleType"},
Expand All @@ -339,11 +350,12 @@
},
"publisher": {
"type": "object",
"additionalProperties": false,
"properties": {
"name": {"type":"string"},
"publisherIdentifier": {"type":"string"},
"publisherIdentifierScheme": {"type":"string"},
"schemeURI": {"type":"string", "format": "uri"},
"schemeUri": {"type":"string", "format": "uri"},
"lang": {"type":"string"}
},
"required": ["name"]
Expand All @@ -353,6 +365,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"subject": {"type": "string"},
"subjectScheme": {"type": "string"},
Expand All @@ -377,6 +390,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"date": {"$ref": "#/definitions/date"},
"dateType": {"$ref": "#/definitions/dateType"},
Expand All @@ -394,6 +408,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"alternateIdentifier": {"type": "string"},
"alternateIdentifierType": {"type": "string"}
Expand All @@ -407,6 +422,7 @@
"items": {
"type": "object",
"allOf": [{ "$ref": "#/definitions/relatedObject"}],
"unevaluatedProperties": false,
"properties": {
"relatedIdentifier": {"type": "string"},
"relatedIdentifierType": {"$ref": "#/definitions/relatedIdentifierType"}
Expand All @@ -421,9 +437,11 @@
"items": {
"type": "object",
"allOf": [{ "$ref": "#/definitions/relatedObject"}],
"unevaluatedProperties": false,
"properties": {
"relatedItemIdentifier": {
"type": "object",
"additionalProperties": false,
"properties": {
"relatedItemIdentifier": {"type": "string"},
"relatedItemIdentifierType": {"$ref": "#/definitions/relatedIdentifierType"}
Expand All @@ -435,6 +453,7 @@
"type": "array",
"items": {
"type": "object",
"unevaluatedProperties": false,
"allOf": [{ "$ref": "#/definitions/creator" }],
"required": ["name"]
}
Expand All @@ -443,6 +462,7 @@
"type": "array",
"items": {
"type": "object",
"unevaluatedProperties": false,
"allOf": [{ "$ref": "#/definitions/contributor" }],
"required": ["contributorType", "name"]
}
Expand All @@ -451,6 +471,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"title": {"type": "string"},
"titleType": {"$ref": "#/definitions/titleType"},
Expand Down Expand Up @@ -506,6 +527,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"rights": {"type": "string"},
"rightsUri": {"type": "string", "format": "uri"},
Expand All @@ -521,6 +543,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"description": {"type": "string"},
"descriptionType": {"$ref": "#/definitions/descriptionType"},
Expand All @@ -534,11 +557,13 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"geoLocationPlace": {"type": "string"},
"geoLocationPoint": {"$ref": "#/definitions/geoLocationPoint"},
"geoLocationBox": {
"type": "object",
"additionalProperties": false,
"properties": {
"westBoundLongitude": {"$ref": "#/definitions/longitude"},
"eastBoundLongitude": {"$ref": "#/definitions/longitude"},
Expand All @@ -547,21 +572,16 @@
},
"required": ["westBoundLongitude", "eastBoundLongitude", "southBoundLatitude", "northBoundLatitude"]
},
"geoLocationPolygons": {
"geoLocationPolygon": {
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"polygonPoints": {
"type": "array",
"items": {"$ref": "#/definitions/geoLocationPoint"},
"minItems": 4
},
"polygonPoint": {"$ref": "#/definitions/geoLocationPoint"},
"inPolygonPoint": {"$ref": "#/definitions/geoLocationPoint"}
},
"required": ["polygonPoints"]
},
"uniqueItems": true
}
}
}
}
},
Expand All @@ -571,6 +591,7 @@
"type": "array",
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"funderName": {"type": "string"},
"funderIdentifier": {"type": "string"},
Expand All @@ -597,7 +618,6 @@
}
},
"required": [
"url",
"creators",
"titles",
"publisher",
Expand Down
34 changes: 14 additions & 20 deletions tests/example/full.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,9 @@
from datacite import DataCiteMDSClient, schema42
from datacite import DataCiteMDSClient, schema45

# If you want to generate XML for earlier versions, you need to use either the
# schema31, schema40 or schema41 instead.
prefix = "10.1234"

data = {
"identifiers": [
{
"identifierType": "DOI",
"identifier": "10.1234/foo.bar",
}
],
"doi": f"{prefix}/test-doi",
"creators": [
{"name": "Smith, John"},
],
Expand All @@ -18,50 +12,50 @@
"title": "Minimal Test Case",
}
],
"publisher": "Invenio Software",
"publisher": {"name": "Invenio Software"},
"publicationYear": "2015",
"types": {"resourceType": "Dataset", "resourceTypeGeneral": "Dataset"},
"schemaVersion": "http://datacite.org/schema/kernel-4",
}

# Validate dictionary
assert schema42.validate(data)
assert schema45.validate(data)

# Generate DataCite XML from dictionary.
doc = schema42.tostring(data)
doc = schema45.tostring(data)

# Initialize the MDS client.
d = DataCiteMDSClient(
username="MYDC.MYACCOUNT",
username="DATACITE.ACCOUNT",
password="mypassword",
prefix="10.1234",
prefix=prefix,
test_mode=True,
)

# Set metadata for DOI
d.metadata_post(doc)

# Mint new DOI
d.doi_post("10.1234/test-doi", "http://example.org/test-doi")
d.doi_post(f"{prefix}/test-doi", "http://example.org/test-doi")

# Get DOI location
location = d.doi_get("10.1234/test-doi")
location = d.doi_get(f"{prefix}/test-doi")

# Set alternate URL for content type (available through content negotiation)
d.media_post(
"10.1234/test-doi",
f"{prefix}/test-doi",
{
"application/json": "http://example.org/test-doi/json/",
"application/xml": "http://example.org/test-doi/xml/",
},
)

# Get alternate URLs
mapping = d.media_get("10.1234/test-doi")
mapping = d.media_get(f"{prefix}/test-doi")
assert mapping["application/json"] == "http://example.org/test-doi/json/"

# Get metadata for DOI
doc = d.metadata_get("10.1234/test-doi")
doc = d.metadata_get(f"{prefix}/test-doi")

# Make DOI inactive
d.metadata_delete("10.1234/test-doi")
d.metadata_delete(f"{prefix}/test-doi")
Loading

0 comments on commit e2cb020

Please sign in to comment.