Skip to content

Commit

Permalink
Merge pull request #33 from HelloPret/fix-relationship-with-underscor…
Browse files Browse the repository at this point in the history
…e-issue

Fix : on error the name of the relationship is not transfomed into a camel case
  • Loading branch information
richmolj authored Jun 4, 2019
2 parents 3ddc7b5 + 60df3a9 commit d271168
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 79 deletions.
2 changes: 1 addition & 1 deletion src/util/validation-error-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export class ValidationErrorBuilder<T extends SpraypaintBase> {
meta: JsonapiErrorMeta,
err: JsonapiError
) {
let relatedObject = model[meta.name]
let relatedObject = model[model.klass.deserializeKey(meta.name)]
if (Array.isArray(relatedObject)) {
relatedObject = relatedObject.find(r => {
return r.id === meta.id || r.temp_id === meta["temp-id"]
Expand Down
8 changes: 8 additions & 0 deletions test/fixtures.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ export class Person extends ApplicationRecord {
@Attr firstName!: string | null
@Attr lastName!: string | null
@Attr({ type: Number }) age!: number | null
@BelongsTo({ type: "person_details" }) personDetail!: PersonDetail
}

@Model()
Expand All @@ -39,6 +40,13 @@ export class PersonWithLinks extends Person {
@Link() webView!: string
}

@Model()
export class PersonDetail extends ApplicationRecord {
static jsonapiType = "person_details"

@Attr address!: string
}

@Model({ keyCase: { server: "snake", client: "snake" } })
export class PersonWithoutCamelizedKeys extends Person {
@Attr first_name!: string
Expand Down
217 changes: 139 additions & 78 deletions test/integration/validations.test.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,10 @@
import { expect, sinon, fetchMock } from "../test-helper"
import { Author, Book, Genre } from "../fixtures"
import { Author, Book, Genre, Person, PersonDetail } from "../fixtures"
import { tempId } from "../../src/util/temp-id"
import { SpraypaintBase, ModelRecord } from "../../src/model"
import { ValidationError } from "../../src/validation-errors"

const mockErrors = {
firstName: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "First Name cannot be blank",
meta: { attribute: "first_name", message: "cannot be blank" }
},
lastName: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "Last Name cannot be blank",
meta: { attribute: "last-name", message: "cannot be blank" }
},
bookTitle: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "Title cannot be blank",
meta: {
relationship: {
name: "books",
type: "books",
["temp-id"]: "abc1",
attribute: "title",
message: "cannot be blank"
}
}
},
bookGenreName: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "Name cannot be blank",
meta: {
relationship: {
name: "books",
type: "books",
["temp-id"]: "abc1",
relationship: {
name: "genre",
type: "genres",
id: "1",
attribute: "name",
message: "cannot be blank"
}
}
}
},
bookGenreBase: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "base some error",
meta: {
relationship: {
name: "books",
type: "books",
["temp-id"]: "abc1",
relationship: {
name: "genre",
type: "genres",
id: "1",
attribute: "base",
message: "some error"
}
}
}
}
} as any

const resetMocks = () => {
const resetMocks = (mockErrors: any) => {
fetchMock.restore()

let errors = []
Expand All @@ -96,19 +24,91 @@ const resetMocks = () => {
})
}

let instance: Author
let tempIdIndex = 0
describe("validations", () => {
describe("validations (1/2)", () => {
const mockErrors = {
firstName: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "First Name cannot be blank",
meta: { attribute: "first_name", message: "cannot be blank" }
},
lastName: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "Last Name cannot be blank",
meta: { attribute: "last-name", message: "cannot be blank" }
},
bookTitle: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "Title cannot be blank",
meta: {
relationship: {
name: "books",
type: "books",
["temp-id"]: "abc1",
attribute: "title",
message: "cannot be blank"
}
}
},
bookGenreName: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "Name cannot be blank",
meta: {
relationship: {
name: "books",
type: "books",
["temp-id"]: "abc1",
relationship: {
name: "genre",
type: "genres",
id: "1",
attribute: "name",
message: "cannot be blank"
}
}
}
},
bookGenreBase: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "base some error",
meta: {
relationship: {
name: "books",
type: "books",
["temp-id"]: "abc1",
relationship: {
name: "genre",
type: "genres",
id: "1",
attribute: "base",
message: "some error"
}
}
}
}
} as any

let instance: Author

beforeEach(() => {
resetMocks()
resetMocks(mockErrors)
})

beforeEach(() => {
sinon.stub(tempId, "generate").callsFake(() => {
tempIdIndex++
return `abc${tempIdIndex}`
})

instance = new Author({ lastName: "King" })
const genre = new Genre({ id: "1" })
genre.isPersisted = true
Expand Down Expand Up @@ -279,3 +279,64 @@ describe("validations", () => {
})
})
})

describe("validations (2/2)", () => {
const mockErrors = {
personDetailBase: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "base some error",
meta: {
relationship: {
name: "person_detail",
type: "person_details",
["temp-id"]: "abc1",
id: "1",
attribute: "base",
message: "some error"
}
}
}
} as any

let instance: Person

beforeEach(() => {
resetMocks(mockErrors)
})

beforeEach(() => {
sinon.stub(tempId, "generate").callsFake(() => {
tempIdIndex++
return `abc${tempIdIndex}`
})

const personDetail = new PersonDetail({ id: "1" })
personDetail.isPersisted = true
instance = new Person({
personDetail
})
})

afterEach(() => {
tempIdIndex = 0
;(<any>tempId.generate).restore()
})

it("applies errors to nested belongsTo relationships with underscores", async () => {
const isSuccess = await instance.save({ with: ["person_details"] })
expect(instance.isPersisted).to.eq(false)
expect(isSuccess).to.eq(false)
expect(instance.personDetail.errors).to.deep.equal({
base: {
title: "Validation Error",
attribute: "base",
code: "unprocessable_entity",
fullMessage: "some error",
message: "some error",
rawPayload: mockErrors.personDetailBase
}
})
})
})

0 comments on commit d271168

Please sign in to comment.