Skip to content

Commit 02d6e1b

Browse files
Merge pull request #134 from shiftcode/#119-expression-validation
#119 expression validation
2 parents 75133e0 + f19e81c commit 02d6e1b

38 files changed

+827
-461
lines changed

src/dynamo/expression/comparator-operators.const.ts

Lines changed: 0 additions & 1 deletion
This file was deleted.

src/dynamo/expression/condition-expression-builder.spec.ts

Lines changed: 46 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,18 @@ import { has } from 'lodash'
22
import { ComplexModel } from '../../../test/models'
33
import { Model, PartitionKey, Property } from '../../decorator/impl'
44
import { metadataForClass } from '../../decorator/metadata'
5-
import { buildFilterExpression, deepFilter } from './condition-expression-builder'
5+
import { typeOf } from '../../mapper'
6+
import {
7+
buildFilterExpression,
8+
deepFilter,
9+
ERR_ARITY_DEFAULT,
10+
ERR_ARITY_IN,
11+
ERR_VALUES_BETWEEN_TYPE,
12+
ERR_VALUES_IN,
13+
} from './condition-expression-builder'
14+
import { operatorParameterArity } from './functions/operator-parameter-arity.function'
15+
import { ConditionOperator } from './type'
16+
import { dynamicTemplate } from './util'
617

718
@Model()
819
class MyModel {
@@ -264,24 +275,6 @@ describe('expressions', () => {
264275
expect(has(condition.attributeValues, ':creationDate_2')).toBeTruthy()
265276
expect(condition.attributeValues[':creationDate_2']).toEqual({ S: date2.toISOString() })
266277
})
267-
268-
it('should throw error for wrong value arity', () => {
269-
expect(() => buildFilterExpression('age', 'attribute_type', [], undefined, undefined)).toThrowError(
270-
'expected 1 value(s) for operator attribute_type, this is not the right amount of method parameters for this operator',
271-
)
272-
})
273-
274-
it('should throw error for wrong value arity', () => {
275-
expect(() => buildFilterExpression('age', 'attribute_type', [undefined], undefined, undefined)).toThrowError(
276-
'expected 1 value(s) for operator attribute_type, this is not the right amount of method parameters for this operator',
277-
)
278-
})
279-
280-
it('should throw error for wrong value type', () => {
281-
expect(() => buildFilterExpression('age', 'IN', ['myValue', 'mySecondValue'], undefined, undefined)).toThrowError(
282-
'expected 1 value(s) for operator IN, this is not the right amount of method parameters for this operator (IN operator requires one value of array type)',
283-
)
284-
})
285278
})
286279

287280
describe('operator nested attributes', () => {
@@ -312,4 +305,38 @@ describe('expressions', () => {
312305
expect(condition.attributeValues).toEqual({ ':person__birthdays_at_5__year': { N: '2016' } })
313306
})
314307
})
308+
309+
describe('validation', () => {
310+
describe('arity', () => {
311+
it('should throw default error for wrong arity', () => {
312+
const operator: ConditionOperator = 'attribute_type'
313+
expect(() => buildFilterExpression('age', operator, [], undefined, undefined)).toThrow(
314+
dynamicTemplate(ERR_ARITY_DEFAULT, { parameterArity: operatorParameterArity(operator), operator }),
315+
)
316+
})
317+
318+
it('should throw error for wrong IN arity', () => {
319+
const operator: ConditionOperator = 'IN'
320+
expect(() =>
321+
buildFilterExpression('age', operator, ['myValue', 'mySecondValue'], undefined, undefined),
322+
).toThrowError(dynamicTemplate(ERR_ARITY_IN, { parameterArity: operatorParameterArity(operator), operator }))
323+
})
324+
})
325+
326+
describe('operator values', () => {
327+
it('should throw error for wrong IN values', () => {
328+
const operator: ConditionOperator = 'IN'
329+
expect(() => buildFilterExpression('age', operator, ['myValue'], undefined, undefined)).toThrowError(
330+
ERR_VALUES_IN,
331+
)
332+
})
333+
334+
it('should throw error for wrong value type', () => {
335+
const operator: ConditionOperator = 'BETWEEN'
336+
expect(() => buildFilterExpression('age', operator, ['myValue', 2], undefined, undefined)).toThrowError(
337+
dynamicTemplate(ERR_VALUES_BETWEEN_TYPE, { value1: typeOf('myValue'), value2: typeOf(2) }),
338+
)
339+
})
340+
})
341+
})
315342
})

0 commit comments

Comments
 (0)