Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/collection.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@
if (validationResult.issues) {
console.error(validationResult.issues)

throw new OperationError(

Check failure on line 122 in src/collection.ts

View workflow job for this annotation

GitHub Actions / test

tests/regressions/348-nested-relations.test.ts > creates a record with a nested relation through another model

Error: Failed to create a new record with initial values: does not match the schema. Please see the schema validation errors above. ❯ Collection.create src/collection.ts:122:13 ❯ tests/regressions/348-nested-relations.test.ts:36:16 ⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯⎯ Serialized Error: { code: 'INVALID_INITIAL_VALUES' }
'Failed to create a new record with initial values: does not match the schema. Please see the schema validation errors above.',
OperationErrorCodes.INVALID_INITIAL_VALUES,
)
Expand Down Expand Up @@ -586,6 +586,9 @@
}

const sanitizedInitialValues = sanitize(initialValues)

console.log({ sanitizedInitialValues })

return {
sanitizedInitialValues,
/**
Expand Down
44 changes: 44 additions & 0 deletions tests/regressions/348-nested-relations.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
import { Collection } from '#/src/collection.js'
import z from 'zod'

it('creates a record with a nested relation through another model', async () => {
const contactSchema = z.object({ email: z.email() })
const userSchema = z.object({ id: z.number(), contact: contactSchema })
const postSchema = z.object({
title: z.string(),
author: userSchema,
})

const contacts = new Collection({ schema: contactSchema })
const users = new Collection({ schema: userSchema })
const posts = new Collection({ schema: postSchema })

users.defineRelations(({ one }) => ({
contact: one(contacts),
}))
posts.defineRelations(({ one }) => ({
author: one(users),
}))

const contact = await contacts.create({ email: 'john@example.com' })
expect(contact).toEqual({ email: 'john@example.com' })

const user = await users.create({ id: 1, contact })
expect(user).toEqual({ id: 1, contact: { email: 'john@example.com' } })

/**
* Creating a post introduces this relation chain:
* post.author -> user.contact -> contact
*
* Due to our sanitization logic, `author.contact` will be reset to
* the default value based on its relation (`undefined` here).
*/
const post = await posts.create({ title: 'First', author: user })
expect(post).toEqual({
title: 'First',
author: {
id: 1,
contact: { email: 'john@example.com' },
},
})
})
Loading