Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 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
5 changes: 5 additions & 0 deletions packages/language/src/ast.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ declare module './generated/ast' {
* Indicates whether the model is already merged with the base types
*/
$baseMerged?: boolean;

/**
* All fields including those marked with `@ignore`
*/
$allFields?: DataModelField[];
}
}

Expand Down
15 changes: 14 additions & 1 deletion packages/schema/src/cli/cli-util.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { isDataSource, isPlugin, Model } from '@zenstackhq/language/ast';
import { isDataModel, isDataSource, isPlugin, Model } from '@zenstackhq/language/ast';
import { getDataModelAndTypeDefs, getLiteral, hasAttribute } from '@zenstackhq/sdk';
import colors from 'colors';
import fs from 'fs';
Expand Down Expand Up @@ -117,6 +117,9 @@ export async function loadDocument(fileName: string, validateOnly = false): Prom
// finally relink all references
const relinkedModel = await relinkAll(model, services);

// filter out data model fields marked with `@ignore`
filterIgnoredFields(relinkedModel);

return relinkedModel;
}

Expand Down Expand Up @@ -368,6 +371,16 @@ async function relinkAll(model: Model, services: ZModelServices) {
return newDoc.parseResult.value as Model;
}

function filterIgnoredFields(model: Model) {
model.declarations.forEach((decl) => {
if (!isDataModel(decl)) {
return;
}
decl.$allFields = [...decl.fields];
decl.fields = decl.fields.filter((f) => !hasAttribute(f, '@ignore'));
});
}

export async function showNotification() {
try {
const fetchResult = await fetch(CLI_CONFIG_ENDPOINT, {
Expand Down
16 changes: 16 additions & 0 deletions tests/integration/tests/plugins/zod.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1081,4 +1081,20 @@ describe('Zod plugin tests', () => {
success: false,
});
});

it('ignores "@ignore" fields', async () => {
const { zodSchemas } = await loadSchema(
`
model User {
id Int @id @default(autoincrement())
email String @unique
password String @ignore
}
`
);

const schemas = zodSchemas.models;
expect(schemas.UserSchema.safeParse({ id: 1, email: '[email protected]' }).success).toBeTruthy();
expect(schemas.UserPrismaCreateSchema.safeParse({ email: '[email protected]' }).success).toBeTruthy();
});
});
87 changes: 44 additions & 43 deletions tests/integration/tests/schema/todo.zmodel
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,15 @@ plugin zod {
* Model for a space in which users can collaborate on Lists and Todos
*/
model Space {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String @length(4, 50)
slug String @unique @length(4, 16)
owner User? @relation(fields: [ownerId], references: [id])
ownerId String?
members SpaceUser[]
lists List[]
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
name String @length(4, 50)
slug String @unique @length(4, 16)
owner User? @relation(fields: [ownerId], references: [id])
ownerId String?
members SpaceUser[]
lists List[]

// require login
@@deny('all', auth() == null)
Expand All @@ -47,14 +47,14 @@ model Space {
* Model representing membership of a user in a space
*/
model SpaceUser {
id String @id @default(uuid())
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
spaceId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
role String
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
spaceId String
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
userId String
role String
@@unique([userId, spaceId])

// require login
Expand All @@ -71,18 +71,19 @@ model SpaceUser {
* Model for a user
*/
model User {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique @email
password String? @password @omit
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
email String @unique @email
password String? @password @omit
emailVerified DateTime?
name String?
ownedSpaces Space[]
spaces SpaceUser[]
image String? @url
lists List[]
todos Todo[]
name String?
bio String @ignore
ownedSpaces Space[]
spaces SpaceUser[]
image String? @url
lists List[]
todos Todo[]

// can be created by anyone, even not logged in
@@allow('create', true)
Expand All @@ -98,17 +99,17 @@ model User {
* Model for a Todo list
*/
model List {
id String @id @default(uuid())
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
spaceId String
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String
title String @length(1, 100)
private Boolean @default(false)
todos Todo[]
revision Int @default(0)
space Space @relation(fields: [spaceId], references: [id], onDelete: Cascade)
spaceId String
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String
title String @length(1, 100)
private Boolean @default(false)
todos Todo[]
revision Int @default(0)

// require login
@@deny('all', auth() == null)
Expand All @@ -131,14 +132,14 @@ model List {
* Model for a single Todo
*/
model Todo {
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String
list List @relation(fields: [listId], references: [id], onDelete: Cascade)
listId String
title String @length(1, 100)
id String @id @default(uuid())
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
ownerId String
list List @relation(fields: [listId], references: [id], onDelete: Cascade)
listId String
title String @length(1, 100)
completedAt DateTime?

// require login
Expand Down
Loading