@@ -3,6 +3,7 @@ import { Record } from "immutable";
33import { ResultErrorRecord } from "./result-error-record" ;
44import { Result } from "../interfaces/result" ;
55import { ErrorType } from "../enumerations/error-type" ;
6+ import { RecordUtils } from "../utilities/record-utils" ;
67
78const 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
@@ -60,27 +59,44 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
6059 return this . _addErrorByType ( key , message , ErrorType . ValidationError ) ;
6160 }
6261
62+ /**
63+ * Evaluates whether there are any errors on the result
64+ */
65+ public doesNotHaveErrors ( ) : boolean {
66+ return ! this . hasErrors ( ) ;
67+ }
68+
6369 /**
6470 * Returns total number of errors
6571 */
6672 public errorCount ( ) : number {
67- if ( this . errors == null ) {
73+ if ( this . doesNotHaveErrors ( ) ) {
6874 return 0 ;
6975 }
7076
71- return CollectionUtils . hasValues ( this . errors ) ? this . errors . length : 0 ;
77+ return this . errors ! . length ;
7278 }
7379
7480 /**
75- * Determines if the result contains an error for the supplied key
76- * @param key error key for which to search
81+ * Returns an error message for a given key
82+ * @param key
7783 */
78- public hasErrorFor ( key : string ) : boolean {
79- if ( this . errors == null || ! this . hasErrors ( ) ) {
84+ public getErrorMessageFor ( key : string ) : string | undefined {
85+ return this . errors ?. find ( ( e ) => e . key === key ) ?. message ;
86+ }
87+
88+ /**
89+ * Determines if the result contains an error for the supplied key(s)
90+ * @param keys error keys for which to search
91+ */
92+ public hasErrorFor ( ...keys : string [ ] ) : boolean {
93+ if ( this . doesNotHaveErrors ( ) ) {
8094 return false ;
8195 }
8296
83- return this . errors . some ( ( e ) => e . key === key ) ;
97+ return this . errors ! . some ( ( error : ResultErrorRecord ) =>
98+ keys . some ( ( key : string ) => key === error . key )
99+ ) ;
84100 }
85101
86102 /**
@@ -94,7 +110,7 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
94110 * Map all errors into simple string array
95111 */
96112 public listErrors ( ) : string [ ] {
97- if ( ! this . hasErrors ( ) ) {
113+ if ( this . doesNotHaveErrors ( ) ) {
98114 return [ ] ;
99115 }
100116 const errors = this . errors as ResultErrorRecord [ ] ;
@@ -105,9 +121,10 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
105121 * Map all error messages into a simple string array.
106122 */
107123 public listErrorMessages ( ) : string [ ] {
108- if ( ! this . hasErrors ( ) ) {
124+ if ( this . doesNotHaveErrors ( ) ) {
109125 return [ ] ;
110126 }
127+
111128 const errors = this . errors as ResultErrorRecord [ ] ;
112129 return errors
113130 . map ( ( e ) => String ( e . message ) || "" )
@@ -118,7 +135,7 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
118135 * Merges new values into the record and returns a new instance.
119136 *
120137 * @param {Partial<Result<T>> } values
121- * @returns {ResultRecord }
138+ * @returns {ResultRecord<T> }
122139 * @memberof ResultRecord
123140 */
124141 public with ( values : Partial < Result < T > > ) : ResultRecord < T > {
@@ -153,9 +170,9 @@ class ResultRecord<T> extends Record(defaultValues) implements Result<T> {
153170}
154171
155172// -----------------------------------------------------------------------------------------
156- // #region Export
173+ // #region Exports
157174// -----------------------------------------------------------------------------------------
158175
159176export { ResultRecord } ;
160177
161- // #endregion Export
178+ // #endregion Exports
0 commit comments