@@ -29,6 +29,7 @@ import { copyDescriptionAndInheritedFields } from '../meta/copyDescriptionAndInh
29
29
import { removeInheritedFields } from '../meta/removeInheritedFields' ;
30
30
import { getNextCode } from './getNextCode' ;
31
31
32
+ import { errorReturn } from '@imports/utils/return' ;
32
33
import { BCRYPT_SALT_ROUNDS } from '../consts' ;
33
34
34
35
const regexUtils = {
@@ -74,80 +75,38 @@ const ALLOWED_CURRENCIES = ['BRL'];
74
75
75
76
export async function validateAndProcessValueFor ( { meta, fieldName, value, actionType, objectOriginalValues, objectNewValues, idsToUpdate } ) {
76
77
if ( meta == null ) {
77
- return {
78
- success : false ,
79
- errors : [
80
- {
81
- message : `MetaObject.Meta does not exists` ,
82
- } ,
83
- ] ,
84
- } ;
78
+ return errorReturn ( `MetaObject.Meta does not exists` ) ;
85
79
}
86
80
87
81
const field = meta . fields [ fieldName ] ;
88
82
89
83
if ( ! field ) {
90
- return {
91
- success : false ,
92
- errors : [
93
- {
94
- message : `Field ${ fieldName } does not exists on ${ meta . _id } ` ,
95
- } ,
96
- ] ,
97
- } ;
84
+ return errorReturn ( `Field ${ fieldName } does not exists on ${ meta . _id } ` ) ;
98
85
}
99
86
100
87
// Validate required fields
101
88
102
89
if ( field . isRequired === true && ( value == null || ( typeof value === 'string' && size ( value ) === 0 ) ) ) {
103
- return {
104
- success : false ,
105
- errors : [
106
- {
107
- message : `Field ${ fieldName } is required` ,
108
- } ,
109
- ] ,
110
- } ;
90
+ return errorReturn ( `Field ${ fieldName } is required` ) ;
111
91
}
112
92
113
93
// Validate List fields
114
94
if ( field . isList === true ) {
115
95
if ( field . maxElements && field . maxElements > 0 ) {
116
96
if ( ! isArray ( value ) || value . length > field . maxElements ) {
117
- return {
118
- success : false ,
119
- errors : [
120
- {
121
- message : `Value for field ${ fieldName } must be array with the maximum of ${ field . maxElements } item(s)` ,
122
- } ,
123
- ] ,
124
- } ;
97
+ return errorReturn ( `Value for field ${ fieldName } must be array with the maximum of ${ field . maxElements } item(s)` ) ;
125
98
}
126
99
}
127
100
128
101
if ( field . minElements && field . minElements > 0 ) {
129
102
if ( ! isArray ( value ) || value . length < field . minElements ) {
130
- return {
131
- success : false ,
132
- errors : [
133
- {
134
- message : `Value for field ${ fieldName } must be array with the minimum of ${ field . minElements } item(s)` ,
135
- } ,
136
- ] ,
137
- } ;
103
+ return errorReturn ( `Value for field ${ fieldName } must be array with the minimum of ${ field . minElements } item(s)` ) ;
138
104
}
139
105
}
140
106
141
107
if ( field . isAllowDuplicates === false && isArray ( value ) ) {
142
108
if ( value . some ( ( itemA , indexA ) => value . some ( ( itemB , indexB ) => indexA !== indexB && isEqual ( itemA , itemB ) ) ) ) {
143
- return {
144
- success : false ,
145
- errors : [
146
- {
147
- message : `Value for field ${ fieldName } must be array no duplicated values` ,
148
- } ,
149
- ] ,
150
- } ;
109
+ return errorReturn ( `Value for field ${ fieldName } must be a list with no duplicated values` ) ;
151
110
}
152
111
}
153
112
}
@@ -157,14 +116,7 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
157
116
if ( isNumber ( field . minSelected ) ) {
158
117
if ( field . minSelected === 1 ) {
159
118
if ( ! value || ( isArray ( value ) && value . length === 0 ) ) {
160
- return {
161
- success : false ,
162
- errors : [
163
- {
164
- message : `Value for field ${ fieldName } must be an array with at least 1 item` ,
165
- } ,
166
- ] ,
167
- } ;
119
+ return errorReturn ( `Value for field ${ fieldName } must be an array with at least 1 item` ) ;
168
120
}
169
121
}
170
122
}
@@ -197,26 +149,12 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
197
149
const collection = MetaObject . Collections [ meta . name ] ;
198
150
199
151
if ( collection == null ) {
200
- return {
201
- success : false ,
202
- errors : [
203
- {
204
- message : `Collection for ${ meta . name } does not exists` ,
205
- } ,
206
- ] ,
207
- } ;
152
+ return errorReturn ( `Collection for ${ meta . name } does not exists` ) ;
208
153
}
209
154
210
155
const count = await collection . countDocuments ( query ) ;
211
156
if ( count > 0 ) {
212
- return {
213
- success : false ,
214
- errors : [
215
- {
216
- message : `Value for field ${ fieldName } must be unique` ,
217
- } ,
218
- ] ,
219
- } ;
157
+ return errorReturn ( `Value for field ${ fieldName } must be unique` ) ;
220
158
}
221
159
}
222
160
@@ -521,14 +459,7 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
521
459
522
460
if ( isNumber ( field . size ) && field . size > 0 ) {
523
461
if ( value . length > field . size ) {
524
- return {
525
- success : false ,
526
- errors : [
527
- {
528
- message : `Value for field ${ fieldName } must be less than ${ field . size } characters` ,
529
- } ,
530
- ] ,
531
- } ;
462
+ return errorReturn ( `Value for field ${ fieldName } must be less than ${ field . size } characters` ) ;
532
463
}
533
464
}
534
465
@@ -1051,27 +982,13 @@ export async function validateAndProcessValueFor({ meta, fieldName, value, actio
1051
982
1052
983
const lookupCollection = MetaObject . Collections [ field . document ] ;
1053
984
if ( lookupCollection == null ) {
1054
- return {
1055
- success : false ,
1056
- errors : [
1057
- {
1058
- message : `Collection ${ field . document } not found` ,
1059
- } ,
1060
- ] ,
1061
- } ;
985
+ return errorReturn ( `Collection ${ field . document } not found` ) ;
1062
986
}
1063
987
1064
988
const record = await lookupCollection . findOne ( { _id : value . _id } ) ;
1065
989
1066
990
if ( record == null ) {
1067
- return {
1068
- success : false ,
1069
- errors : [
1070
- {
1071
- message : `Record not found for field ${ fieldName } with _id [${ value . _id } ] on document [${ field . document } ]` ,
1072
- } ,
1073
- ] ,
1074
- } ;
991
+ return errorReturn ( `Record not found for field ${ fieldName } with _id [${ value . _id } ] on document [${ field . document } ]` ) ;
1075
992
}
1076
993
1077
994
const inheritedFieldsResult = await copyDescriptionAndInheritedFields ( {
0 commit comments