mirror of
https://github.com/bitwarden/browser.git
synced 2025-02-13 00:51:45 +01:00
parse validation errors from error response model
This commit is contained in:
parent
fc5fcb905f
commit
035b4e1dd5
@ -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,16 +23,38 @@ export class ErrorResponse {
|
|||||||
}
|
}
|
||||||
|
|
||||||
getSingleMessage(): string {
|
getSingleMessage(): string {
|
||||||
if (this.validationErrors) {
|
if (this.validationErrors == null) {
|
||||||
for (const key in this.validationErrors) {
|
return this.message;
|
||||||
if (!this.validationErrors.hasOwnProperty(key)) {
|
}
|
||||||
continue;
|
for (const key in this.validationErrors) {
|
||||||
}
|
if (!this.validationErrors.hasOwnProperty(key)) {
|
||||||
if (this.validationErrors[key].length) {
|
continue;
|
||||||
return this.validationErrors[key][0];
|
}
|
||||||
}
|
if (this.validationErrors[key].length) {
|
||||||
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user