Skip to content

Commit

Permalink
Merge pull request #58 from Draggha/57/fix-non-first-persisted-relati…
Browse files Browse the repository at this point in the history
…onship-validations

fix non first persisted relationship validations (fixes #57)
  • Loading branch information
richmolj authored Feb 24, 2020
2 parents 70b630c + 9c9506e commit 84027ad
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 2 deletions.
2 changes: 2 additions & 0 deletions src/util/validation-error-builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,8 @@ export class ValidationErrorBuilder<T extends SpraypaintBase> {
let relatedObject = model[model.klass.deserializeKey(meta.name)]
if (Array.isArray(relatedObject)) {
relatedObject = relatedObject.find(r => {
if (meta["temp-id"] === undefined) return r.id === meta.id

// For now graphiti is returning the related object id as an integer
// where the related object's ID is a string
return (
Expand Down
72 changes: 70 additions & 2 deletions test/integration/validations.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ const resetMocks = (mockErrors: any, status: number = 422) => {
}

let tempIdIndex = 0
describe("validations (1/2)", () => {
describe("validations (1/3)", () => {
const mockErrors = {
firstName: {
code: "unprocessable_entity",
Expand Down Expand Up @@ -330,7 +330,7 @@ describe("validations (1/2)", () => {
})
})

describe("validations (2/2)", () => {
describe("validations (2/3)", () => {
const mockErrors = {
personDetailBase: {
code: "unprocessable_entity",
Expand Down Expand Up @@ -392,6 +392,74 @@ describe("validations (2/2)", () => {
})
})

describe("validations (3/3)", () => {
const mockErrors = {
bookTitle: {
code: "unprocessable_entity",
status: "422",
title: "Validation Error",
detail: "Title cannot be blank",
meta: {
relationship: {
id: "30",
// ["temp-id"]: "abc1",
name: "books",
type: "books",
attribute: "title",
message: "cannot be blank"
}
}
}
} as any

let instance: Author

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

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

const genre = new Genre({ id: "20", name: "Horror" })
genre.isPersisted = true
const book1 = new Book({ id: "10", title: "The Shining", genre })
book1.isPersisted = true
const book2 = new Book({ id: "30", title: "Pet Sematary", genre })
book2.isPersisted = true
instance = new Author({ firstName: "Stephen", lastName: "King" })
instance.id = "1"
instance.books = [book1, book2]
instance.specialBooks = []
instance.isPersisted = true
})

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

it("applies errors to objects other than the first in nested hasMany relationships", async () => {
instance.books[1].title = ""
const isSuccess = await instance.save({ with: { books: "genre" } })

expect(isSuccess).to.eq(false)
expect(instance.books[1].errors).to.deep.equal({
title: {
title: "Validation Error",
attribute: "title",
code: "unprocessable_entity",
fullMessage: "Title cannot be blank",
message: "cannot be blank",
rawPayload: mockErrors.bookTitle
}
})
})
})

describe("Errors without graphiti validation (1/2)", () => {
const mockErrors = {
noMeta: {
Expand Down

0 comments on commit 84027ad

Please sign in to comment.