Skip to content

Commit

Permalink
Merge pull request #1158 from LD4P/display-error-for-bad-prop-template
Browse files Browse the repository at this point in the history
Display error for bad prop template
  • Loading branch information
jermnelson committed Aug 2, 2019
2 parents 835f3fd + 8136aae commit d688793
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 11 deletions.
26 changes: 26 additions & 0 deletions __tests__/ResourceValidator.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,4 +123,30 @@ describe('validate()', () => {

expect(results[1]).toEqual([{ message: 'Required', path: ['Instance', 'Barcode', 'Barcode', 'Barcode'], reduxPath: ['resource', 'resourceTemplate:Monograph:Instance', 'http://id.loc.gov/ontologies/bibframe/itemPortion', 'abcdCode', 'resourceTemplate:bf2:Identifiers:Barcode', 'http://www.w3.org/1999/02/22-rdf-syntax-ns#value'] }])
})

it('when a resourceTemplate is not found', () => {
const resource = {
'rt:fooched': {},
}
const rt = {
'rt:fooched': {
resourceLabel: 'borked',
propertyTemplates: [
{
propertyURI: 'http://examples.org/bogusOntologies/lookup1',
propertyLabel: 'lookup type with valueTemplateRefs',
type: 'lookup',
valueConstraint: {
valueTemplateRefs: [
'resourceTemplate:bf2:Note',
],
},
},
],
},
}
const results = new Validator({ resource, entities: { rt } }).validate()

expect(results[0].resource['rt:fooched'].errors[0]).toMatch('unable to retrieve rt:fooched from local store')
})
})
40 changes: 40 additions & 0 deletions __tests__/__fixtures__/lookupWithValueTemplateRefs.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
{
"id": "Sinopia:RT:Fixture:LookupWithValueTemplateRefs",
"resourceURI": "http://examples.org/bogusOntologies/Resource",
"remark": "This is a sample Resource Template, content is meaningless",
"resourceLabel": "test lookup type misconfigured with valueTemplateRefs",
"author": "surrogate for Nancy Lorimer",
"schema": "https://ld4p.github.io/sinopia/schemas/0.0.9/resource-template.json",
"propertyTemplates": [
{
"mandatory": "false",
"repeatable": "true",
"type": "lookup",
"valueConstraint": {
"defaults": [],
"useValuesFrom": [],
"valueDataType": {},
"valueTemplateRefs": [
"resourceTemplate:bf2:Note"
]
},
"propertyLabel": "lookup type with valueTemplateRefs",
"propertyURI": "http://examples.org/bogusOntologies/lookup1"
},
{
"mandatory": "false",
"repeatable": "true",
"type": "resource",
"valueConstraint": {
"defaults": [],
"useValuesFrom": [],
"valueDataType": {},
"valueTemplateRefs": [
"resourceTemplate:bf2:Title:Note"
]
},
"propertyLabel": "resource type with valueTemplateRefs",
"propertyURI": "http://examples.org/bogusOntologies/lookup2"
}
]
}
1 change: 1 addition & 0 deletions __tests__/fixtureLoaderHelper.js
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@ const rtFileNames = [
'literalRepeatDefaultLiteralNonEnglish.json',
'literalRepeatDefaultLiteralOnly.json',
'literalRepeatNoDefault.json',
'lookupWithValueTemplateRefs.json',
'propertyURIRepeated.json',
'rdaItemMonograph.json',
]
Expand Down
22 changes: 22 additions & 0 deletions __tests__/integration/propertyTemplateMisconfigured.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright 2019 Stanford University see LICENSE for license

import pupExpect from 'expect-puppeteer'
import { testUserLogin } from './loginHelper'

describe('Errors with Misconfigured Property Templates', () => {
beforeAll(async () => {
await testUserLogin()
})

beforeEach(async () => {
await page.goto('http://127.0.0.1:8888/templates')
await page.waitForSelector('.react-bootstrap-table')
})

it('lookup type misconfigured with valueTemplateRefs', async () => {
// expect.assertions(3)
await pupExpect(page).toClick('a[href="/editor"]', { text: 'test lookup type misconfigured with valueTemplateRefs' })
await pupExpect(page).toClick('button.btn-add[data-id="lookup1"]')
await pupExpect(page).toMatch('This propertyTemplate should not be of type lookup.')
})
})
26 changes: 16 additions & 10 deletions src/ResourceValidator.js
Original file line number Diff line number Diff line change
Expand Up @@ -35,16 +35,22 @@ export default class Validator {
validateResource(reduxPath, labelPath) {
const resourceTemplateId = reduxPath.slice(-1)[0]
const resourceTemplate = getResourceTemplate({ selectorReducer: this.selectorReducer }, resourceTemplateId)
resourceTemplate.propertyTemplates.forEach((propertyTemplate) => {
const newReduxPath = [...reduxPath, propertyTemplate.propertyURI]
const newLabelPath = [...labelPath, resourceTemplate.resourceLabel, propertyTemplate.propertyLabel]
if (propertyTemplate.mandatory === 'true') {
this.validateMandatoryProperty(newReduxPath, newLabelPath)
}
if (!_.isEmpty(propertyTemplate.valueConstraint?.valueTemplateRefs)) {
this.validateNestedResourceProperty(newReduxPath, newLabelPath, propertyTemplate)
}
})
if (resourceTemplate) {
resourceTemplate.propertyTemplates.forEach((propertyTemplate) => {
const newReduxPath = [...reduxPath, propertyTemplate.propertyURI]
const newLabelPath = [...labelPath, resourceTemplate.resourceLabel, propertyTemplate.propertyLabel]
if (propertyTemplate.mandatory === 'true') {
this.validateMandatoryProperty(newReduxPath, newLabelPath)
}
if (!_.isEmpty(propertyTemplate.valueConstraint?.valueTemplateRefs)) {
this.validateNestedResourceProperty(newReduxPath, newLabelPath, propertyTemplate)
}
})
} else {
// not sure this error is ever surfaced anywhere, but we do need to trap for undefined resourceTemplate
// possibly will be surfaced when issue #1148 is addressed
this.addError(reduxPath, labelPath, `unable to retrieve ${resourceTemplateId} from local store`)
}
}

validateMandatoryProperty(reduxPath, labelPath) {
Expand Down
13 changes: 13 additions & 0 deletions src/components/editor/property/PropertyComponent.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,19 @@ import { getTagNameForPropertyTemplate } from 'utilities/propertyTemplates'

const PropertyComponent = (props) => {
const tag = getTagNameForPropertyTemplate(props.propertyTemplate)
if (!tag) {
return (
<div className="row">
<div className="col-md-12" style={{ marginTop: '10px' }}>
<div className="alert alert-danger alert-dismissible">
<button className="close" data-dismiss="alert" aria-label="close">&times;</button>
This propertyTemplate should not be of type {props.propertyTemplate.type}.
</div>
</div>
</div>
)
}

const TagName = React.lazy(() => import(`./${tag}`))
return (
<React.Suspense fallback={<div>Loading...</div>}>
Expand Down
2 changes: 1 addition & 1 deletion src/utilities/propertyTemplates.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ const textFieldType = (config, propertyTemplate) => {
case 'resource':
return 'InputURI'
default:
console.error(`Unknown propertyTemplate type (component=${config}, type=${propertyTemplate.type})`)
// error case handled by caller
return null
}
}
Expand Down

0 comments on commit d688793

Please sign in to comment.