@@ -10,7 +10,8 @@ import { operatorParameterArity } from './functions/operator-parameter-arity.fun
1010import { uniqAttributeValueName } from './functions/unique-attribute-value-name.function'
1111import { ConditionOperator } from './type/condition-operator.type'
1212import { Expression } from './type/expression.type'
13- import { validateAttributeValue } from './update-expression-builder'
13+ import { validateAttributeType } from './update-expression-builder'
14+ import { dynamicTemplate } from './util'
1415
1516type BuildFilterFn = (
1617 attributePath : string ,
@@ -96,7 +97,7 @@ export function buildFilterExpression(
9697 values = deepFilter ( values , value => value !== undefined )
9798
9899 // check if provided values are valid for given operator
99- validateValues ( operator , values )
100+ validateForOperator ( operator , values )
100101
101102 // load property metadata if model metadata was provided
102103 let propertyMetadata : PropertyMetadata < any > | undefined
@@ -165,7 +166,7 @@ function buildInConditionExpression(
165166 . reduce (
166167 ( result , mappedValue : Attribute | null , index : number ) => {
167168 if ( mappedValue !== null ) {
168- validateAttributeValue ( 'IN condition' , mappedValue , 'S' , 'N' , 'B' )
169+ validateAttributeType ( 'IN condition' , mappedValue , 'S' , 'N' , 'B' )
169170 result [ `${ valuePlaceholder } _${ index } ` ] = mappedValue
170171 }
171172 return result
@@ -201,7 +202,7 @@ function buildBetweenConditionExpression(
201202 throw new Error ( 'make sure to provide an actual value for te BETWEEN operator' )
202203 }
203204 [ mappedValue1 , mappedValue2 ]
204- . forEach ( mv => validateAttributeValue ( 'between' , mv , 'S' , 'N' , 'B' ) )
205+ . forEach ( mv => validateAttributeType ( 'between' , mv , 'S' , 'N' , 'B' ) )
205206
206207 const value2Placeholder = uniqAttributeValueName ( attributePath , [ valuePlaceholder ] . concat ( existingValueNames || [ ] ) )
207208
@@ -244,14 +245,14 @@ function buildDefaultConditionExpression(
244245 const attribute : Attribute | null = toDbOne ( values [ 0 ] , propertyMetadata )
245246 switch ( operator ) {
246247 case 'begins_with' :
247- validateAttributeValue ( `${ operator } condition` , attribute , 'S' , 'B' )
248+ validateAttributeType ( `${ operator } condition` , attribute , 'S' , 'B' )
248249 break
249250 case 'contains' :
250251 case '<' :
251252 case '<=' :
252253 case '>' :
253254 case '>=' :
254- validateAttributeValue ( `${ operator } condition` , attribute , 'N' , 'S' , 'B' )
255+ validateAttributeType ( `${ operator } condition` , attribute , 'N' , 'S' , 'B' )
255256 break
256257 }
257258
@@ -271,51 +272,85 @@ function buildDefaultConditionExpression(
271272 * Every operator requires a predefined arity of parameters, this method checks for the correct arity and throws an Error
272273 * if not correct
273274 *
274- * @param {any[] } values The values which will be applied to the operator function implementation
275+ * @param operator
276+ * @param values The values which will be applied to the operator function implementation, not every operator requires values
275277 * @throws {Error } error Throws an error if the amount of values won't match the operator function parameter arity or
276278 * the given values is not an array
277279 */
278- function validateValues ( operator : ConditionOperator , values ?: any [ ] ) {
279- const parameterArity = operatorParameterArity ( operator )
280+ function validateForOperator ( operator : ConditionOperator , values ?: any [ ] ) {
281+ validateArity ( operator , values )
282+
283+ /*
284+ * validate values if operator supports values
285+ */
286+ if ( ! isFunctionOperator ( operator ) || isFunctionOperator ( operator ) && ! isNoParamFunctionOperator ( operator ) ) {
287+ if ( values && Array . isArray ( values ) && values . length ) {
288+ validateValues ( operator , values )
289+ } else {
290+ // TODO
291+ throw new Error ( 'blub' )
292+ }
293+ }
294+ }
295+
296+ // tslint:disable:no-invalid-template-strings
297+ /*
298+ * error messages for arity issues
299+ */
300+ export const ERR_ARITY_IN = 'expected ${parameterArity} value(s) for operator ${operator}, this is not the right amount of method parameters for this operator (IN operator requires one value of array type)'
301+ export const ERR_ARITY_DEFAULT = 'expected ${parameterArity} value(s) for operator ${operator}, this is not the right amount of method parameters for this operator'
302+
303+ // tslint:enable:no-invalid-template-strings
304+
305+ function validateArity ( operator : ConditionOperator , values ?: any [ ] ) {
280306 if ( values === null || values === undefined ) {
281307 if ( isFunctionOperator ( operator ) && ! isNoParamFunctionOperator ( operator ) ) {
282308 // the operator needs some values to work
283- throw new Error (
284- `expected ${ parameterArity } value(s) for operator ${ operator } , this is not the right amount of method parameters for this operator` ,
285- )
309+ throw new Error ( dynamicTemplate ( ERR_ARITY_DEFAULT , { parameterArity : operatorParameterArity ( operator ) , operator} ) )
286310 }
287311 } else if ( values && Array . isArray ( values ) ) {
312+ const parameterArity = operatorParameterArity ( operator )
288313 // check for correct amount of values
289314 if ( values . length !== parameterArity ) {
290315 switch ( operator ) {
291316 case 'IN' :
292- throw new Error (
293- `expected ${ parameterArity } value(s) for operator ${ operator } , this is not the right amount of method parameters for this operator (IN operator requires one value of array type)` ,
294- )
317+ throw new Error ( dynamicTemplate ( ERR_ARITY_IN , { parameterArity, operator } ) )
295318 default :
296- throw new Error (
297- `expected ${ parameterArity } value(s) for operator ${ operator } , this is not the right amount of method parameters for this operator` ,
298- )
319+ throw new Error ( dynamicTemplate ( ERR_ARITY_DEFAULT , { parameterArity, operator } ) )
299320 }
300321 }
322+ }
323+ }
301324
302- // some additional operator dependent validation
303- switch ( operator ) {
304- case 'BETWEEN' :
305- // values must be the same type
306- if ( typeOf ( values [ 0 ] ) !== typeOf ( values [ 1 ] ) ) {
307- throw new Error (
308- `both values for operator BETWEEN must have the same type, got ${ typeOf ( values [ 0 ] ) } and ${ typeOf (
309- values [ 1 ] ,
310- ) } `,
311- )
312- }
313- break
314- case 'IN' :
315- if ( ! Array . isArray ( values [ 0 ] ) ) {
316- throw new Error ( 'the provided value for IN operator must be an array' )
317- }
318- }
325+
326+ /*
327+ * error message for wrong operator values
328+ */
329+ // tslint:disable:no-invalid-template-strings
330+ export const ERR_VALUES_BETWEEN_TYPE = 'both values for operator BETWEEN must have the same type, got ${value1} and ${value2}'
331+ export const ERR_VALUES_IN = 'the provided value for IN operator must be an array'
332+ // tslint:enable:no-invalid-template-strings
333+
334+ /**
335+ * Every operator has some constraints about the values it supports, this method makes sure everything is fine for given
336+ * operator and values
337+ */
338+ function validateValues ( operator : ConditionOperator , values : any [ ] ) {
339+ // some additional operator dependent validation
340+ switch ( operator ) {
341+ case 'BETWEEN' :
342+ // values must be the same type
343+ if ( typeOf ( values [ 0 ] ) !== typeOf ( values [ 1 ] ) ) {
344+ throw new Error ( dynamicTemplate (
345+ ERR_VALUES_BETWEEN_TYPE ,
346+ { value1 : typeOf ( values [ 0 ] ) , value2 : typeOf ( values [ 1 ] ) }
347+ ) )
348+ }
349+ break
350+ case 'IN' :
351+ if ( ! Array . isArray ( values [ 0 ] ) ) {
352+ throw new Error ( ERR_VALUES_IN )
353+ }
319354 }
320355}
321356
0 commit comments