Skip to content

Commit c1b8a42

Browse files
feat(empty-strings): drop empty string strictness to allow feature
BREAKING CHANGE: empty strings are now valid dynamoDB values (see https://aws.amazon.com/about-aws/whats-new/2020/05/amazon-dynamodb-now-supports-empty-values-for-non-key-string-and-binary-attributes-in-dynamodb-tables/ for official statement) and thus they are mapped to and from db.
1 parent 29134f9 commit c1b8a42

File tree

3 files changed

+12
-7
lines changed

3 files changed

+12
-7
lines changed

src/mapper/for-type/string.mapper.spec.ts

+5-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ describe('string mapper', () => {
99

1010
it('should work (empty string)', () => {
1111
const attributeValue = StringMapper.toDb('')
12-
expect(attributeValue).toBe(null)
12+
expect(attributeValue).toStrictEqual({ S: '' })
1313
})
1414

1515
it('should work (null)', () => {
@@ -28,6 +28,10 @@ describe('string mapper', () => {
2828
const stringValue = StringMapper.fromDb({ S: 'myStringValue' })
2929
expect(stringValue).toBe('myStringValue')
3030
})
31+
it('should allow empty string values', () => {
32+
const stringValue = StringMapper.fromDb({ S: '' })
33+
expect(stringValue).toBe('')
34+
})
3135
it('should throw if not a string attribute', () => {
3236
expect(() => StringMapper.fromDb(<any>{ N: '8' })).toThrow()
3337
})

src/mapper/for-type/string.mapper.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,16 @@ import { StringAttribute } from '../type/attribute.type'
55
import { MapperForType } from './base.mapper'
66

77
function stringFromDb(attributeValue: StringAttribute): string {
8-
if (attributeValue.S) {
8+
if (attributeValue.S || attributeValue.S === '') {
99
return attributeValue.S
1010
} else {
1111
throw new Error(`there is no S(tring) value defined on given attribute value: ${JSON.stringify(attributeValue)}`)
1212
}
1313
}
1414

1515
function stringToDb(modelValue: string): StringAttribute | null {
16-
// an empty string is not a valid value for string attribute
17-
if (modelValue === '' || modelValue === null || modelValue === undefined) {
16+
// an empty string is valid for a string attribute
17+
if (modelValue === null || modelValue === undefined) {
1818
return null
1919
} else {
2020
return { S: modelValue }

src/mapper/mapper.spec.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ describe('Mapper', () => {
6767

6868
it('string (empty)', () => {
6969
const attrValue = <StringAttribute>toDbOne('')!
70-
expect(attrValue).toBeNull()
70+
expect(attrValue.S).toStrictEqual('')
7171
})
7272

7373
it('number', () => {
@@ -801,7 +801,7 @@ describe('Mapper', () => {
801801
// OK
802802
id: 'myId',
803803

804-
// x -> empty strings are not valid
804+
// x -> empty strings are valid
805805
name: '',
806806

807807
// x -> empty set is not valid
@@ -824,7 +824,8 @@ describe('Mapper', () => {
824824
expect(toDbValue.id).toBeDefined()
825825
expect(keyOf(toDbValue.id)).toBe('S')
826826

827-
expect(toDbValue.name).toBeUndefined()
827+
expect(toDbValue.name).toBeDefined()
828+
expect(keyOf(toDbValue.name)).toBe('S')
828829

829830
expect(toDbValue.roles).toBeUndefined()
830831

0 commit comments

Comments
 (0)