Skip to content

Commit 4d80638

Browse files
authored
Fixing preventChanges (#680)
1 parent a265142 commit 4d80638

File tree

2 files changed

+13
-3
lines changed

2 files changed

+13
-3
lines changed

src/hooks/prevent-changes.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -22,18 +22,18 @@ export function preventChanges (
2222

2323
return (context: any) => {
2424
checkContext(context, 'before', ['patch'], 'preventChanges');
25-
const { data } = context;
25+
let data = { ...context.data };
2626

2727
fieldNames.forEach(name => {
2828
if (_has(data, name)) {
2929
if (ifThrow) {
3030
throw new BadRequest(`Field ${name} may not be patched. (preventChanges)`);
3131
}
3232
// Delete data.contactPerson.name
33-
context.data = _omit(data, name);
33+
data = _omit(data, name);
3434
}
3535
});
3636

37-
return context;
37+
return { ...context, data };
3838
};
3939
}

test/hooks/prevent-changes.test.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,16 @@ describe('services preventChanges', () => {
130130
assert.deepEqual(context.data,
131131
{ first: 'John', last: 'Doe', a: { b: 'john', c: { d: {} } } },
132132
'5');
133+
134+
context = preventChanges(false, 'first', 'last')(clone(hookBefore));
135+
assert.deepEqual(context.data,
136+
{ a: { b: 'john', c: { d: { e: 1 } } } }
137+
);
138+
139+
context = preventChanges(false, 'first', 'a.b', 'a.c.d.e')(clone(hookBefore));
140+
assert.deepEqual(context.data,
141+
{ last: 'Doe', a: { c: { d: { } } } }
142+
);
133143
});
134144
});
135145
});

0 commit comments

Comments
 (0)