Skip to content

Commit 960473f

Browse files
wadetandyrichmolj
authored andcommitted
Make JSORMBase#clearErrors() to through the setter (#77)
We were previously assigning `this._errors` directly, which caused a problem when reusing reactive frameworks that were observing model changes by wrapping the existing setters with observers. Also moved the clearErrors call into the model's `save()` method out of the payload writer's asJSON method, which was a very confusing place to have it live.
1 parent 468eea5 commit 960473f

File tree

3 files changed

+16
-3
lines changed

3 files changed

+16
-3
lines changed

src/model.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -588,7 +588,7 @@ export class JSORMBase {
588588
}
589589

590590
clearErrors() {
591-
this._errors = {}
591+
this.errors = {}
592592
}
593593

594594
isDirty(relationships?: IncludeScope): boolean {
@@ -820,6 +820,8 @@ export class JSORMBase {
820820
verb = "patch"
821821
}
822822

823+
this.clearErrors()
824+
823825
const json = payload.asJSON()
824826

825827
try {

src/util/write-payload.ts

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -133,8 +133,6 @@ export class WritePayload<T extends JSORMBase> {
133133
type: this.jsonapiType
134134
}
135135

136-
this.model.clearErrors()
137-
138136
if (this.model.id) {
139137
data.id = this.model.id
140138
}

test/integration/validations.test.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -154,6 +154,19 @@ describe("validations", () => {
154154
expect(instance.errors).to.deep.eq({})
155155
})
156156

157+
it("clears errors via the public setter", async () => {
158+
fetchMock.restore()
159+
fetchMock.mock({
160+
matcher: "*",
161+
response: { data: { id: "1", type: "employees" } }
162+
})
163+
let spy = (<any>sinon.spy)(instance, "errors", ["set"])
164+
165+
await instance.save()
166+
167+
expect(spy.set).to.have.been.calledOnce
168+
})
169+
157170
it("instantiates a new error object instance after save", async () => {
158171
const originalErrors = (instance.errors = { foo: "bar" })
159172
const result = instance.save({ with: { books: "genre" } })

0 commit comments

Comments
 (0)