Skip to content

Commit c199967

Browse files
committed
fix tests
1 parent 6d2a08b commit c199967

File tree

2 files changed

+61
-11
lines changed

2 files changed

+61
-11
lines changed

packages/schema/src/language-server/validator/attribute-application-validator.ts

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -270,16 +270,6 @@ function assignableToAttributeParam(arg: AttributeArg, param: AttributeParam, at
270270
let dstType = param.type.type;
271271
let dstIsArray = param.type.array;
272272

273-
if (isAliasDecl(arg.$resolvedType?.decl)) {
274-
if (dstType === 'ContextType') {
275-
// TODO: what is context type? Passed to true to avoid error, to be fixed later
276-
return true;
277-
}
278-
const aliasExpression = arg.$resolvedType.decl.expression;
279-
const mappedType = mappedRawExpressionTypeToResolvedShape(aliasExpression.$type);
280-
return dstType === mappedType;
281-
}
282-
283273
if (dstType === 'ContextType') {
284274
// ContextType is inferred from the attribute's container's type
285275
if (isDataModelField(attr.$container)) {
@@ -310,6 +300,16 @@ function assignableToAttributeParam(arg: AttributeArg, param: AttributeParam, at
310300
}
311301
}
312302

303+
// alias expression is compared to corresponding expression resolved shape
304+
if (isAliasDecl(arg.$resolvedType?.decl)) {
305+
// TODO: what is context type? Passed to true to avoid error, to be fixed later
306+
if (dstType === 'ContextType') return true;
307+
308+
const alias = arg.$resolvedType.decl;
309+
const mappedAliasResolvedType = mappedRawExpressionTypeToResolvedShape(alias.expression.$type);
310+
return dstType === mappedAliasResolvedType || dstType === 'Any' || mappedAliasResolvedType === 'Any';
311+
}
312+
313313
// destination is field reference or transitive field reference, check if
314314
// argument is reference or array or reference
315315
if (dstType === 'FieldReference' || dstType === 'TransitiveFieldReference') {

tests/integration/tests/plugins/policy.test.ts

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ model M {
111111
).toEqual(expect.objectContaining({ AND: [{ AND: [] }, { value: { gt: 10 } }] }));
112112
});
113113

114-
it('alias expressions', async () => {
114+
it('simple alias expressions', async () => {
115115
const { policy } = await loadSchema(
116116
`
117117
alias allowAll() {
@@ -171,4 +171,54 @@ model M {
171171
)
172172
).toEqual({ AND: [{ authorId: { equals: 'u2' } }, { published: true }] });
173173
});
174+
175+
it('complex alias expressions', async () => {
176+
const model = `
177+
alias currentUserId() {
178+
auth().id
179+
}
180+
181+
alias complexAlias() {
182+
auth().cart.tasks?[id == 123] && value >10 && currentUserId() != null
183+
}
184+
185+
model User {
186+
id Int @id @default(autoincrement())
187+
cart Cart?
188+
}
189+
190+
model Cart {
191+
id Int @id @default(autoincrement())
192+
tasks Task[]
193+
user User @relation(fields: [userId], references: [id])
194+
userId Int @unique
195+
}
196+
197+
model Task {
198+
id Int @id @default(autoincrement())
199+
cart Cart @relation(fields: [cartId], references: [id])
200+
cartId Int
201+
value Int
202+
@@allow('read', complexAlias())
203+
}
204+
`;
205+
206+
const { policy } = await loadSchema(model, {
207+
compile: false,
208+
generateNoCompile: true,
209+
output: 'out/',
210+
});
211+
212+
expect(
213+
(policy.policy.task.modelLevel.read.guard as Function)({ user: { cart: { tasks: [{ id: 1 }] } } })
214+
).toEqual(
215+
expect.objectContaining({
216+
AND: [{ AND: [{ OR: [] }, { value: { gt: 10 } }] }, { OR: [] }],
217+
})
218+
);
219+
220+
expect(
221+
(policy.policy.task.modelLevel.read.guard as Function)({ user: { cart: { tasks: [{ id: 123 }] } } })
222+
).toEqual(expect.objectContaining({ AND: [{ AND: [{ AND: [] }, { value: { gt: 10 } }] }, { OR: [] }] }));
223+
});
174224
});

0 commit comments

Comments
 (0)