Skip to content
This repository has been archived by the owner on Jun 22, 2023. It is now read-only.

Commit

Permalink
Merge pull request #173 from trompamusic/feat/medium-of-performance
Browse files Browse the repository at this point in the history
feat(schema): Add rdau:P60537 (medium of performance) field
  • Loading branch information
ChristiaanScheermeijer authored Apr 30, 2021
2 parents 1e669bf + ac61d6b commit 5deee86
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 13 deletions.
4 changes: 4 additions & 0 deletions src/routes/helpers/jsonld/MediaObject.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"https://schema.org/MediaObject"
]
},
"relationalProperties": ["mediumOfPerformance"],
"properties": {
"identifier": [
"http://purl.org/dc/terms/identifier",
Expand Down Expand Up @@ -287,6 +288,9 @@
],
"width": [
"https://schema.org/width"
],
"mediumOfPerformance": [
"http://rdaregistry.info/Elements/u/P60537"
]
}
}
4 changes: 4 additions & 0 deletions src/routes/helpers/jsonld/MusicComposition.json
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
"https://schema.org/MusicComposition"
]
},
"relationalProperties": ["mediumOfPerformance"],
"properties": {
"identifier": [
"http://purl.org/dc/terms/identifier",
Expand Down Expand Up @@ -323,6 +324,9 @@
],
"recordedAs": [
"https://schema.org/recordedAs"
],
"mediumOfPerformance": [
"http://rdaregistry.info/Elements/u/P60537"
]
}
}
6 changes: 4 additions & 2 deletions src/routes/helpers/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ const scopedContexts = {
prov: 'http://www.w3.org/ns/prov#',
skos: 'http://www.w3.org/2004/02/skos/core#',
rdf: 'http://www.w3.org/1999/02/22-rdf-syntax-ns#',
bib: 'https://bib.schema.org/'
bib: 'https://bib.schema.org/',
rdau: 'http://rdaregistry.info/Elements/u/'
}

/**
Expand Down Expand Up @@ -41,6 +42,7 @@ export const transformJsonLD = (type, data) => {
if (!config) {
throw new Error(`JSON LD not supported for type "${type}"`)
}
const jsonldRelationalProperties = config.relationalProperties || []

// Base JSON-LD document
const jsonLdData = {
Expand Down Expand Up @@ -72,7 +74,7 @@ export const transformJsonLD = (type, data) => {
}

// Transform the value when it's a relational property
if (elementValue && schemaHelper.isRelationalProperty(property)) {
if (elementValue && (schemaHelper.isRelationalProperty(property) || jsonldRelationalProperties.includes(key))) {
if (Array.isArray(elementValue)) {
elementValue = elementValue.map(id => ({
'@id': id
Expand Down
4 changes: 2 additions & 2 deletions src/schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ import { resolvers } from './resolvers'
import { authenticationFieldTransformer } from './transformers/authenticationFieldTransformer'
import { subscriptionFieldTransformer } from './transformers/subscriptionFieldTransformer'
import { createdUpdatedFieldTransformer } from './transformers/createdUpdatedFieldTransformer'
import { additionalTypeFieldTransformer } from './transformers/additionalTypeFieldTransformer'
import { validUrlFieldTransformer } from './transformers/validUrlFieldTransformer'
import concatenate from './utils/concatenate'

/*
Expand Down Expand Up @@ -51,6 +51,6 @@ export const schema = transformSchema(
subscriptionFieldTransformer,
authenticationFieldTransformer,
createdUpdatedFieldTransformer,
additionalTypeFieldTransformer
validUrlFieldTransformer
]
)
2 changes: 2 additions & 0 deletions src/schema/type/MediaObject.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -206,4 +206,6 @@ type MediaObject implements SearchableInterface & ThingInterface & ProvenanceEnt
uploadDate: _Neo4jDate
"https://schema.org/width"
width: Int
"A url to a skos:Concept representing the medium of performance of this MusicCompositation (e.g. instrumentation) "
mediumOfPerformance: [String]
}
2 changes: 2 additions & 0 deletions src/schema/type/MusicComposition.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -233,4 +233,6 @@ type MusicComposition implements SearchableInterface & ThingInterface & Provenan
musicalKey: String
"https://schema.org/recordedAs"
recordedAs: [MusicRecording] @relation(name: "RECORDED_AS", direction: OUT)
"A url to a skos:Concept representing the medium of performance of this MusicCompositation (e.g. instrumentation) "
mediumOfPerformance: [String]
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,31 @@ import isURL from 'validator/lib/isURL'
import { parseFieldName } from '../utils/schema'

/**
* Test the given additionalType values for valid URLs
* Test the given field values for valid URLs
* @param {null|[string]} values
* @return {boolean}
*/
export const additionalTypeIsValid = (values) => {
export const fieldIsValid = (values) => {
if (!Array.isArray(values)) {
return true
}

return !values.some(additionalType => !isURL(additionalType, {
return !values.some(value => !isURL(value, {
require_protocol: true,
require_valid_protocol: true,
protocols: ['http', 'https']
}))
}

export const additionalTypeFieldTransformer = new TransformRootFields((operation, fieldName, field) => {
const validationFields = ['additionalType', 'mediumOfPerformance']

export const validUrlFieldTransformer = new TransformRootFields((operation, fieldName, field) => {
// Only needed for mutations
if (operation !== 'Mutation') {
return undefined
}

const { action } = parseFieldName(fieldName)
const { action, type } = parseFieldName(fieldName)

if (action !== 'Create' && action !== 'Update' && action !== 'Merge') {
return undefined
Expand All @@ -35,10 +37,11 @@ export const additionalTypeFieldTransformer = new TransformRootFields((operation
const next = field.resolve

field.resolve = (object, params, context, info) => {
if (!additionalTypeIsValid(params.additionalType)) {
throw new UserInputError('additionalType list can only contain valid URLs with protocol')
}

validationFields.forEach(field => {
if (!fieldIsValid(params[field])) {
throw new UserInputError(`${field} list can only contain valid URLs with protocol`)
}
})
return next(object, params, context, info)
}
})

0 comments on commit 5deee86

Please sign in to comment.