Skip to content
New issue

Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? # to your account

[Bug?] @password Attribute Function Calling Undefined Zod Function During Update, Upsert #2000

Open
breagan1983 opened this issue Feb 22, 2025 · 1 comment

Comments

@breagan1983
Copy link

Error

Uncaught:
Error calling enhanced Prisma method `user.upsert`: result.merge is not a function

policy-utils.ts:1387

Description and expected behavior

  • ✅ Works Fine
    • db.user.create({data: {username: 'admin', password: 'abc12345'}})
  • ❌ Causes Error
    • db.user.update({where: {username: 'admin'}, data: {password: 'abc123456789123'}})
    • db.user.upsert({where: {username: 'admin'}, update: {username: 'admin2'}, create: {username: 'admin', password: 'abc123456789'}})

Additional Context*

  • I think it relates to issues talked about in @zenstackhq/zenstack#676 and @colinhacks/zod:2646
  • Some of the schemas in this.zodSchemas.models are of type ZodObject, but it seems like any models that have model level validation on it along with polymorphic extensions of it are of type ZodEffects . (in img below, the User model is derived from the Entity model)
    Image

Testing

  • Problem is resolved if I remove any @@validate functions

ZModel
(sidenote: please don't be alarmed by the overly permissive @@allow() s, still in early development)

abstract model Base {
    id              String          @id  @default(uuid())    @deny('update', true)
    createdAt       DateTime        @default(now())          @deny('update', true)
    updatedAt       DateTime        @updatedAt               @deny('update', true)
    active          Boolean         @default(false)
    published       Boolean         @default(true)           
    deleted         Boolean         @default(false)
    startDate       DateTime?
    endDate         DateTime?

    @@allow('create', true)
    @@allow('read', true)
    @@allow('update', true)
}

enum EntityType {
    User
    Alias
    Group
    Service
    Device
    Organization
    Guest
}

model Entity extends Base {
    entityType      EntityType
    name            String?                 @unique
    members         Entity[]                @relation("members")
    memberOf        Entity[]                @relation("members")
    @@delegate(entityType)


    @@allow('create', true)
    @@allow('read', true)
    @@allow('update', true)
    @@validate(!active || (active && name != null), "Active Entities Must Have A Name")
}

model User extends Entity {
    profile         Json?               
    username        String                  @unique 
    password        String                  @password

    @@allow('create', true)
    @@allow('read', true)
    @@allow('update', true)
}

Environment (please complete the following information):

  • ZenStack version: 2.11.6
  • Prisma version: 6.3.1
  • Database type: SQLite
  • Zod Version : 3.24.2
@ymc9
Copy link
Member

ymc9 commented Feb 23, 2025

Thanks for filing this @breagan1983 . The @password and @@validation combination is untested and doesn't work 😢 . You're right, the problem is after zod refinement the schema is not an ZodObject anymore and cannot be further merged.

I'll make a fix to use the pre-refine schema for merging and then reapply the refinement. A bit awkward but it should work.

# for free to join this conversation on GitHub. Already have an account? # to comment
Projects
None yet
Development

No branches or pull requests

2 participants