data icon indicating copy to clipboard operation
data copied to clipboard

Creating record with deeper nested relation fails schema validation

Open bmg-sander opened this issue 1 month ago • 3 comments

The following code fails to create a record:

  const contactSchema = z.object({ email: z.string() })
  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: '[email protected]' })
  const user = await users.create({ id: 1, contact })
  const post = await posts.create({ title: 'First', author: user })

I receive the following validation error from this code:

[
  {
    expected: 'object',
    code: 'invalid_type',
    path: [ 'author', 'contact' ],
    message: 'Invalid input: expected object, received undefined'
  }
]

Is this expected or am I doing something wrong?

bmg-sander avatar Nov 21 '25 09:11 bmg-sander