Skip to content

Commit

Permalink
Better format error messages on the metadata page
Browse files Browse the repository at this point in the history
  • Loading branch information
markpatton committed Feb 3, 2025
1 parent 6216575 commit a4e984a
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/components/workflow-metadata/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,7 @@ export default class WorkflowMetadata extends Component {
target: ENV.APP.rootElement,
type: 'error',
title: 'Form Validation Error',
html: this.validationErrorMsg(metadataSchema.getErrors()),
html: metadataSchema.getErrorMessage(),
});
return;
}
Expand Down
37 changes: 36 additions & 1 deletion app/services/metadata-schema.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ export default class MetadataSchemaService extends Service {

if (readonly && readonly.length > 0) {
/**
* For each key in the data object, if they are marked as "read only",
* For each key in the data object, if they are marked as "read only"
* set the field's 'readonly' to true, and 'toolbarSticky' to false iff the
* key refers to an array
*/
Expand Down Expand Up @@ -183,6 +183,41 @@ export default class MetadataSchemaService extends Service {
return this.validator.errors;
}

/**
* Turn a property returned by something like Ajv into its title.
* Ideally this would be looked up in the schema instead, but the general case is complicated.
*/
getPropertyTitle(property) {
// ab-cd -> abCd
let title = property.replace(/-\w/g, (s) => s.charAt(1).toUpperCase());

// Clean out non-word characters
title = property.replace(/\W/g, '');

// Turn into words
title = title.replace(/[A-Z]/g, (s) => ' ' + s).trim();

// Capitalize first word
title = title.charAt(0).toUpperCase() + title.slice(1);

return title;
}

/**
* Return a human readable message describing validation errorss.
*/
getErrorMessage() {
return this.validator.errors
.map((error) => {
if (error.keyword === 'required') {
return 'Missing required metadata: ' + this.getPropertyTitle(error.params.missingProperty);
} else {
return error.message;
}
})
.join(', ');
}

/**
* Get all unique field names across a set of schema. This includes any unique field
* in schema referenced in the 'allOf' validation sections, unless otherwise specified.
Expand Down
4 changes: 2 additions & 2 deletions tests/acceptance/nih-submission-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -453,7 +453,7 @@ module('Acceptance | submission', function (hooks) {
await click('.alpaca-form-button-Next');

await waitFor('#swal2-content');
assert.dom('#swal2-content').includesText("should have required property 'author'");
assert.dom('#swal2-content').includesText('Missing required metadata: Author');

// Some reason, setting the document query to a variable before clicking works,
// but calling the query selector in the click does not work
Expand Down Expand Up @@ -592,7 +592,7 @@ module('Acceptance | submission', function (hooks) {
await click('.alpaca-form-button-Next');

await waitFor('#swal2-content');
assert.dom('#swal2-content').includesText("should have required property 'author'");
assert.dom('#swal2-content').includesText('Missing required metadata: Author');

const confirmBtn = '.swal2-confirm';
assert.ok(confirmBtn, 'No SweetAlert OK button found');
Expand Down

0 comments on commit a4e984a

Please sign in to comment.