prisma-guard 1.2.0 → 1.3.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,188 @@
1
+ import * as zod from 'zod';
2
+ import { z } from 'zod';
3
+ import * as zod_v4_core from 'zod/v4/core';
4
+
5
+ interface FieldMeta {
6
+ type: string;
7
+ isList: boolean;
8
+ isRequired: boolean;
9
+ isId: boolean;
10
+ isRelation: boolean;
11
+ hasDefault: boolean;
12
+ isUpdatedAt: boolean;
13
+ isEnum?: boolean;
14
+ isUnique?: boolean;
15
+ }
16
+ interface GuardLogger {
17
+ warn(message: string): void;
18
+ }
19
+ type DataFieldRefine = (base: z.ZodTypeAny) => z.ZodTypeAny;
20
+ type InputOpts = {
21
+ mode?: 'create' | 'update';
22
+ partial?: boolean;
23
+ allowNull?: boolean;
24
+ refine?: Record<string, (field: z.ZodTypeAny) => z.ZodTypeAny>;
25
+ } & ({
26
+ pick: string[];
27
+ omit?: never;
28
+ } | {
29
+ omit: string[];
30
+ pick?: never;
31
+ } | {
32
+ pick?: never;
33
+ omit?: never;
34
+ });
35
+ type ModelOpts = {
36
+ include?: Record<string, ModelOpts>;
37
+ _count?: true | Record<string, true>;
38
+ strict?: boolean;
39
+ maxDepth?: number;
40
+ } & ({
41
+ pick: string[];
42
+ omit?: never;
43
+ } | {
44
+ omit: string[];
45
+ pick?: never;
46
+ } | {
47
+ pick?: never;
48
+ omit?: never;
49
+ });
50
+ type QueryMethod = 'findMany' | 'findFirst' | 'findFirstOrThrow' | 'findUnique' | 'findUniqueOrThrow' | 'count' | 'aggregate' | 'groupBy';
51
+ type MutationMethod = 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany';
52
+ interface ShapeConfig {
53
+ where?: Record<string, unknown>;
54
+ include?: Record<string, true | NestedIncludeArgs>;
55
+ select?: Record<string, true | NestedSelectArgs>;
56
+ orderBy?: Record<string, true>;
57
+ cursor?: Record<string, true>;
58
+ take?: {
59
+ max: number;
60
+ default?: number;
61
+ };
62
+ skip?: true;
63
+ distinct?: string[];
64
+ _count?: true | Record<string, true>;
65
+ _avg?: Record<string, true>;
66
+ _sum?: Record<string, true>;
67
+ _min?: Record<string, true>;
68
+ _max?: Record<string, true>;
69
+ by?: string[];
70
+ having?: Record<string, true>;
71
+ }
72
+ interface NestedIncludeArgs {
73
+ where?: Record<string, unknown>;
74
+ include?: Record<string, true | NestedIncludeArgs>;
75
+ select?: Record<string, true | NestedSelectArgs>;
76
+ orderBy?: Record<string, true>;
77
+ cursor?: Record<string, true>;
78
+ take?: {
79
+ max: number;
80
+ default?: number;
81
+ };
82
+ skip?: true;
83
+ }
84
+ interface NestedSelectArgs {
85
+ select?: Record<string, true | NestedSelectArgs>;
86
+ where?: Record<string, unknown>;
87
+ orderBy?: Record<string, true>;
88
+ cursor?: Record<string, true>;
89
+ take?: {
90
+ max: number;
91
+ default?: number;
92
+ };
93
+ skip?: true;
94
+ }
95
+ type ShapeOrFn<TCtx = unknown> = ShapeConfig | ((ctx: TCtx) => ShapeConfig);
96
+ interface ScopeEntry {
97
+ readonly fk: string;
98
+ readonly root: string;
99
+ readonly relationName: string;
100
+ }
101
+ type ScopeMap = Record<string, readonly ScopeEntry[]>;
102
+ type TypeMap = Record<string, Record<string, FieldMeta>>;
103
+ type EnumMap = Record<string, readonly string[]>;
104
+ type ZodChains = Record<string, Record<string, (base: any) => z.ZodTypeAny>>;
105
+ type ZodDefaults = Record<string, readonly string[]>;
106
+ type UniqueConstraint = readonly string[];
107
+ type UniqueMap = Record<string, readonly UniqueConstraint[]>;
108
+ type MissingScopeContextMode = 'error' | 'warn' | 'ignore';
109
+ type FindUniqueMode = 'verify' | 'reject';
110
+ type OnScopeRelationWrite = 'error' | 'warn' | 'strip';
111
+ interface GuardGeneratedConfig {
112
+ onMissingScopeContext: MissingScopeContextMode;
113
+ findUniqueMode?: FindUniqueMode;
114
+ onScopeRelationWrite?: OnScopeRelationWrite;
115
+ strictDecimal?: boolean;
116
+ enforceProjection?: boolean;
117
+ }
118
+ interface GuardConfig {
119
+ scopeMap: ScopeMap;
120
+ typeMap: TypeMap;
121
+ enumMap: EnumMap;
122
+ zodChains: ZodChains;
123
+ guardConfig: GuardGeneratedConfig;
124
+ uniqueMap?: UniqueMap;
125
+ zodDefaults?: ZodDefaults;
126
+ logger?: GuardLogger;
127
+ wrapZodErrors?: boolean;
128
+ }
129
+ interface QuerySchema<TCtx = unknown> {
130
+ parse(body: unknown, opts?: {
131
+ ctx?: TCtx;
132
+ caller?: string;
133
+ }): Record<string, unknown>;
134
+ schemas: Partial<Record<string, z.ZodObject<any>>>;
135
+ }
136
+ interface InputSchema {
137
+ parse(data: unknown): Record<string, unknown>;
138
+ schema: z.ZodObject<any>;
139
+ }
140
+ interface GuardShape extends ShapeConfig {
141
+ data?: Record<string, true | DataFieldRefine | unknown>;
142
+ create?: Record<string, true | DataFieldRefine | unknown>;
143
+ update?: Record<string, true | DataFieldRefine | unknown>;
144
+ }
145
+ type GuardShapeOrFn = GuardShape | ((ctx: any) => GuardShape);
146
+ type GuardInput = GuardShapeOrFn | Record<string, GuardShapeOrFn>;
147
+ type GuardableMethodName = QueryMethod | MutationMethod;
148
+ type ExtractReturn<T, K extends string> = K extends keyof T ? T[K] extends (...args: any[]) => infer R ? R : never : never;
149
+ type GuardedModel<TDelegate> = {
150
+ [K in GuardableMethodName as K extends keyof TDelegate ? K : never]: ExtractReturn<TDelegate, K> extends never ? never : (body: unknown) => ExtractReturn<TDelegate, K>;
151
+ };
152
+
153
+ declare function createGuard<TModels extends TypeMap = TypeMap, TRoots extends string = string, TModelExt = unknown>(config: GuardConfig & {
154
+ typeMap: TModels;
155
+ }): {
156
+ input: (model: Extract<keyof TModels, string>, opts: InputOpts) => InputSchema;
157
+ model: (model: Extract<keyof TModels, string>, opts: ModelOpts) => zod.ZodObject<any, zod_v4_core.$strip>;
158
+ query: <TCtx = unknown>(model: Extract<keyof TModels, string>, method: QueryMethod, config_: ShapeOrFn<TCtx> | Record<string, ShapeOrFn<TCtx>>) => QuerySchema<TCtx>;
159
+ extension: <TCtx extends Record<string, unknown> = Record<string, unknown>>(contextFn: () => TCtx) => {
160
+ name: string;
161
+ model: TModelExt;
162
+ query: {
163
+ $allOperations({ model, operation, args, query }: any): any;
164
+ };
165
+ };
166
+ };
167
+
168
+ declare class PolicyError extends Error {
169
+ readonly status = 403;
170
+ readonly code = "POLICY_DENIED";
171
+ constructor(message?: string, options?: ErrorOptions);
172
+ }
173
+ declare class ShapeError extends Error {
174
+ readonly status = 400;
175
+ readonly code = "SHAPE_INVALID";
176
+ constructor(message: string, options?: ErrorOptions);
177
+ }
178
+ declare class CallerError extends Error {
179
+ readonly status = 400;
180
+ readonly code = "CALLER_UNKNOWN";
181
+ constructor(message: string, options?: ErrorOptions);
182
+ }
183
+
184
+ declare function force<T>(value: T): {
185
+ value: T;
186
+ };
187
+
188
+ export { CallerError, type EnumMap, type FieldMeta, type GuardConfig, type GuardGeneratedConfig, type GuardInput, type GuardLogger, type GuardShape, type GuardShapeOrFn, type GuardedModel, type InputOpts, type InputSchema, type MissingScopeContextMode, type ModelOpts, type MutationMethod, type NestedIncludeArgs, type NestedSelectArgs, PolicyError, type QueryMethod, type QuerySchema, type ScopeEntry, type ScopeMap, type ShapeConfig, ShapeError, type ShapeOrFn, type TypeMap, type UniqueConstraint, type UniqueMap, type ZodChains, type ZodDefaults, createGuard, force };
@@ -48,7 +48,7 @@ type ModelOpts = {
48
48
  omit?: never;
49
49
  });
