diff --git a/src/index.ts b/src/index.ts index ed76aed..9948125 100644 --- a/src/index.ts +++ b/src/index.ts @@ -791,12 +791,11 @@ export class BufferedChangeset implements IChangeset { */ _valueFor(key: string): any { let changes: Changes = this[CHANGES]; - let errors: Errors = this[ERRORS]; let content: Content = this[CONTENT]; - if (errors.hasOwnProperty(key)) { - let e: Err = errors[key]; - return e.value; + // Derived property, i.e. property defined in the changeset itself + if (Object.prototype.hasOwnProperty.apply(this, [key])) { + return this[key]; } // 'person' diff --git a/test/index.test.ts b/test/index.test.ts index 7f1ad9b..2fb775c 100644 --- a/test/index.test.ts +++ b/test/index.test.ts @@ -1942,6 +1942,60 @@ describe('Unit | Utility | changeset', () => { ]); }); + it('#validate changeset getter', async () => { + class MyModel { + isOptionOne = false; + isOptionTwo = false; + isOptionThree = true; + } + + const Validations = { + isOptionSelected: (newValue: boolean) => { + return newValue === true ? true : 'No options selected'; + } + }; + + function myValidator({ + key, + newValue, + oldValue, + changes, + content + }: { + key: string; + newValue: unknown; + oldValue: unknown; + changes: any; + content: any; + }) { + let validatorFn = get(Validations, key); + + if (typeof validatorFn === 'function') { + return validatorFn(newValue, oldValue, changes, content); + } + } + + const myObject = new MyModel(); + const myChangeset = Changeset(myObject, myValidator, Validations); + + Object.defineProperty(myChangeset, 'isOptionSelected', { + get() { + return this.get('isOptionOne') || this.get('isOptionTwo') || this.get('isOptionThree'); + } + }); + + await myChangeset.validate(); + expect(myChangeset.isInvalid).toEqual(false); + + myChangeset.set('isOptionThree', false); + await myChangeset.validate(); + expect(myChangeset.isInvalid).toEqual(true); + + myChangeset.set('isOptionTwo', true); + await myChangeset.validate(); + expect(myChangeset.isInvalid).toEqual(false); + }); + it('#isInvalid does not trigger validations without validate keys', async () => { const model = { name: 'o' }; const dummyChangeset = Changeset(model, lookupValidator(dummyValidations));