mirror of
https://github.com/bitwarden/browser
synced 2025-12-16 16:23:44 +00:00
parse validation errors from error response model
This commit is contained in:
@@ -3,6 +3,8 @@ import { Injectable } from '@angular/core';
|
|||||||
import { I18nService } from '../../abstractions/i18n.service';
|
import { I18nService } from '../../abstractions/i18n.service';
|
||||||
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
import { PlatformUtilsService } from '../../abstractions/platformUtils.service';
|
||||||
|
|
||||||
|
import { ErrorResponse } from '../../models/response/errorResponse';
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class ValidationService {
|
export class ValidationService {
|
||||||
constructor(private i18nService: I18nService, private platformUtilsService: PlatformUtilsService) { }
|
constructor(private i18nService: I18nService, private platformUtilsService: PlatformUtilsService) { }
|
||||||
@@ -15,23 +17,10 @@ export class ValidationService {
|
|||||||
errors.push(data);
|
errors.push(data);
|
||||||
} else if (data == null || typeof data !== 'object') {
|
} else if (data == null || typeof data !== 'object') {
|
||||||
errors.push(defaultErrorMessage);
|
errors.push(defaultErrorMessage);
|
||||||
} else if (data.validationErrors == null) {
|
} else if (data.validationErrors != null) {
|
||||||
errors.push(data.message ? data.message : defaultErrorMessage);
|
errors.concat((data as ErrorResponse).getAllMessages());
|
||||||
} else {
|
} else {
|
||||||
for (const key in data.validationErrors) {
|
errors.push(data.message ? data.message : defaultErrorMessage);
|
||||||
if (!data.validationErrors.hasOwnProperty(key)) {
|
|
||||||
continue;
|
|
||||||
}
|
|
||||||
|
|
||||||
data.validationErrors[key].forEach((item: string) => {
|
|
||||||
let prefix = '';
|
|
||||||
if (key.indexOf('[') > -1 && key.indexOf(']') > -1) {
|
|
||||||
const lastSep = key.lastIndexOf('.');
|
|
||||||
prefix = key.substr(0, lastSep > -1 ? lastSep : key.length) + ': ';
|
|
||||||
}
|
|
||||||
errors.push(prefix + item);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (errors.length === 1) {
|
if (errors.length === 1) {
|
||||||
|
|||||||
@@ -23,7 +23,9 @@ export class ErrorResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getSingleMessage(): string {
|
getSingleMessage(): string {
|
||||||
if (this.validationErrors) {
|
if (this.validationErrors == null) {
|
||||||
|
return this.message;
|
||||||
|
}
|
||||||
for (const key in this.validationErrors) {
|
for (const key in this.validationErrors) {
|
||||||
if (!this.validationErrors.hasOwnProperty(key)) {
|
if (!this.validationErrors.hasOwnProperty(key)) {
|
||||||
continue;
|
continue;
|
||||||
@@ -32,7 +34,27 @@ export class ErrorResponse {
|
|||||||
return this.validationErrors[key][0];
|
return this.validationErrors[key][0];
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
return this.message;
|
return this.message;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
getAllMessages(): string[] {
|
||||||
|
const messages: string[] = [];
|
||||||
|
if (this.validationErrors == null) {
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
|
for (const key in this.validationErrors) {
|
||||||
|
if (!this.validationErrors.hasOwnProperty(key)) {
|
||||||
|
continue;
|
||||||
|
}
|
||||||
|
this.validationErrors[key].forEach((item: string) => {
|
||||||
|
let prefix = '';
|
||||||
|
if (key.indexOf('[') > -1 && key.indexOf(']') > -1) {
|
||||||
|
const lastSep = key.lastIndexOf('.');
|
||||||
|
prefix = key.substr(0, lastSep > -1 ? lastSep : key.length) + ': ';
|
||||||
|
}
|
||||||
|
messages.push(prefix + item);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
return messages;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user