Skip to content
New issue

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

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

Already on GitHub? Sign in to your account

fix(runtime): throw PrismaValidationError when auth() is used in @default without user context #1618

Merged
merged 1 commit into from
Jul 28, 2024
Merged
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
19 changes: 15 additions & 4 deletions packages/runtime/src/enhancements/default-auth.ts
Original file line number Diff line number Diff line change
@@ -1,12 +1,19 @@
/* eslint-disable @typescript-eslint/no-unused-vars */
/* eslint-disable @typescript-eslint/no-explicit-any */

import { FieldInfo, NestedWriteVisitor, PrismaWriteActionType, enumerate, getFields, requireField } from '../cross';
import { clone } from '../cross';
import {
FieldInfo,
NestedWriteVisitor,
PrismaWriteActionType,
clone,
enumerate,
getFields,
requireField,
} from '../cross';
import { DbClientContract } from '../types';
import { EnhancementContext, InternalEnhancementOptions } from './create-enhancement';
import { DefaultPrismaProxyHandler, PrismaProxyActions, makeProxy } from './proxy';
import { isUnsafeMutate } from './utils';
import { isUnsafeMutate, prismaClientValidationError } from './utils';

/**
* Gets an enhanced Prisma client that supports `@default(auth())` attribute.
Expand Down Expand Up @@ -143,7 +150,11 @@ class DefaultAuthHandler extends DefaultPrismaProxyHandler {

private getDefaultValueFromAuth(fieldInfo: FieldInfo) {
if (!this.userContext) {
throw new Error(`Evaluating default value of field \`${fieldInfo.name}\` requires a user context`);
throw prismaClientValidationError(
this.prisma,
this.options.prismaModule,
`Evaluating default value of field \`${fieldInfo.name}\` requires a user context`
);
}
return fieldInfo.defaultValueProvider?.(this.userContext);
}
Expand Down
34 changes: 34 additions & 0 deletions tests/regression/tests/issue-1596.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { isPrismaClientValidationError } from '@zenstackhq/runtime';
import { loadSchema } from '@zenstackhq/testtools';

describe('issue 1596', () => {
it('regression', async () => {
const { enhance } = await loadSchema(
`
model User {
id Int @id
posts Post[]
}

model Post {
id Int @id
title String
author User @relation(fields: [authorId], references: [id])
authorId Int @default(auth().id)
}
`
);

const db = enhance();

try {
await db.post.create({ data: { title: 'Post1' } });
} catch (e) {
// eslint-disable-next-line jest/no-conditional-expect
expect(isPrismaClientValidationError(e)).toBe(true);
return;
}

throw new Error('Expected error');
});
});
Loading