Commit 40bcda62 authored by Thomas Randolph's avatar Thomas Randolph

Add server-sent name-field validation errors

parent 34ac2c84
...@@ -13,6 +13,10 @@ const DEFAULT_NAME_FOR_LICENSE_REPORT = 'License-Check'; ...@@ -13,6 +13,10 @@ const DEFAULT_NAME_FOR_LICENSE_REPORT = 'License-Check';
const DEFAULT_NAME_FOR_VULNERABILITY_CHECK = 'Vulnerability-Check'; const DEFAULT_NAME_FOR_VULNERABILITY_CHECK = 'Vulnerability-Check';
const READONLY_NAMES = [DEFAULT_NAME_FOR_LICENSE_REPORT, DEFAULT_NAME_FOR_VULNERABILITY_CHECK]; const READONLY_NAMES = [DEFAULT_NAME_FOR_LICENSE_REPORT, DEFAULT_NAME_FOR_VULNERABILITY_CHECK];
function mapServerResponseToValidationErrors(messages) {
return Object.entries(messages).flatMap(([key, msgs]) => msgs.map(msg => `${key} ${msg}`));
}
export default { export default {
components: { components: {
ApproversList, ApproversList,
...@@ -50,6 +54,7 @@ export default { ...@@ -50,6 +54,7 @@ export default {
showValidation: false, showValidation: false,
isFallback: false, isFallback: false,
containsHiddenGroups: false, containsHiddenGroups: false,
serverValidationErrors: [],
...this.getInitialData(), ...this.getInitialData(),
}; };
// TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114 // TODO: Remove feature flag in https://gitlab.com/gitlab-org/gitlab/-/issues/235114
...@@ -98,11 +103,17 @@ export default { ...@@ -98,11 +103,17 @@ export default {
return invalidObject; return invalidObject;
}, },
invalidName() { invalidName() {
if (!this.isMultiSubmission) { let error = '';
return '';
if (this.isMultiSubmission) {
if (this.serverValidationErrors.includes('name has already been taken')) {
error = __('Rule name is already taken.');
} else if (!this.name) {
error = __('Please provide a name');
}
} }
return !this.name ? __('Please provide a name') : ''; return error;
}, },
invalidApprovalsRequired() { invalidApprovalsRequired() {
if (!isNumber(this.approvalsRequired)) { if (!isNumber(this.approvalsRequired)) {
...@@ -204,15 +215,27 @@ export default { ...@@ -204,15 +215,27 @@ export default {
* - Multi rule? * - Multi rule?
*/ */
submit() { submit() {
let submission;
this.serverValidationErrors = [];
if (!this.validate()) { if (!this.validate()) {
return Promise.resolve(); submission = Promise.resolve();
} else if (this.isFallbackSubmission) { } else if (this.isFallbackSubmission) {
return this.submitFallback(); submission = this.submitFallback();
} else if (!this.isMultiSubmission) { } else if (!this.isMultiSubmission) {
return this.submitSingleRule(); submission = this.submitSingleRule();
} else {
submission = this.submitRule();
} }
return this.submitRule(); submission.catch(failureResponse => {
this.serverValidationErrors = mapServerResponseToValidationErrors(
failureResponse?.response?.data?.message || {},
);
});
return submission;
}, },
/** /**
* Submit the rule, by either put-ing or post-ing. * Submit the rule, by either put-ing or post-ing.
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment