Skip to content

Commit 480e136

Browse files
author
Mat Jones
committed
update error checking and record mapping in ResultRecord
1 parent 5c46e4c commit 480e136

1 file changed

Lines changed: 18 additions & 17 deletions

File tree

src/view-models/result-record.ts

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ import { Record } from "immutable";
33
import { ResultErrorRecord } from "./result-error-record";
44
import { Result } from "../interfaces/result";
55
import { ErrorType } from "../enumerations/error-type";
6+
import { RecordUtils } from "../utilities/record-utils";
67

78
const defaultValues: Result<any> = {
89
errors: undefined,
@@ -19,15 +20,13 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
1920

2021
constructor(params?: Result<T>) {
2122
if (params == null) {
22-
params = {};
23+
params = Object.assign(defaultValues, params);
2324
}
2425

2526
if (CollectionUtils.hasValues(params.errors)) {
2627
const errors = params.errors as any[];
2728
params.errors = errors.map((error) =>
28-
error instanceof ResultErrorRecord
29-
? error
30-
: new ResultErrorRecord(error)
29+
RecordUtils.ensureRecord(error, ResultErrorRecord)
3130
);
3231
}
3332

@@ -62,7 +61,6 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
6261

6362
/**
6463
* Evaluates whether there are any errors on the result
65-
* @returns {boolean}
6664
*/
6765
public doesNotHaveErrors(): boolean {
6866
return !this.hasErrors();
@@ -72,11 +70,11 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
7270
* Returns total number of errors
7371
*/
7472
public errorCount(): number {
75-
if (this.errors == null) {
73+
if (this.doesNotHaveErrors()) {
7674
return 0;
7775
}
7876

79-
return CollectionUtils.hasValues(this.errors) ? this.errors.length : 0;
77+
return this.errors!.length;
8078
}
8179

8280
/**
@@ -88,15 +86,17 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
8886
}
8987

9088
/**
91-
* Determines if the result contains an error for the supplied key
92-
* @param key error key for which to search
89+
* Determines if the result contains an error for the supplied key(s)
90+
* @param keys error keys for which to search
9391
*/
94-
public hasErrorFor(key: string): boolean {
95-
if (this.errors == null || !this.hasErrors()) {
92+
public hasErrorFor(...keys: string[]): boolean {
93+
if (this.doesNotHaveErrors()) {
9694
return false;
9795
}
9896

99-
return this.errors.some((e) => e.key === key);
97+
return this.errors!.some((error: ResultErrorRecord) =>
98+
keys.some((key: string) => key === error.key)
99+
);
100100
}
101101

102102
/**
@@ -110,7 +110,7 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
110110
* Map all errors into simple string array
111111
*/
112112
public listErrors(): string[] {
113-
if (!this.hasErrors()) {
113+
if (this.doesNotHaveErrors()) {
114114
return [];
115115
}
116116
const errors = this.errors as ResultErrorRecord[];
@@ -121,9 +121,10 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
121121
* Map all error messages into a simple string array.
122122
*/
123123
public listErrorMessages(): string[] {
124-
if (!this.hasErrors()) {
124+
if (this.doesNotHaveErrors()) {
125125
return [];
126126
}
127+
127128
const errors = this.errors as ResultErrorRecord[];
128129
return errors
129130
.map((e) => String(e.message) || "")
@@ -134,7 +135,7 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
134135
* Merges new values into the record and returns a new instance.
135136
*
136137
* @param {Partial<Result<T>>} values
137-
* @returns {ResultRecord}
138+
* @returns {ResultRecord<T>}
138139
* @memberof ResultRecord
139140
*/
140141
public with(values: Partial<Result<T>>): ResultRecord<T> {
@@ -169,9 +170,9 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
169170
}
170171

171172
// -----------------------------------------------------------------------------------------
172-
// #region Export
173+
// #region Exports
173174
// -----------------------------------------------------------------------------------------
174175

175176
export { ResultRecord };
176177

177-
// #endregion Export
178+
// #endregion Exports

0 commit comments

Comments
 (0)