Skip to content

Commit 36716f8

Browse files
Merge pull request dynamodb-toolbox#1009 from dynamodb-toolbox/improve-parser-errors-path
improve parser errors path
2 parents d40b0fa + fe31cdf commit 36716f8

31 files changed

+318
-226
lines changed

src/schema/actions/format/anyOf.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import type { AnyOfAttribute, Attribute } from '~/attributes/index.js'
22
import { DynamoDBToolboxError } from '~/errors/index.js'
3+
import { formatValuePath } from '~/schema/actions/utils/formatValuePath.js'
34

45
import { attrFormatter } from './attribute.js'
56
import type { FormatterReturn, FormatterYield } from './formatter.js'
67
import type { FormatAttrValueOptions } from './options.js'
7-
import { formatValuePath } from './utils.js'
88

99
export function* anyOfAttrFormatter(
1010
attribute: AnyOfAttribute,

src/schema/actions/format/attribute.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { Attribute, RequiredOption } from '~/attributes/index.js'
22
import { DynamoDBToolboxError } from '~/errors/index.js'
3+
import { formatValuePath } from '~/schema/actions/utils/formatValuePath.js'
34

45
import { anyAttrFormatter } from './any.js'
56
import { anyOfAttrFormatter } from './anyOf.js'
@@ -10,7 +11,6 @@ import type { FormatAttrValueOptions } from './options.js'
1011
import { primitiveAttrFormatter } from './primitive.js'
1112
import { recordAttrFormatter } from './record.js'
1213
import { setAttrFormatter } from './set.js'
13-
import { formatValuePath } from './utils.js'
1414

1515
export const requiringOptions = new Set<RequiredOption>(['always', 'atLeastOnce'])
1616

src/schema/actions/format/list.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { ListAttribute } from '~/attributes/index.js'
22
import { DynamoDBToolboxError } from '~/errors/index.js'
3+
import { formatValuePath } from '~/schema/actions/utils/formatValuePath.js'
34
import { isArray } from '~/utils/validation/isArray.js'
45

56
import { attrFormatter } from './attribute.js'
67
import type { FormatterReturn, FormatterYield } from './formatter.js'
78
import type { FormatAttrValueOptions } from './options.js'
8-
import { formatValuePath } from './utils.js'
99
import { matchProjection } from './utils.js'
1010

1111
export function* listAttrFormatter(

src/schema/actions/format/map.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { MapAttribute } from '~/attributes/index.js'
22
import { DynamoDBToolboxError } from '~/errors/index.js'
3+
import { formatValuePath } from '~/schema/actions/utils/formatValuePath.js'
34
import { isObject } from '~/utils/validation/isObject.js'
45

56
import { attrFormatter } from './attribute.js'
67
import type { FormatterReturn, FormatterYield } from './formatter.js'
78
import type { FormatAttrValueOptions } from './options.js'
8-
import { formatValuePath } from './utils.js'
99
import { matchProjection, sanitize } from './utils.js'
1010

1111
export function* mapAttrFormatter(

src/schema/actions/format/primitive.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { PrimitiveAttribute } from '~/attributes/index.js'
22
import { DynamoDBToolboxError } from '~/errors/index.js'
3+
import { formatValuePath } from '~/schema/actions/utils/formatValuePath.js'
34
import type { Transformer } from '~/transformers/index.js'
45
import { isValidPrimitive } from '~/utils/validation/isValidPrimitive.js'
56

67
import type { FormatterReturn, FormatterYield } from './formatter.js'
78
import type { FormatAttrValueOptions } from './options.js'
8-
import { formatValuePath } from './utils.js'
99

1010
export function* primitiveAttrFormatter(
1111
attribute: PrimitiveAttribute,

src/schema/actions/format/record.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { RecordAttribute } from '~/attributes/index.js'
22
import { DynamoDBToolboxError } from '~/errors/index.js'
3+
import { formatValuePath } from '~/schema/actions/utils/formatValuePath.js'
34
import { isObject } from '~/utils/validation/isObject.js'
45

56
import { attrFormatter } from './attribute.js'
67
import { Formatter, type FormatterReturn, type FormatterYield } from './formatter.js'
78
import type { FormatAttrValueOptions } from './options.js'
8-
import { formatValuePath } from './utils.js'
99
import { matchProjection, sanitize } from './utils.js'
1010

1111
export function* recordAttrFormatter(

src/schema/actions/format/set.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { SetAttribute } from '~/attributes/index.js'
22
import { DynamoDBToolboxError } from '~/errors/index.js'
3+
import { formatValuePath } from '~/schema/actions/utils/formatValuePath.js'
34
import { isSet } from '~/utils/validation/isSet.js'
45

56
import { attrFormatter } from './attribute.js'
67
import type { FormatterReturn, FormatterYield } from './formatter.js'
78
import type { FormatAttrValueOptions } from './options.js'
8-
import { formatValuePath } from './utils.js'
99

1010
export function* setAttrFormatter(
1111
attribute: SetAttribute,

src/schema/actions/format/utils.ts

Lines changed: 0 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,3 @@
1-
import type { CharsToEscape } from '~/schema/types/paths.js'
2-
import { isNumber } from '~/utils/validation/isNumber.js'
3-
import { isString } from '~/utils/validation/isString.js'
4-
51
export const sanitize = (str: string): string =>
62
str.replace(/\\/g, '\\\\').replace(/[-[\]/{}()*+?.^$|]/g, '\\$&')
73

@@ -37,35 +33,3 @@ export const matchProjection = (
3733

3834
return { isProjected: true, childrenAttributes }
3935
}
40-
41-
const charsToEscape: CharsToEscape[] = ['[', ']', '.']
42-
export const formatValuePath = (valuePath: (string | number)[] | undefined): string | undefined => {
43-
if (valuePath === undefined || valuePath.length === 0) {
44-
return undefined
45-
}
46-
47-
let path = ''
48-
let isRoot = true
49-
50-
for (const valuePathPart of valuePath) {
51-
if (isString(valuePathPart)) {
52-
const shouldBeEscaped = charsToEscape.some(charToEscape =>
53-
valuePathPart.includes(charToEscape)
54-
)
55-
56-
if (shouldBeEscaped) {
57-
path += `['${valuePathPart}']`
58-
} else {
59-
path += `${isRoot ? '' : '.'}${valuePathPart}`
60-
}
61-
}
62-
63-
if (isNumber(valuePathPart)) {
64-
path += `[${valuePathPart}]`
65-
}
66-
67-
isRoot = false
68-
}
69-
70-
return path
71-
}

src/schema/actions/format/utils.unit.test.ts

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { formatValuePath, matchProjection, sanitize } from './utils.js'
1+
import { matchProjection, sanitize } from './utils.js'
22

33
describe('sanitizes', () => {
44
test('sanitizes str with special chars', () => {
@@ -64,14 +64,3 @@ describe('matchProjection', () => {
6464
})
6565
})
6666
})
67-
68-
describe('formatValuePath', () => {
69-
test('it correctly formats simple paths', () => {
70-
expect(formatValuePath(['foo', 'bar'])).toStrictEqual('foo.bar')
71-
expect(formatValuePath(['foo', 1, 2, 'bar'])).toStrictEqual('foo[1][2].bar')
72-
})
73-
74-
test('it correctly formats complex paths', () => {
75-
expect(formatValuePath(['f.oo', 'ba[r', 'ba]z'])).toStrictEqual(`['f.oo']['ba[r']['ba]z']`)
76-
})
77-
})

src/schema/actions/parse/any.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
11
import type { AnyAttribute } from '~/attributes/index.js'
22
import { cloneDeep } from '~/utils/cloneDeep.js'
33

4-
import type { ParseValueOptions } from './options.js'
4+
import type { ParseAttrValueOptions } from './options.js'
55
import type { ParserReturn, ParserYield } from './parser.js'
66
import { applyCustomValidation } from './utils.js'
77

8-
export function* anyAttrParser<OPTIONS extends ParseValueOptions = {}>(
8+
export function* anyAttrParser<OPTIONS extends ParseAttrValueOptions = {}>(
99
attribute: AnyAttribute,
1010
inputValue: unknown,
1111
options: OPTIONS = {} as OPTIONS

0 commit comments

Comments
 (0)