50
50
  type QueryMethod = 'findMany' | 'findFirst' | 'findFirstOrThrow' | 'findUnique' | 'findUniqueOrThrow' | 'count' | 'aggregate' | 'groupBy';
51
- type MutationMethod = 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'delete' | 'deleteMany';
51
+ type MutationMethod = 'create' | 'createMany' | 'createManyAndReturn' | 'update' | 'updateMany' | 'updateManyAndReturn' | 'upsert' | 'delete' | 'deleteMany';
52
52
  interface ShapeConfig {
53
53
  where?: Record<string, unknown>;
54
54
  include?: Record<string, true | NestedIncludeArgs>;
@@ -112,6 +112,8 @@ interface GuardGeneratedConfig {
112
112
  onMissingScopeContext: MissingScopeContextMode;
113
113
  findUniqueMode?: FindUniqueMode;
114
114
  onScopeRelationWrite?: OnScopeRelationWrite;
115
+ strictDecimal?: boolean;
116
+ enforceProjection?: boolean;
115
117
  }
116
118
  interface GuardConfig {
117
119
  scopeMap: ScopeMap;
@@ -137,6 +139,8 @@ interface InputSchema {
137
139
  }
138
140
  interface GuardShape extends ShapeConfig {
139
141
  data?: Record<string, true | DataFieldRefine | unknown>;
142
+ create?: Record<string, true | DataFieldRefine | unknown>;
143
+ update?: Record<string, true | DataFieldRefine | unknown>;
140
144
  }
141
145
  type GuardShapeOrFn = GuardShape | ((ctx: any) => GuardShape);
142
146
  type GuardInput = GuardShapeOrFn | Record<string, GuardShapeOrFn>;
@@ -177,4 +181,8 @@ declare class CallerError extends Error {
177
181
  constructor(message: string, options?: ErrorOptions);
178
182
  }
179
183
 
180
- export { CallerError, type EnumMap, type FieldMeta, type GuardConfig, type GuardGeneratedConfig, type GuardInput, type GuardLogger, type GuardShape, type GuardShapeOrFn, type GuardedModel, type InputOpts, type InputSchema, type MissingScopeContextMode, type ModelOpts, type MutationMethod, type NestedIncludeArgs, type NestedSelectArgs, PolicyError, type QueryMethod, type QuerySchema, type ScopeEntry, type ScopeMap, type ShapeConfig, ShapeError, type ShapeOrFn, type TypeMap, type UniqueConstraint, type UniqueMap, type ZodChains, type ZodDefaults, createGuard };
184
+ declare function force<T>(value: T): {
185
+ value: T;
186
+ };
187
+
188
+ export { CallerError, type EnumMap, type FieldMeta, type GuardConfig, type GuardGeneratedConfig, type GuardInput, type GuardLogger, type GuardShape, type GuardShapeOrFn, type GuardedModel, type InputOpts, type InputSchema, type MissingScopeContextMode, type ModelOpts, type MutationMethod, type NestedIncludeArgs, type NestedSelectArgs, PolicyError, type QueryMethod, type QuerySchema, type ScopeEntry, type ScopeMap, type ShapeConfig, ShapeError, type ShapeOrFn, type TypeMap, type UniqueConstraint, type UniqueMap, type ZodChains, type ZodDefaults, createGuard, force };