Skip to content

Commit

Permalink
Fix mutability issue
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartmr committed Dec 6, 2021
1 parent c8779d2 commit 97bd260
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "not-me",
"version": "4.3.3",
"version": "4.3.4",
"description": "Easy and type-safe validation",
"main": "lib/index.js",
"types": "lib/types.d.ts",
Expand Down
19 changes: 18 additions & 1 deletion src/schemas/base/base-schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,24 @@ export abstract class BaseSchema<

if (this.mapMode) {
shapedValue = {} as BaseType;
let shapedValueWithUnknownProperties = _currentValue as BaseType;

/*
Used with ObjectSchema.union(),
in order make the object more precise as it goes through many filters,
including transform() calls on each of its properties
*/
let shapedValueWithUnknownProperties = {
/*
Avoid changing the original object instance (which might even break the expected types from it, when running transformations),
by cloning it
Also fixes errors like 'TypeError: Cannot set property x of #<y> which has only a getter'
We can assume it's an object since only the ObjectSchema sets this.mapMode.
We also know it's an object because we already went through the type filter
*/
...(_currentValue as { [key: string]: unknown }),
} as unknown as BaseType;

for (let i = 0; i < this.shapeFilters.length; i++) {
const shapeFilter = this.shapeFilters[i] || throwError();
Expand Down

0 comments on commit 97bd260

Please sign in to comment.