|
9 | 9 | type BuiltinType, |
10 | 10 | type EnumDef, |
11 | 11 | type FieldDef, |
12 | | - type ProcedureDef, |
13 | 12 | type GetModels, |
14 | 13 | type ModelDef, |
| 14 | + type ProcedureDef, |
15 | 15 | type SchemaDef, |
16 | 16 | } from '../../../schema'; |
17 | 17 | import { extractFields } from '../../../utils/object-utils'; |
@@ -199,10 +199,7 @@ export class InputValidator<Schema extends SchemaDef> { |
199 | 199 | >(model, 'find', options, (model, options) => this.makeFindSchema(model, options), args); |
200 | 200 | } |
201 | 201 |
|
202 | | - validateExistsArgs( |
203 | | - model: GetModels<Schema>, |
204 | | - args: unknown, |
205 | | - ): ExistsArgs<Schema, GetModels<Schema>> | undefined { |
| 202 | + validateExistsArgs(model: GetModels<Schema>, args: unknown): ExistsArgs<Schema, GetModels<Schema>> | undefined { |
206 | 203 | return this.validate<ExistsArgs<Schema, GetModels<Schema>>>( |
207 | 204 | model, |
208 | 205 | 'exists', |
@@ -429,9 +426,11 @@ export class InputValidator<Schema extends SchemaDef> { |
429 | 426 | } |
430 | 427 |
|
431 | 428 | private makeExistsSchema(model: string) { |
432 | | - return z.strictObject({ |
433 | | - where: this.makeWhereSchema(model, false).optional(), |
434 | | - }).optional(); |
| 429 | + return z |
| 430 | + .strictObject({ |
| 431 | + where: this.makeWhereSchema(model, false).optional(), |
| 432 | + }) |
| 433 | + .optional(); |
435 | 434 | } |
436 | 435 |
|
437 | 436 | private makeScalarSchema(type: string, attributes?: readonly AttributeApplication[]) { |
@@ -577,7 +576,12 @@ export class InputValidator<Schema extends SchemaDef> { |
577 | 576 | if (enumDef) { |
578 | 577 | // enum |
579 | 578 | if (Object.keys(enumDef.values).length > 0) { |
580 | | - fieldSchema = this.makeEnumFilterSchema(enumDef, !!fieldDef.optional, withAggregations); |
| 579 | + fieldSchema = this.makeEnumFilterSchema( |
| 580 | + enumDef, |
| 581 | + !!fieldDef.optional, |
| 582 | + withAggregations, |
| 583 | + !!fieldDef.array, |
| 584 | + ); |
581 | 585 | } |
582 | 586 | } else if (fieldDef.array) { |
583 | 587 | // array field |
@@ -614,7 +618,12 @@ export class InputValidator<Schema extends SchemaDef> { |
614 | 618 | if (enumDef) { |
615 | 619 | // enum |
616 | 620 | if (Object.keys(enumDef.values).length > 0) { |
617 | | - fieldSchema = this.makeEnumFilterSchema(enumDef, !!def.optional, false); |
| 621 | + fieldSchema = this.makeEnumFilterSchema( |
| 622 | + enumDef, |
| 623 | + !!def.optional, |
| 624 | + false, |
| 625 | + false, |
| 626 | + ); |
618 | 627 | } else { |
619 | 628 | fieldSchema = z.never(); |
620 | 629 | } |
@@ -696,24 +705,23 @@ export class InputValidator<Schema extends SchemaDef> { |
696 | 705 | !!fieldDef.array, |
697 | 706 | ).optional(); |
698 | 707 | } else { |
699 | | - // array, enum, primitives |
700 | | - if (fieldDef.array) { |
| 708 | + // enum, array, primitives |
| 709 | + const enumDef = getEnum(this.schema, fieldDef.type); |
| 710 | + if (enumDef) { |
| 711 | + fieldSchemas[fieldName] = this.makeEnumFilterSchema( |
| 712 | + enumDef, |
| 713 | + !!fieldDef.optional, |
| 714 | + false, |
| 715 | + !!fieldDef.array, |
| 716 | + ).optional(); |
| 717 | + } else if (fieldDef.array) { |
701 | 718 | fieldSchemas[fieldName] = this.makeArrayFilterSchema(fieldDef.type as BuiltinType).optional(); |
702 | 719 | } else { |
703 | | - const enumDef = getEnum(this.schema, fieldDef.type); |
704 | | - if (enumDef) { |
705 | | - fieldSchemas[fieldName] = this.makeEnumFilterSchema( |
706 | | - enumDef, |
707 | | - !!fieldDef.optional, |
708 | | - false, |
709 | | - ).optional(); |
710 | | - } else { |
711 | | - fieldSchemas[fieldName] = this.makePrimitiveFilterSchema( |
712 | | - fieldDef.type as BuiltinType, |
713 | | - !!fieldDef.optional, |
714 | | - false, |
715 | | - ).optional(); |
716 | | - } |
| 720 | + fieldSchemas[fieldName] = this.makePrimitiveFilterSchema( |
| 721 | + fieldDef.type as BuiltinType, |
| 722 | + !!fieldDef.optional, |
| 723 | + false, |
| 724 | + ).optional(); |
717 | 725 | } |
718 | 726 | } |
719 | 727 | } |
@@ -757,24 +765,31 @@ export class InputValidator<Schema extends SchemaDef> { |
757 | 765 | return this.schema.typeDefs && type in this.schema.typeDefs; |
758 | 766 | } |
759 | 767 |
|
760 | | - private makeEnumFilterSchema(enumDef: EnumDef, optional: boolean, withAggregations: boolean) { |
| 768 | + private makeEnumFilterSchema(enumDef: EnumDef, optional: boolean, withAggregations: boolean, array: boolean) { |
761 | 769 | const baseSchema = z.enum(Object.keys(enumDef.values) as [string, ...string[]]); |
| 770 | + if (array) { |
| 771 | + return this.internalMakeArrayFilterSchema(baseSchema); |
| 772 | + } |
762 | 773 | const components = this.makeCommonPrimitiveFilterComponents( |
763 | 774 | baseSchema, |
764 | 775 | optional, |
765 | | - () => z.lazy(() => this.makeEnumFilterSchema(enumDef, optional, withAggregations)), |
| 776 | + () => z.lazy(() => this.makeEnumFilterSchema(enumDef, optional, withAggregations, array)), |
766 | 777 | ['equals', 'in', 'notIn', 'not'], |
767 | 778 | withAggregations ? ['_count', '_min', '_max'] : undefined, |
768 | 779 | ); |
769 | 780 | return z.union([this.nullableIf(baseSchema, optional), z.strictObject(components)]); |
770 | 781 | } |
771 | 782 |
|
772 | 783 | private makeArrayFilterSchema(type: BuiltinType) { |
| 784 | + return this.internalMakeArrayFilterSchema(this.makeScalarSchema(type)); |
| 785 | + } |
| 786 | + |
| 787 | + private internalMakeArrayFilterSchema(elementSchema: ZodType) { |
773 | 788 | return z.strictObject({ |
774 | | - equals: this.makeScalarSchema(type).array().optional(), |
775 | | - has: this.makeScalarSchema(type).optional(), |
776 | | - hasEvery: this.makeScalarSchema(type).array().optional(), |
777 | | - hasSome: this.makeScalarSchema(type).array().optional(), |
| 789 | + equals: elementSchema.array().optional(), |
| 790 | + has: elementSchema.optional(), |
| 791 | + hasEvery: elementSchema.array().optional(), |
| 792 | + hasSome: elementSchema.array().optional(), |
778 | 793 | isEmpty: z.boolean().optional(), |
779 | 794 | }); |
780 | 795 | } |
|
0 commit comments