Skip to content

Commit aaa8d43

Browse files
fix: formally validate up results during migrations (#7)
Co-authored-by: Andrew Bastin <andrewbastin.k@gmail.com>
1 parent dfa3b05 commit aaa8d43

File tree

2 files changed

+27
-11
lines changed

2 files changed

+27
-11
lines changed

src/__tests__/index.spec.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -992,14 +992,14 @@ describe("createVersionedEntity", () => {
992992
if (result.type === "ok") {
993993
// The root's migration function saw v1 children
994994
expect(rootChildVersions).toEqual([1, 1])
995-
996-
// Final result: parent is migrated but children are not (up didn't migrate them)
995+
996+
// Final result: parent is migrated, and children are also migrated by the schema's entityRefUptoVersion transform
997997
expect(result.value.v).toBe(2)
998998
expect(result.value.name).toBe("root_v2")
999-
expect(result.value.children[0].v).toBe(1)
1000-
expect(result.value.children[0].name).toBe("child1")
1001-
expect(result.value.children[1].v).toBe(1)
1002-
expect(result.value.children[1].name).toBe("child2")
999+
expect(result.value.children[0].v).toBe(2)
1000+
expect(result.value.children[0].name).toBe("child1_v2")
1001+
expect(result.value.children[1].v).toBe(2)
1002+
expect(result.value.children[1].name).toBe("child2_v2")
10031003
}
10041004
})
10051005
})
@@ -1088,12 +1088,12 @@ describe("createVersionedEntity", () => {
10881088
if (result.type === "ok") {
10891089
// Parent still sees v1 child during migration (even without z.lazy)
10901090
expect(childMigrationVersion).toBe(1)
1091-
1092-
// Final result: parent is migrated but child is not (up didn't migrate it)
1091+
1092+
// Final result: parent is migrated, and child is also migrated by the schema's entityRefUptoVersion transform
10931093
expect(result.value.v).toBe(2)
10941094
expect(result.value.name).toBe("parent_parent_v2")
1095-
expect(result.value.child.v).toBe(1)
1096-
expect(result.value.child.name).toBe("child")
1095+
expect(result.value.child.v).toBe(2)
1096+
expect(result.value.child.name).toBe("child_child_v2")
10971097
}
10981098
})
10991099
})

src/index.ts

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -266,7 +266,23 @@ export class VersionedEntity<
266266
}
267267
}
268268

269-
finalData = upDef.up(finalData)
269+
const nextData = upDef.up(finalData)
270+
271+
const nextDataParseResult = upDef.schema.safeParse(nextData)
272+
273+
if (!nextDataParseResult.success) {
274+
return {
275+
type: "err",
276+
error: {
277+
type: "GIVEN_VER_VALIDATION_FAIL",
278+
version: up,
279+
versionDef: upDef,
280+
error: nextDataParseResult.error
281+
}
282+
}
283+
}
284+
285+
finalData = nextDataParseResult.data
270286
}
271287

272288
return { type: "ok", value: finalData }

0 commit comments

Comments
 (0)