chore: update guard with array contains null (#1922)

This commit is contained in:
ChengLei Shao 2023-05-24 11:24:15 +08:00 committed by GitHub
parent e299f5452c
commit 7080db72eb
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 15 deletions

View File

@ -108,6 +108,19 @@ describe('update-guard', () => {
});
});
test('association with null array', () => {
const values = {
name: 'u1',
posts: [null],
};
const guard = new UpdateGuard();
guard.setModel(User.model);
const sanitized = guard.sanitize(values);
expect(sanitized).toEqual({ name: 'u1', posts: [null] });
});
test('association black list', () => {
const values = {
name: 'username123',

View File

@ -10,6 +10,7 @@ type UpdateValues = {
};
type UpdateAction = 'create' | 'update';
export class UpdateGuard {
model: ModelStatic<any>;
action: UpdateAction;
@ -18,6 +19,21 @@ export class UpdateGuard {
private blackList: BlackList;
private whiteList: WhiteList;
static fromOptions(model, options) {
const guard = new UpdateGuard();
guard.setModel(model);
guard.setWhiteList(options.whitelist);
guard.setBlackList(options.blacklist);
guard.setAction(lodash.get(options, 'action', 'update'));
guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
if (options.underscored) {
guard.underscored = options.underscored;
}
return guard;
}
setAction(action: UpdateAction) {
this.action = action;
}
@ -78,6 +94,10 @@ export class UpdateGuard {
let associationValues = associationsValues[association];
const filterAssociationToBeUpdate = (value) => {
if (value === null) {
return value;
}
const associationKeysToBeUpdate = this.associationKeysToBeUpdate || [];
if (associationKeysToBeUpdate.includes(association)) {
@ -155,19 +175,4 @@ export class UpdateGuard {
return result;
}
static fromOptions(model, options) {
const guard = new UpdateGuard();
guard.setModel(model);
guard.setWhiteList(options.whitelist);
guard.setBlackList(options.blacklist);
guard.setAction(lodash.get(options, 'action', 'update'));
guard.setAssociationKeysToBeUpdate(options.updateAssociationValues);
if (options.underscored) {
guard.underscored = options.underscored;
}
return guard;
}
}