Skip to content
This repository has been archived by the owner on Dec 12, 2020. It is now read-only.

Commit

Permalink
fix: Fix guess array element issue on type array of Object (#36)
Browse files Browse the repository at this point in the history
  • Loading branch information
ktutnik committed Feb 3, 2020
1 parent cb99203 commit 07932ab
Show file tree
Hide file tree
Showing 5 changed files with 244 additions and 173 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"license": "MIT",
"dependencies": {
"@types/validator": "^12.0.1",
"tinspector": "^2.2.9",
"tinspector": "^2.2.10",
"tslib": "^1.10.0",
"validator": "^12.1.0"
},
Expand Down
5 changes: 4 additions & 1 deletion src/visitor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,10 @@ function objectVisitor(value: any, ast: ObjectNode, opt: VisitorOption): Result
}

function visitor(value: any, ast: SuperNode, opt: VisitorOption): Result {
if (value === undefined || value === null || value.constructor === ast.type || ast.type === Object) return { value }
if (value === undefined || value === null)
return { value }
if (value.constructor === ast.type || ast.type === Object)
return { value: ast.kind === "Array" && opt.guessArrayElement && !Array.isArray(value) ? [value] : value }
return visitorMap[ast.kind](value, ast as any, opt)
}

Expand Down
35 changes: 35 additions & 0 deletions test/__snapshots__/converter-array.spec.ts.snap
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
// Jest Snapshot v1, https://goo.gl/fbAQLP

exports[`Array Converter Should convert array of Object as is 1`] = `
Array [
Object {
"birthday": "2018-2-2",
"deceased": "ON",
"id": "200",
"name": "Mimi",
},
Object {
"birthday": "2018-2-2",
"deceased": "ON",
"id": "200",
"name": "Mimi",
},
Object {
"birthday": "2018-2-2",
"deceased": "ON",
"id": "200",
"name": "Mimi",
},
]
`;

exports[`Guess Array Element Should work for array on nested Object without type override 1`] = `
AnimalClass {
"tags": Array [
Object {
"id": "123",
"name": "lorem",
},
],
}
`;
37 changes: 36 additions & 1 deletion test/converter-array.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,15 @@ describe("Array Converter", () => {
])
})

it("Should convert array of Object as is", () => {
const result = convert([
{ id: "200", name: "Mimi", deceased: "ON", birthday: "2018-2-2" },
{ id: "200", name: "Mimi", deceased: "ON", birthday: "2018-2-2" },
{ id: "200", name: "Mimi", deceased: "ON", birthday: "2018-2-2" }
], { type: [Object] })
expect(result.value).toMatchSnapshot()
})

it("Should convert nested array inside model", () => {
@reflect.parameterProperties()
class TagModel {
Expand Down Expand Up @@ -107,8 +116,34 @@ describe("Array Converter", () => {
expect(result.issues).toEqual([{ path: "1.tags.1.id", messages: ["Unable to convert \"Hello\" into Number"] }])
})

it("Should able to guess non array for single element as element if defined", () => {
})

describe("Guess Array Element", () => {
it("Should able to guess single value as Array based on type", () => {
const b = convert("1234", { type: [Number], guessArrayElement: true })
expect(b.value).toEqual([1234])
})

it("Should work for array of Object", () => {
const b = convert("1234", { type: [Object], guessArrayElement: true })
expect(b.value).toEqual(["1234"])
})

it("Should work for array on nested Object without type override", () => {
@reflect.parameterProperties()
class TagModel {
constructor(
public id: number,
public name: string,
) { }
}
@reflect.parameterProperties()
class AnimalClass {
constructor(
public tags: TagModel[] // <-- no type override
) { }
}
const b = convert({ tags: { id: "123", name: "lorem" } }, { type: AnimalClass, guessArrayElement: true })
expect(b.value).toMatchSnapshot()
})
})
Loading

0 comments on commit 07932ab

Please sign in to comment.