Skip to content

Commit

Permalink
fix: array index mapping error handling (#115)
Browse files Browse the repository at this point in the history
* fix: array index mapping error handling

* chore: upgrade github workflow
  • Loading branch information
koladilip authored Jul 12, 2024
1 parent 0d09b0b commit 85fefcf
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 2 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ jobs:
coverage:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v1
- uses: actions/checkout@v4
- uses: ArtiomTr/jest-coverage-report-action@v2
1 change: 1 addition & 0 deletions src/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,7 @@ export type FlatMappingPaths = {
to?: string;
input?: string;
output?: string;
[key: string]: any;
};

export type FlatMappingAST = FlatMappingPaths & {
Expand Down
10 changes: 9 additions & 1 deletion src/utils/converter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,15 @@ function processArrayIndexFilter(
? flatMapping.inputExpr
: createObjectExpression();
}
return currrentOutputPropAST.value.elements[filterIndex];
const objectExpr = currrentOutputPropAST.value.elements[filterIndex];
if (!isLastPart && objectExpr?.type !== SyntaxType.OBJECT_EXPR) {
throw new JsonTemplateMappingError(
'Invalid mapping: invalid array index mapping',
flatMapping.input as string,
flatMapping.output as string,
);
}
return objectExpr;
}

function isPathWithEmptyPartsAndObjectRoot(expr: Expression) {
Expand Down
4 changes: 4 additions & 0 deletions test/scenarios/mappings/data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,10 @@ export const data: Scenario[] = [
],
},
},
{
mappingsPath: 'invalid_array_index_mappings.json',
error: 'Invalid mapping',
},
{
description: 'Index mappings in last part',
mappings: [
Expand Down
10 changes: 10 additions & 0 deletions test/scenarios/mappings/invalid_array_index_mappings.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"from": "$.products[*].a",
"to": "$.items[*].a"
},
{
"from": "$.products[*].b",
"to": "$.items[0].b"
}
]

0 comments on commit 85fefcf

Please sign in to comment.