mirror of
https://github.com/node-red/node-red.git
synced 2025-03-01 10:36:34 +00:00
Updated the logic to be easier to follow
This commit is contained in:
parent
10771a2019
commit
9a435217ec
@ -65,21 +65,6 @@ module.exports = class PayloadValidator {
|
|||||||
return _.set(object, location, value);
|
return _.set(object, location, value);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
|
||||||
* lodash set will always pass but might not actually set the value
|
|
||||||
* So here we test that it was actually set and use that to confirm we can set
|
|
||||||
*
|
|
||||||
* @param {*} object
|
|
||||||
* @param {*} location
|
|
||||||
* @returns
|
|
||||||
*/
|
|
||||||
canSet(object, location) {
|
|
||||||
const test = 'test';
|
|
||||||
let clonedObject = clone(object);
|
|
||||||
clonedObject = this.setValue(clonedObject, location, test);
|
|
||||||
return test === this.getValue(clonedObject, location);
|
|
||||||
}
|
|
||||||
|
|
||||||
check(_after) {
|
check(_after) {
|
||||||
let after = _after;
|
let after = _after;
|
||||||
try {
|
try {
|
||||||
@ -101,10 +86,12 @@ module.exports = class PayloadValidator {
|
|||||||
conversationId: this.conversationId
|
conversationId: this.conversationId
|
||||||
});
|
});
|
||||||
|
|
||||||
if (!this.canSet(after, location)) {
|
|
||||||
|
after = this.setValue(after, location, beforeValue);
|
||||||
|
|
||||||
|
if (!_.has(after, location, beforeValue)) {
|
||||||
throw new Error(`Cant set value as ${location} is no longer present in after`);
|
throw new Error(`Cant set value as ${location} is no longer present in after`);
|
||||||
}
|
}
|
||||||
after = this.setValue(after, location, beforeValue);
|
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (e) {
|
} catch (e) {
|
||||||
|
@ -280,26 +280,4 @@ describe('Unit: PayloadValidator', () => {
|
|||||||
assert.strictEqual(result.should.set.hello, value);
|
assert.strictEqual(result.should.set.hello, value);
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|
||||||
describe('Can set', () => {
|
|
||||||
let payloadValidator;
|
|
||||||
before(() => {
|
|
||||||
const event = orgEvent('event');
|
|
||||||
payloadValidator = new PayloadValidator(event, 'before-worker-id-nodeId');
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Should get back false for canSet when object is a string', () => {
|
|
||||||
const object = 'some string';
|
|
||||||
const location = 'hello';
|
|
||||||
const result = payloadValidator.canSet(object, location);
|
|
||||||
assert.strictEqual(result, false);
|
|
||||||
});
|
|
||||||
|
|
||||||
it('Should get back true for canSet', () => {
|
|
||||||
const object = {};
|
|
||||||
const location = 'should.set.hello';
|
|
||||||
const result = payloadValidator.canSet(object, location);
|
|
||||||
assert.strictEqual(result, true);
|
|
||||||
});
|
|
||||||
});
|
|
||||||
});
|
});
|
||||||
|
Loading…
x
Reference in New Issue
Block a user