| 33 | }; |
| 34 | |
| 35 | export class InputValidator<Schema extends SchemaDef> { |
| 36 | readonly zodFactory: ZodSchemaFactory<Schema>; |
| 37 | private readonly enabled: boolean; |
| 38 | |
| 39 | constructor( |
| 40 | private readonly client: ClientContract<Schema>, |
| 41 | options?: InputValidatorOptions, |
| 42 | ) { |
| 43 | this.zodFactory = new ZodSchemaFactory(client); |
| 44 | this.enabled = options?.enabled !== false; |
| 45 | } |
| 46 | |
| 47 | // #region Entry points |
| 48 | |
| 49 | validateFindArgs( |
| 50 | model: GetModels<Schema>, |
| 51 | args: unknown, |
| 52 | operation: 'findFirst' | 'findUnique' | 'findMany', |
| 53 | ): FindArgs<Schema, GetModels<Schema>, any, true> | undefined { |
| 54 | return this.validate<FindArgs<Schema, GetModels<Schema>, any, true> | undefined>( |
| 55 | model, |
| 56 | operation, |
| 57 | (model) => |
| 58 | match(operation) |
| 59 | .with('findFirst', () => this.zodFactory.makeFindFirstSchema(model)) |
| 60 | .with('findUnique', () => this.zodFactory.makeFindUniqueSchema(model)) |
| 61 | .with('findMany', () => this.zodFactory.makeFindManySchema(model)) |
| 62 | .exhaustive(), |
| 63 | args, |
| 64 | ); |
| 65 | } |
| 66 | |
| 67 | validateExistsArgs( |
| 68 | model: GetModels<Schema>, |
| 69 | args: unknown, |
| 70 | ): ExistsArgs<Schema, GetModels<Schema>, any> | undefined { |
| 71 | return this.validate<ExistsArgs<Schema, GetModels<Schema>, any> | undefined>( |
| 72 | model, |
| 73 | 'exists', |
| 74 | (model) => this.zodFactory.makeExistsSchema(model), |
| 75 | args, |
| 76 | ); |
| 77 | } |
| 78 | |
| 79 | validateCreateArgs(model: GetModels<Schema>, args: unknown): CreateArgs<Schema, GetModels<Schema>, any> { |
| 80 | return this.validate<CreateArgs<Schema, GetModels<Schema>, any>>( |
| 81 | model, |
| 82 | 'create', |
| 83 | (model) => this.zodFactory.makeCreateSchema(model), |
| 84 | args, |
| 85 | ); |
| 86 | } |
| 87 | |
| 88 | validateCreateManyArgs(model: GetModels<Schema>, args: unknown): CreateManyArgs<Schema, GetModels<Schema>> { |
| 89 | return this.validate<CreateManyArgs<Schema, GetModels<Schema>>>( |
| 90 | model, |
| 91 | 'createMany', |
| 92 | (model) => this.zodFactory.makeCreateManySchema(model), |
nothing calls this directly
no outgoing calls
no test coverage detected