Skip to content

Commit

Permalink
Re-apply fix: showing proper validation message if trying to import c…
Browse files Browse the repository at this point in the history
…ontributions that are not installed"" (#1097)

Re-apply fix: showing proper validation message if trying to import contributions that are not installed
  • Loading branch information
fcastill authored May 8, 2019
2 parents 2cd7944 + 20b6e91 commit 2d6f4d7
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,9 @@ export class ImportErrorFormatterService {
case 'improper-import':
messageHeader = this._translate.instant('IMPORT-ERROR:IMPROPER_IMPORT');
break;
case 'contrib-not-installed':
messageHeader = this._translate.instant('IMPORT-ERROR:CONTRIB_NOT_INSTALLED');
break;
default:
messageHeader = this._translate.instant('APP-LIST:BROKEN_RULE_UNKNOWN');
break;
Expand Down Expand Up @@ -124,6 +127,11 @@ export class ImportErrorFormatterService {
ref: detail.params.ref,
});
break;
case 'contrib-not-installed':
errorMessage = this._translate.instant('IMPORT-ERROR:CONTRIB_NOT_INSTALLED_CONTENT', {
ref: detail.params.ref,
});
break;
default:
errorMessage = this._translate.instant('APP-LIST:BROKEN_RULE_UNKNOWN');
break;
Expand Down
2 changes: 2 additions & 0 deletions apps/client/src/i18n/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
"IMPORT-ERROR:PROPERTY_REQUIRED": "Property required",
"IMPORT-ERROR:ACTIVITY_MISSING": "Activity missing",
"IMPORT-ERROR:TRIGGER_MISSING": "Trigger missing",
"IMPORT-ERROR:CONTRIB_NOT_INSTALLED": "Contribution missing",
"IMPORT-ERROR:ACTIVITY_MISSING_IN_IMPORT": "Activity missing in imports",
"IMPORT-ERROR:TRIGGER_MISSING_IN_IMPORT": "Trigger missing in imports",
"IMPORT-ERROR:UNKNOWN_APP_TYPE": "Unknown app type/model",
Expand All @@ -50,6 +51,7 @@
"IMPORT-ERROR:ONE_AMONG_CONTENT": "should equal one of the allowed values: {{allowedValues}}",
"IMPORT-ERROR:ACTIVITY_MISSING_CONTENT": "{{ref}} - You will need to install this activity before you can use it in an app.",
"IMPORT-ERROR:TRIGGER_MISSING_CONTENT": "{{ref}} - You will need to install this trigger before you can use it in an app.",
"IMPORT-ERROR:CONTRIB_NOT_INSTALLED_CONTENT": "{{ref}} - You will need to install this contribution before you can use it in an app",
"IMPORT-ERROR:ACTIVITY_MISSING_IN_IMPORT_CONTENT": "{{dataPath}} - {{ref}} activity is missing in the imports",
"IMPORT-ERROR:TRIGGER_MISSING_IN_IMPORT_CONTENT": "{{dataPath}} - {{ref}} trigger is missing in the imports",
"IMPORT-ERROR:UNSUPPORTED_RESOURCE_TYPE": "Unsupported resource type",
Expand Down
29 changes: 25 additions & 4 deletions apps/server/src/modules/transfer/import/import-app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export function importApp(
): App {
const now = new Date().toISOString();
if (rawApp.imports) {
validateImports(rawApp.imports);
validateImports(rawApp.imports, contributions);
}
const importsRefAgent = createFromImports(rawApp.imports, contributions);
const newApp = cleanAndValidateApp(
Expand Down Expand Up @@ -94,10 +94,11 @@ export function importApp(
return newApp;
}

function validateImports(imports) {
function validateImports(imports, contributions) {
const improperImports = imports.filter(
eachImport => !IMPORT_SYNTAX.exec(eachImport.trim())
);

const importsErrors = improperImports.map(importsError => ({
keyword: 'improper-import',
dataPath: '.imports',
Expand All @@ -106,8 +107,28 @@ function validateImports(imports) {
ref: importsError,
},
}));
if (importsErrors.length) {
throw new ValidationError('Validation error in imports', importsErrors);

const contribsNotInstalled = imports.filter(
eachImport => {
const validateImport = IMPORT_SYNTAX.exec(eachImport.trim());
if(validateImport) {
return !contributions.has(validateImport[2]);
}
});

const contribsNotInstalledErrors = contribsNotInstalled.map(
contribRef => ({
keyword: "contrib-not-installed",
message: `contribution "${contribRef}" is not installed`,
params: {
ref: contribRef,
},
}));

const allErrors = [...importsErrors, ...contribsNotInstalledErrors];

if (allErrors.length) {
throw new ValidationError('Validation error in imports', allErrors);
}
}

Expand Down

0 comments on commit 2d6f4d7

Please sign in to comment.