Skip to content
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
26 changes: 21 additions & 5 deletions packages/runtime/src/client/crud-types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,12 @@ export type WhereInput<
? ArrayFilter<Schema, GetModelFieldType<Schema, Model, Key>>
: // enum
GetModelFieldType<Schema, Model, Key> extends GetEnums<Schema>
? EnumFilter<Schema, GetModelFieldType<Schema, Model, Key>, ModelFieldIsOptional<Schema, Model, Key>>
? EnumFilter<
Schema,
GetModelFieldType<Schema, Model, Key>,
ModelFieldIsOptional<Schema, Model, Key>,
WithAggregations
>
: // primitive
PrimitiveFilter<
Schema,
Expand All @@ -237,14 +242,25 @@ export type WhereInput<
NOT?: OrArray<WhereInput<Schema, Model, ScalarOnly>>;
};

type EnumFilter<Schema extends SchemaDef, T extends GetEnums<Schema>, Nullable extends boolean> =
type EnumFilter<
Schema extends SchemaDef,
T extends GetEnums<Schema>,
Nullable extends boolean,
WithAggregations extends boolean,
> =
| NullableIf<keyof GetEnum<Schema, T>, Nullable>
| {
| ({
equals?: NullableIf<keyof GetEnum<Schema, T>, Nullable>;
in?: (keyof GetEnum<Schema, T>)[];
notIn?: (keyof GetEnum<Schema, T>)[];
not?: EnumFilter<Schema, T, Nullable>;
};
not?: EnumFilter<Schema, T, Nullable, WithAggregations>;
} & (WithAggregations extends true
? {
_count?: NumberFilter<Schema, 'Int', false, false>;
_min?: EnumFilter<Schema, T, false, false>;
_max?: EnumFilter<Schema, T, false, false>;
}
: {}));

type ArrayFilter<Schema extends SchemaDef, T extends string> = {
equals?: MapScalarType<Schema, T>[] | null;
Expand Down
6 changes: 5 additions & 1 deletion packages/runtime/src/client/crud/validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1256,11 +1256,15 @@ export class InputValidator<Schema extends SchemaDef> {
private makeGroupBySchema(model: GetModels<Schema>) {
const modelDef = requireModel(this.schema, model);
const nonRelationFields = Object.keys(modelDef.fields).filter((field) => !modelDef.fields[field]?.relation);
const bySchema =
nonRelationFields.length > 0
? this.orArray(z.enum(nonRelationFields as [string, ...string[]]), true)
: z.never();

let schema: z.ZodSchema = z.strictObject({
where: this.makeWhereSchema(model, false).optional(),
orderBy: this.orArray(this.makeOrderBySchema(model, false, true), true).optional(),
by: this.orArray(z.enum(nonRelationFields as [string, ...string[]]), true),
by: bySchema,
having: this.makeHavingSchema(model).optional(),
skip: this.makeSkipSchema().optional(),
take: this.makeTakeSchema().optional(),
Expand Down