chore: update guard with array contains null (#1922)
This commit is contained in:
parent
e299f5452c
commit
7080db72eb
@ -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', () => {
|
test('association black list', () => {
|
||||||
const values = {
|
const values = {
|
||||||
name: 'username123',
|
name: 'username123',
|
||||||
|
@ -10,6 +10,7 @@ type UpdateValues = {
|
|||||||
};
|
};
|
||||||
|
|
||||||
type UpdateAction = 'create' | 'update';
|
type UpdateAction = 'create' | 'update';
|
||||||
|
|
||||||
export class UpdateGuard {
|
export class UpdateGuard {
|
||||||
model: ModelStatic<any>;
|
model: ModelStatic<any>;
|
||||||
action: UpdateAction;
|
action: UpdateAction;
|
||||||
@ -18,6 +19,21 @@ export class UpdateGuard {
|
|||||||
private blackList: BlackList;
|
private blackList: BlackList;
|
||||||
private whiteList: WhiteList;
|
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) {
|
setAction(action: UpdateAction) {
|
||||||
this.action = action;
|
this.action = action;
|
||||||
}
|
}
|
||||||
@ -78,6 +94,10 @@ export class UpdateGuard {
|
|||||||
let associationValues = associationsValues[association];
|
let associationValues = associationsValues[association];
|
||||||
|
|
||||||
const filterAssociationToBeUpdate = (value) => {
|
const filterAssociationToBeUpdate = (value) => {
|
||||||
|
if (value === null) {
|
||||||
|
return value;
|
||||||
|
}
|
||||||
|
|
||||||
const associationKeysToBeUpdate = this.associationKeysToBeUpdate || [];
|
const associationKeysToBeUpdate = this.associationKeysToBeUpdate || [];
|
||||||
|
|
||||||
if (associationKeysToBeUpdate.includes(association)) {
|
if (associationKeysToBeUpdate.includes(association)) {
|
||||||
@ -155,19 +175,4 @@ export class UpdateGuard {
|
|||||||
|
|
||||||
return result;
|
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;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user