synapse-react-client
Version:
[](https://travis-ci.com/Sage-Bionetworks/Synapse-React-Client) [](https://badge.fury.io/js/synaps
94 lines • 4.08 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.transformErrors = exports.getFriendlyPropertyName = exports.dropNullishArrayValues = void 0;
var lodash_es_1 = require("lodash-es");
var synapseTypes_1 = require("../../../utils/synapseTypes");
/**
* Strips null values from arrays in the provided form data. If the array is empty after
* removing null values, the key is removed from the form data.
*
* This allows users to submit forms with empty array fields (SWC-5762)
*/
function dropNullishArrayValues(formData) {
var newFormData = {};
Object.keys(formData).forEach(function (key) {
var value = formData[key];
if (Array.isArray(value)) {
value = value.filter(function (item) { return item != null; });
if (!(0, lodash_es_1.isEmpty)(value)) {
newFormData[key] = value;
}
}
else {
newFormData[key] = value;
}
});
return newFormData;
}
exports.dropNullishArrayValues = dropNullishArrayValues;
/**
* Inspects the property of the AjvError and modifies it to be comparable to simple key strings, like entity property keys.
* @param error
* @returns
*/
function getFriendlyPropertyName(error) {
if (error.property.startsWith('[')) {
// Additional properties are surrounded by brackets and quotations, so let's remove them
return error.property.substring(2, error.property.length - 2);
}
else if (error.property.startsWith('.')) {
return error.property.substring(1);
}
else {
return error.property;
}
}
exports.getFriendlyPropertyName = getFriendlyPropertyName;
function transformErrors(errors) {
// Transform the errors in the following ways:
// - Simplify the set of errors when failing to select an enumeration defined with an anyOf (SWC-5724)
// - Show a custom error message when using a property that collides with an internal entity property (SWC-5678)
// Fixing anyOf errors
// Group the errors by the property that the error applies to
var grouped = (0, lodash_es_1.groupBy)(errors, function (error) { return error.property; });
Object.keys(grouped).map(function (property) {
var errorGroup = grouped[property];
// First, see if it is an anyOf error
var hasAnyOfError = errorGroup.some(function (e) { return e.message === 'should match some schema in anyOf'; });
// We determine if it's an anyOf *enum* error if all error messages in the property match one of these three messages:
var isEnumError = hasAnyOfError &&
errorGroup.every(function (error) {
if (error.message === 'should be string') {
return true;
}
else if (error.message === 'should be equal to constant') {
return true;
}
else if (error.message === 'should match some schema in anyOf') {
return true;
}
else {
return false;
}
});
// If it's an anyOf enum error, we just modify the first message and drop the rest
if (isEnumError && errorGroup.length > 0) {
errorGroup[0].message = 'should be equal to one of the allowed values';
grouped[property] = [errorGroup[0]];
}
});
// Ungroup the errors after potentially modifying them
errors = (0, lodash_es_1.flatMap)(grouped);
// Custom error message if the custom annotation key collides with an internal entity property
errors = errors.map(function (error) {
var propertyName = getFriendlyPropertyName(error);
if (synapseTypes_1.entityJsonKeys.includes(propertyName)) {
error.message = "\"" + propertyName + "\" is a reserved internal key and cannot be used.";
}
return error;
});
// Return the transformed errors.
return errors;
}
exports.transformErrors = transformErrors;
//# sourceMappingURL=AnnotationEditorUtils.js.map