@zapier/policy-schema 0.7.0 → 0.8.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.
package/dist/index.cjs CHANGED
@@ -3,6 +3,21 @@
3
3
  var zod = require('zod');
4
4
 
5
5
  // src/schema.ts
6
+ var VALID_VERSIONS = [1, 2];
7
+ var VALID_EFFECTS = ["allow", "ask", "deny"];
8
+ var VALID_OPERATOR_LIST = [
9
+ "equals",
10
+ "exists",
11
+ "in",
12
+ "keys_in",
13
+ "matches",
14
+ "matches_host",
15
+ "matches_path",
16
+ "matches_url",
17
+ "range",
18
+ "size",
19
+ "values_in"
20
+ ];
6
21
  var ScalarSchema = zod.z.union([zod.z.string(), zod.z.number(), zod.z.boolean()]);
7
22
  var PathSchema = zod.z.array(zod.z.union([zod.z.string(), zod.z.number()])).min(1);
8
23
  var JsonValueSchema = zod.z.lazy(
@@ -125,16 +140,100 @@ var OperatorSchema = zod.z.enum([
125
140
  "size"
126
141
  ]);
127
142
  var ConditionValueSchema = JsonValueSchema;
128
- var StatementSchema = zod.z.object({
143
+ var V1StatementSchema = zod.z.object({
144
+ id: zod.z.string().optional(),
145
+ effect: zod.z.enum(["allow", "deny"]),
146
+ permissions: zod.z.array(zod.z.string()).min(1),
147
+ resources: zod.z.array(zod.z.string()).min(1),
148
+ conditions: zod.z.array(ConditionSchema).optional()
149
+ }).strict();
150
+ var V2StatementSchema = zod.z.object({
151
+ id: zod.z.string().optional(),
129
152
  effect: zod.z.enum(["allow", "ask", "deny"]),
130
153
  permissions: zod.z.array(zod.z.string()).min(1),
131
154
  resources: zod.z.array(zod.z.string()).min(1),
132
155
  conditions: zod.z.array(ConditionSchema).optional()
133
156
  }).strict();
134
- var PolicySchema = zod.z.object({
157
+ var StatementSchema = V2StatementSchema;
158
+ var V1PolicySchema = zod.z.object({
159
+ version: zod.z.literal(1),
160
+ statements: zod.z.array(V1StatementSchema)
161
+ }).strict();
162
+ var V2PolicySchema = zod.z.object({
135
163
  version: zod.z.literal(2),
136
- statements: zod.z.array(StatementSchema)
164
+ statements: zod.z.array(V2StatementSchema)
137
165
  }).strict();
166
+ var PolicySchema = zod.z.union([V1PolicySchema, V2PolicySchema]);
167
+ function getOpenApiSchema() {
168
+ return {
169
+ type: "object",
170
+ required: ["version", "statements"],
171
+ properties: {
172
+ version: {
173
+ type: "integer",
174
+ enum: [...VALID_VERSIONS],
175
+ description: `Policy schema version. Must be one of ${JSON.stringify([...VALID_VERSIONS])}.`
176
+ },
177
+ statements: {
178
+ type: "array",
179
+ description: "List of policy statements defining permissions.",
180
+ items: {
181
+ type: "object",
182
+ required: ["effect", "permissions", "resources"],
183
+ properties: {
184
+ id: {
185
+ type: "string",
186
+ description: "Optional identifier for debugging and provenance. Included in canonical hash."
187
+ },
188
+ effect: {
189
+ type: "string",
190
+ enum: [...VALID_EFFECTS].sort(),
191
+ description: "Whether to allow, ask, or deny the specified permissions. The 'ask' effect is only valid in version 2 policies."
192
+ },
193
+ permissions: {
194
+ type: "array",
195
+ minItems: 1,
196
+ items: { type: "string" },
197
+ description: "List of permission identifiers."
198
+ },
199
+ resources: {
200
+ type: "array",
201
+ minItems: 1,
202
+ items: { type: "string" },
203
+ description: "List of resource identifiers the statement applies to."
204
+ },
205
+ conditions: {
206
+ type: "array",
207
+ description: "Optional conditions that must be met for the statement to apply.",
208
+ items: {
209
+ type: "object",
210
+ required: ["path", "operator"],
211
+ properties: {
212
+ path: {
213
+ type: "array",
214
+ minItems: 1,
215
+ description: "Path to the value to evaluate in the request context.",
216
+ items: {
217
+ anyOf: [{ type: "string" }, { type: "integer" }]
218
+ }
219
+ },
220
+ operator: {
221
+ type: "string",
222
+ enum: [...VALID_OPERATOR_LIST],
223
+ description: "Comparison operator used to evaluate the condition."
224
+ },
225
+ value: {
226
+ description: "Value to compare against using the operator."
227
+ }
228
+ }
229
+ }
230
+ }
231
+ }
232
+ }
233
+ }
234
+ }
235
+ };
236
+ }
138
237
 
139
238
  exports.ArrayOperatorSchema = ArrayOperatorSchema;
140
239
  exports.ConditionSchema = ConditionSchema;
@@ -147,3 +246,7 @@ exports.ScalarOperatorSchema = ScalarOperatorSchema;
147
246
  exports.SizeValueSchema = SizeValueSchema;
148
247
  exports.StatementSchema = StatementSchema;
149
248
  exports.UrlOperatorSchema = UrlOperatorSchema;
249
+ exports.VALID_EFFECTS = VALID_EFFECTS;
250
+ exports.VALID_OPERATOR_LIST = VALID_OPERATOR_LIST;
251
+ exports.VALID_VERSIONS = VALID_VERSIONS;
252
+ exports.getOpenApiSchema = getOpenApiSchema;
package/dist/index.d.cts CHANGED
@@ -21,13 +21,14 @@ interface Condition {
21
21
  }
22
22
  type Effect = "allow" | "ask" | "deny";
23
23
  interface Statement {
24
+ id?: string;
24
25
  effect: Effect;
25
26
  permissions: string[];
26
27
  resources: string[];
27
28
  conditions?: Condition[];
28
29
  }
29
30
  interface Policy {
30
- version: 2;
31
+ version: 1 | 2;
31
32
  statements: Statement[];
32
33
  }
33
34
  interface PolicyRequest {
@@ -35,9 +36,52 @@ interface PolicyRequest {
35
36
  resource: string;
36
37
  context?: Record<string, unknown>;
37
38
  }
39
+ /** SHA-256 hex digest of an RFC 8785 (JCS) canonicalized JSON value. */
40
+ type CanonicalHash = string & {
41
+ readonly __brand: "CanonicalHash";
42
+ };
43
+ interface OverrideBase {
44
+ id?: string;
45
+ statementHash: CanonicalHash;
46
+ effect: "allow";
47
+ }
48
+ type Override = (OverrideBase & {
49
+ match: "any";
50
+ }) | (OverrideBase & {
51
+ contextHash: CanonicalHash;
52
+ }) | (OverrideBase & {
53
+ conditionals: Condition[];
54
+ });
55
+ interface ConditionTrace {
56
+ condition: Condition;
57
+ resolved: unknown;
58
+ passed: boolean;
59
+ }
60
+ interface OverrideTrace {
61
+ override: Override;
62
+ matched: boolean;
63
+ conditionResults?: ConditionTrace[];
64
+ }
65
+ interface StatementTrace {
66
+ statement: Statement;
67
+ permissionMatched: boolean;
68
+ resourceMatched: boolean;
69
+ conditionResults?: ConditionTrace[];
70
+ passed: boolean;
71
+ overrides?: OverrideTrace[];
72
+ }
73
+ interface EvaluatePolicyOptions {
74
+ policy: Policy;
75
+ request: PolicyRequest;
76
+ overrides?: Override[];
77
+ trace?: boolean;
78
+ }
79
+ type PolicyReason = "matched" | "unmatched" | "hpp" | "overridden";
38
80
  interface PolicyResult {
39
81
  effect: Effect;
40
- matchedStatement?: Statement;
82
+ explicit: boolean;
83
+ reason: PolicyReason;
84
+ statements: StatementTrace[];
41
85
  }
42
86
  interface ParsedUrl {
43
87
  scheme: string;
@@ -52,8 +96,34 @@ interface PolicyHttpRequest {
52
96
  headers: Record<string, string | string[]>;
53
97
  body: string | null;
54
98
  body_json: unknown;
99
+ connection_id?: string;
100
+ }
101
+ interface PolicyActionRequest {
102
+ connection_id?: string;
103
+ selected_api?: string;
104
+ action_type?: string;
105
+ action_key?: string;
106
+ inputs?: Record<string, unknown>;
107
+ }
108
+ /** Simplified OpenAPI-compatible JSON Schema type for getOpenApiSchema(). */
109
+ interface OpenApiSchema {
110
+ type?: string;
111
+ required?: string[];
112
+ properties?: Record<string, OpenApiSchema>;
113
+ items?: OpenApiSchema;
114
+ enum?: (string | number)[];
115
+ description?: string;
116
+ minItems?: number;
117
+ oneOf?: OpenApiSchema[];
118
+ anyOf?: OpenApiSchema[];
55
119
  }
56
120
 
121
+ /** Current policy document version. */
122
+ declare const VALID_VERSIONS: readonly [1, 2];
123
+ /** Valid policy effects. */
124
+ declare const VALID_EFFECTS: readonly ["allow", "ask", "deny"];
125
+ /** Valid condition operators. */
126
+ declare const VALID_OPERATOR_LIST: readonly ["equals", "exists", "in", "keys_in", "matches", "matches_host", "matches_path", "matches_url", "range", "size", "values_in"];
57
127
  declare const RangeValueSchema: z.ZodObject<{
58
128
  min: z.ZodOptional<z.ZodNumber>;
59
129
  max: z.ZodOptional<z.ZodNumber>;
@@ -145,7 +215,9 @@ declare const OperatorSchema: z.ZodEnum<{
145
215
  matches_url: "matches_url";
146
216
  }>;
147
217
  declare const ConditionValueSchema: z.ZodType<JsonValue, unknown, z.core.$ZodTypeInternals<JsonValue, unknown>>;
218
+ /** Statement schema accepting the union of all effects (use PolicySchema for version-aware validation). */
148
219
  declare const StatementSchema: z.ZodObject<{
220
+ id: z.ZodOptional<z.ZodString>;
149
221
  effect: z.ZodEnum<{
150
222
  allow: "allow";
151
223
  ask: "ask";
@@ -204,9 +276,71 @@ declare const StatementSchema: z.ZodObject<{
204
276
  }, z.core.$strict>]>;
205
277
  }, z.core.$strict>]>>>;
206
278
  }, z.core.$strict>;
207
- declare const PolicySchema: z.ZodObject<{
279
+ declare const PolicySchema: z.ZodUnion<readonly [z.ZodObject<{
280
+ version: z.ZodLiteral<1>;
281
+ statements: z.ZodArray<z.ZodObject<{
282
+ id: z.ZodOptional<z.ZodString>;
283
+ effect: z.ZodEnum<{
284
+ allow: "allow";
285
+ deny: "deny";
286
+ }>;
287
+ permissions: z.ZodArray<z.ZodString>;
288
+ resources: z.ZodArray<z.ZodString>;
289
+ conditions: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
290
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
291
+ operator: z.ZodLiteral<"equals">;
292
+ value: z.ZodType<JsonValue, unknown, z.core.$ZodTypeInternals<JsonValue, unknown>>;
293
+ }, z.core.$strict>, z.ZodObject<{
294
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
295
+ operator: z.ZodLiteral<"in">;
296
+ value: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>;
297
+ }, z.core.$strict>, z.ZodObject<{
298
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
299
+ operator: z.ZodLiteral<"matches">;
300
+ value: z.ZodString;
301
+ }, z.core.$strict>, z.ZodObject<{
302
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
303
+ operator: z.ZodLiteral<"matches_host">;
304
+ value: z.ZodString;
305
+ }, z.core.$strict>, z.ZodObject<{
306
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
307
+ operator: z.ZodLiteral<"matches_path">;
308
+ value: z.ZodString;
309
+ }, z.core.$strict>, z.ZodObject<{
310
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
311
+ operator: z.ZodLiteral<"range">;
312
+ value: z.ZodObject<{
313
+ min: z.ZodOptional<z.ZodNumber>;
314
+ max: z.ZodOptional<z.ZodNumber>;
315
+ }, z.core.$strict>;
316
+ }, z.core.$strict>, z.ZodObject<{
317
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
318
+ operator: z.ZodLiteral<"matches_url">;
319
+ value: z.ZodString;
320
+ }, z.core.$strict>, z.ZodObject<{
321
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
322
+ operator: z.ZodLiteral<"values_in">;
323
+ value: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>;
324
+ }, z.core.$strict>, z.ZodObject<{
325
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
326
+ operator: z.ZodLiteral<"exists">;
327
+ }, z.core.$strict>, z.ZodObject<{
328
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
329
+ operator: z.ZodLiteral<"keys_in">;
330
+ value: z.ZodArray<z.ZodString>;
331
+ }, z.core.$strict>, z.ZodObject<{
332
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
333
+ operator: z.ZodLiteral<"size">;
334
+ value: z.ZodUnion<readonly [z.ZodNumber, z.ZodObject<{
335
+ min: z.ZodOptional<z.ZodNumber>;
336
+ max: z.ZodOptional<z.ZodNumber>;
337
+ }, z.core.$strict>]>;
338
+ }, z.core.$strict>]>>>;
339
+ }, z.core.$strict>>;
340
+ }, z.core.$strict>, z.ZodObject<{
208
341
  version: z.ZodLiteral<2>;
209
342
  statements: z.ZodArray<z.ZodObject<{
343
+ id: z.ZodOptional<z.ZodString>;
210
344
  effect: z.ZodEnum<{
211
345
  allow: "allow";
212
346
  ask: "ask";
@@ -265,6 +399,13 @@ declare const PolicySchema: z.ZodObject<{
265
399
  }, z.core.$strict>]>;
266
400
  }, z.core.$strict>]>>>;
267
401
  }, z.core.$strict>>;
268
- }, z.core.$strict>;
402
+ }, z.core.$strict>]>;
403
+ /**
404
+ * Returns an OpenAPI-compatible JSON Schema object for a policy document.
405
+ *
406
+ * Derived from the same constants used by the Zod schemas so the
407
+ * documentation schema and runtime validation always stay in sync.
408
+ */
409
+ declare function getOpenApiSchema(): OpenApiSchema;
269
410
 
270
- export { type ArrayOperator, ArrayOperatorSchema, type Condition, ConditionSchema, type ConditionValue, ConditionValueSchema, type Effect, type JsonValue, type Operator, OperatorSchema, type OtherOperator, OtherOperatorSchema, type ParsedUrl, type Policy, type PolicyHttpRequest, type PolicyRequest, type PolicyResult, PolicySchema, type RangeValue, RangeValueSchema, type ScalarOperator, ScalarOperatorSchema, type SizeValue, SizeValueSchema, type Statement, StatementSchema, type UrlOperator, UrlOperatorSchema };
411
+ export { type ArrayOperator, ArrayOperatorSchema, type CanonicalHash, type Condition, ConditionSchema, type ConditionTrace, type ConditionValue, ConditionValueSchema, type Effect, type EvaluatePolicyOptions, type JsonValue, type OpenApiSchema, type Operator, OperatorSchema, type OtherOperator, OtherOperatorSchema, type Override, type OverrideTrace, type ParsedUrl, type Policy, type PolicyActionRequest, type PolicyHttpRequest, type PolicyReason, type PolicyRequest, type PolicyResult, PolicySchema, type RangeValue, RangeValueSchema, type ScalarOperator, ScalarOperatorSchema, type SizeValue, SizeValueSchema, type Statement, StatementSchema, type StatementTrace, type UrlOperator, UrlOperatorSchema, VALID_EFFECTS, VALID_OPERATOR_LIST, VALID_VERSIONS, getOpenApiSchema };
package/dist/index.d.ts CHANGED
@@ -21,13 +21,14 @@ interface Condition {
21
21
  }
22
22
  type Effect = "allow" | "ask" | "deny";
23
23
  interface Statement {
24
+ id?: string;
24
25
  effect: Effect;
25
26
  permissions: string[];
26
27
  resources: string[];
27
28
  conditions?: Condition[];
28
29
  }
29
30
  interface Policy {
30
- version: 2;
31
+ version: 1 | 2;
31
32
  statements: Statement[];
32
33
  }
33
34
  interface PolicyRequest {
@@ -35,9 +36,52 @@ interface PolicyRequest {
35
36
  resource: string;
36
37
  context?: Record<string, unknown>;
37
38
  }
39
+ /** SHA-256 hex digest of an RFC 8785 (JCS) canonicalized JSON value. */
40
+ type CanonicalHash = string & {
41
+ readonly __brand: "CanonicalHash";
42
+ };
43
+ interface OverrideBase {
44
+ id?: string;
45
+ statementHash: CanonicalHash;
46
+ effect: "allow";
47
+ }
48
+ type Override = (OverrideBase & {
49
+ match: "any";
50
+ }) | (OverrideBase & {
51
+ contextHash: CanonicalHash;
52
+ }) | (OverrideBase & {
53
+ conditionals: Condition[];
54
+ });
55
+ interface ConditionTrace {
56
+ condition: Condition;
57
+ resolved: unknown;
58
+ passed: boolean;
59
+ }
60
+ interface OverrideTrace {
61
+ override: Override;
62
+ matched: boolean;
63
+ conditionResults?: ConditionTrace[];
64
+ }
65
+ interface StatementTrace {
66
+ statement: Statement;
67
+ permissionMatched: boolean;
68
+ resourceMatched: boolean;
69
+ conditionResults?: ConditionTrace[];
70
+ passed: boolean;
71
+ overrides?: OverrideTrace[];
72
+ }
73
+ interface EvaluatePolicyOptions {
74
+ policy: Policy;
75
+ request: PolicyRequest;
76
+ overrides?: Override[];
77
+ trace?: boolean;
78
+ }
79
+ type PolicyReason = "matched" | "unmatched" | "hpp" | "overridden";
38
80
  interface PolicyResult {
39
81
  effect: Effect;
40
- matchedStatement?: Statement;
82
+ explicit: boolean;
83
+ reason: PolicyReason;
84
+ statements: StatementTrace[];
41
85
  }
42
86
  interface ParsedUrl {
43
87
  scheme: string;
@@ -52,8 +96,34 @@ interface PolicyHttpRequest {
52
96
  headers: Record<string, string | string[]>;
53
97
  body: string | null;
54
98
  body_json: unknown;
99
+ connection_id?: string;
100
+ }
101
+ interface PolicyActionRequest {
102
+ connection_id?: string;
103
+ selected_api?: string;
104
+ action_type?: string;
105
+ action_key?: string;
106
+ inputs?: Record<string, unknown>;
107
+ }
108
+ /** Simplified OpenAPI-compatible JSON Schema type for getOpenApiSchema(). */
109
+ interface OpenApiSchema {
110
+ type?: string;
111
+ required?: string[];
112
+ properties?: Record<string, OpenApiSchema>;
113
+ items?: OpenApiSchema;
114
+ enum?: (string | number)[];
115
+ description?: string;
116
+ minItems?: number;
117
+ oneOf?: OpenApiSchema[];
118
+ anyOf?: OpenApiSchema[];
55
119
  }
56
120
 
121
+ /** Current policy document version. */
122
+ declare const VALID_VERSIONS: readonly [1, 2];
123
+ /** Valid policy effects. */
124
+ declare const VALID_EFFECTS: readonly ["allow", "ask", "deny"];
125
+ /** Valid condition operators. */
126
+ declare const VALID_OPERATOR_LIST: readonly ["equals", "exists", "in", "keys_in", "matches", "matches_host", "matches_path", "matches_url", "range", "size", "values_in"];
57
127
  declare const RangeValueSchema: z.ZodObject<{
58
128
  min: z.ZodOptional<z.ZodNumber>;
59
129
  max: z.ZodOptional<z.ZodNumber>;
@@ -145,7 +215,9 @@ declare const OperatorSchema: z.ZodEnum<{
145
215
  matches_url: "matches_url";
146
216
  }>;
147
217
  declare const ConditionValueSchema: z.ZodType<JsonValue, unknown, z.core.$ZodTypeInternals<JsonValue, unknown>>;
218
+ /** Statement schema accepting the union of all effects (use PolicySchema for version-aware validation). */
148
219
  declare const StatementSchema: z.ZodObject<{
220
+ id: z.ZodOptional<z.ZodString>;
149
221
  effect: z.ZodEnum<{
150
222
  allow: "allow";
151
223
  ask: "ask";
@@ -204,9 +276,71 @@ declare const StatementSchema: z.ZodObject<{
204
276
  }, z.core.$strict>]>;
205
277
  }, z.core.$strict>]>>>;
206
278
  }, z.core.$strict>;
207
- declare const PolicySchema: z.ZodObject<{
279
+ declare const PolicySchema: z.ZodUnion<readonly [z.ZodObject<{
280
+ version: z.ZodLiteral<1>;
281
+ statements: z.ZodArray<z.ZodObject<{
282
+ id: z.ZodOptional<z.ZodString>;
283
+ effect: z.ZodEnum<{
284
+ allow: "allow";
285
+ deny: "deny";
286
+ }>;
287
+ permissions: z.ZodArray<z.ZodString>;
288
+ resources: z.ZodArray<z.ZodString>;
289
+ conditions: z.ZodOptional<z.ZodArray<z.ZodUnion<readonly [z.ZodObject<{
290
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
291
+ operator: z.ZodLiteral<"equals">;
292
+ value: z.ZodType<JsonValue, unknown, z.core.$ZodTypeInternals<JsonValue, unknown>>;
293
+ }, z.core.$strict>, z.ZodObject<{
294
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
295
+ operator: z.ZodLiteral<"in">;
296
+ value: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>;
297
+ }, z.core.$strict>, z.ZodObject<{
298
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
299
+ operator: z.ZodLiteral<"matches">;
300
+ value: z.ZodString;
301
+ }, z.core.$strict>, z.ZodObject<{
302
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
303
+ operator: z.ZodLiteral<"matches_host">;
304
+ value: z.ZodString;
305
+ }, z.core.$strict>, z.ZodObject<{
306
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
307
+ operator: z.ZodLiteral<"matches_path">;
308
+ value: z.ZodString;
309
+ }, z.core.$strict>, z.ZodObject<{
310
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
311
+ operator: z.ZodLiteral<"range">;
312
+ value: z.ZodObject<{
313
+ min: z.ZodOptional<z.ZodNumber>;
314
+ max: z.ZodOptional<z.ZodNumber>;
315
+ }, z.core.$strict>;
316
+ }, z.core.$strict>, z.ZodObject<{
317
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
318
+ operator: z.ZodLiteral<"matches_url">;
319
+ value: z.ZodString;
320
+ }, z.core.$strict>, z.ZodObject<{
321
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
322
+ operator: z.ZodLiteral<"values_in">;
323
+ value: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber, z.ZodBoolean]>>;
324
+ }, z.core.$strict>, z.ZodObject<{
325
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
326
+ operator: z.ZodLiteral<"exists">;
327
+ }, z.core.$strict>, z.ZodObject<{
328
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
329
+ operator: z.ZodLiteral<"keys_in">;
330
+ value: z.ZodArray<z.ZodString>;
331
+ }, z.core.$strict>, z.ZodObject<{
332
+ path: z.ZodArray<z.ZodUnion<readonly [z.ZodString, z.ZodNumber]>>;
333
+ operator: z.ZodLiteral<"size">;
334
+ value: z.ZodUnion<readonly [z.ZodNumber, z.ZodObject<{
335
+ min: z.ZodOptional<z.ZodNumber>;
336
+ max: z.ZodOptional<z.ZodNumber>;
337
+ }, z.core.$strict>]>;
338
+ }, z.core.$strict>]>>>;
339
+ }, z.core.$strict>>;
340
+ }, z.core.$strict>, z.ZodObject<{
208
341
  version: z.ZodLiteral<2>;
209
342
  statements: z.ZodArray<z.ZodObject<{
343
+ id: z.ZodOptional<z.ZodString>;
210
344
  effect: z.ZodEnum<{
211
345
  allow: "allow";
212
346
  ask: "ask";
@@ -265,6 +399,13 @@ declare const PolicySchema: z.ZodObject<{
265
399
  }, z.core.$strict>]>;
266
400
  }, z.core.$strict>]>>>;
267
401
  }, z.core.$strict>>;
268
- }, z.core.$strict>;
402
+ }, z.core.$strict>]>;
403
+ /**
404
+ * Returns an OpenAPI-compatible JSON Schema object for a policy document.
405
+ *
406
+ * Derived from the same constants used by the Zod schemas so the
407
+ * documentation schema and runtime validation always stay in sync.
408
+ */
409
+ declare function getOpenApiSchema(): OpenApiSchema;
269
410
 
270
- export { type ArrayOperator, ArrayOperatorSchema, type Condition, ConditionSchema, type ConditionValue, ConditionValueSchema, type Effect, type JsonValue, type Operator, OperatorSchema, type OtherOperator, OtherOperatorSchema, type ParsedUrl, type Policy, type PolicyHttpRequest, type PolicyRequest, type PolicyResult, PolicySchema, type RangeValue, RangeValueSchema, type ScalarOperator, ScalarOperatorSchema, type SizeValue, SizeValueSchema, type Statement, StatementSchema, type UrlOperator, UrlOperatorSchema };
411
+ export { type ArrayOperator, ArrayOperatorSchema, type CanonicalHash, type Condition, ConditionSchema, type ConditionTrace, type ConditionValue, ConditionValueSchema, type Effect, type EvaluatePolicyOptions, type JsonValue, type OpenApiSchema, type Operator, OperatorSchema, type OtherOperator, OtherOperatorSchema, type Override, type OverrideTrace, type ParsedUrl, type Policy, type PolicyActionRequest, type PolicyHttpRequest, type PolicyReason, type PolicyRequest, type PolicyResult, PolicySchema, type RangeValue, RangeValueSchema, type ScalarOperator, ScalarOperatorSchema, type SizeValue, SizeValueSchema, type Statement, StatementSchema, type StatementTrace, type UrlOperator, UrlOperatorSchema, VALID_EFFECTS, VALID_OPERATOR_LIST, VALID_VERSIONS, getOpenApiSchema };
package/dist/index.mjs CHANGED
@@ -1,6 +1,21 @@
1
1
  import { z } from 'zod';
2
2
 
3
3
  // src/schema.ts
4
+ var VALID_VERSIONS = [1, 2];
5
+ var VALID_EFFECTS = ["allow", "ask", "deny"];
6
+ var VALID_OPERATOR_LIST = [
7
+ "equals",
8
+ "exists",
9
+ "in",
10
+ "keys_in",
11
+ "matches",
12
+ "matches_host",
13
+ "matches_path",
14
+ "matches_url",
15
+ "range",
16
+ "size",
17
+ "values_in"
18
+ ];
4
19
  var ScalarSchema = z.union([z.string(), z.number(), z.boolean()]);
5
20
  var PathSchema = z.array(z.union([z.string(), z.number()])).min(1);
6
21
  var JsonValueSchema = z.lazy(
@@ -123,15 +138,99 @@ var OperatorSchema = z.enum([
123
138
  "size"
124
139
  ]);
125
140
  var ConditionValueSchema = JsonValueSchema;
126
- var StatementSchema = z.object({
141
+ var V1StatementSchema = z.object({
142
+ id: z.string().optional(),
143
+ effect: z.enum(["allow", "deny"]),
144
+ permissions: z.array(z.string()).min(1),
145
+ resources: z.array(z.string()).min(1),
146
+ conditions: z.array(ConditionSchema).optional()
147
+ }).strict();
148
+ var V2StatementSchema = z.object({
149
+ id: z.string().optional(),
127
150
  effect: z.enum(["allow", "ask", "deny"]),
128
151
  permissions: z.array(z.string()).min(1),
129
152
  resources: z.array(z.string()).min(1),
130
153
  conditions: z.array(ConditionSchema).optional()
131
154
  }).strict();
132
- var PolicySchema = z.object({
155
+ var StatementSchema = V2StatementSchema;
156
+ var V1PolicySchema = z.object({
157
+ version: z.literal(1),
158
+ statements: z.array(V1StatementSchema)
159
+ }).strict();
160
+ var V2PolicySchema = z.object({
133
161
  version: z.literal(2),
134
- statements: z.array(StatementSchema)
162
+ statements: z.array(V2StatementSchema)
135
163
  }).strict();
164
+ var PolicySchema = z.union([V1PolicySchema, V2PolicySchema]);
165
+ function getOpenApiSchema() {
166
+ return {
167
+ type: "object",
168
+ required: ["version", "statements"],
169
+ properties: {
170
+ version: {
171
+ type: "integer",
172
+ enum: [...VALID_VERSIONS],
173
+ description: `Policy schema version. Must be one of ${JSON.stringify([...VALID_VERSIONS])}.`
174
+ },
175
+ statements: {
176
+ type: "array",
177
+ description: "List of policy statements defining permissions.",
178
+ items: {
179
+ type: "object",
180
+ required: ["effect", "permissions", "resources"],
181
+ properties: {
182
+ id: {
183
+ type: "string",
184
+ description: "Optional identifier for debugging and provenance. Included in canonical hash."
185
+ },
186
+ effect: {
187
+ type: "string",
188
+ enum: [...VALID_EFFECTS].sort(),
189
+ description: "Whether to allow, ask, or deny the specified permissions. The 'ask' effect is only valid in version 2 policies."
190
+ },
191
+ permissions: {
192
+ type: "array",
193
+ minItems: 1,
194
+ items: { type: "string" },
195
+ description: "List of permission identifiers."
196
+ },
197
+ resources: {
198
+ type: "array",
199
+ minItems: 1,
200
+ items: { type: "string" },
201
+ description: "List of resource identifiers the statement applies to."
202
+ },
203
+ conditions: {
204
+ type: "array",
205
+ description: "Optional conditions that must be met for the statement to apply.",
206
+ items: {
207
+ type: "object",
208
+ required: ["path", "operator"],
209
+ properties: {
210
+ path: {
211
+ type: "array",
212
+ minItems: 1,
213
+ description: "Path to the value to evaluate in the request context.",
214
+ items: {
215
+ anyOf: [{ type: "string" }, { type: "integer" }]
216
+ }
217
+ },
218
+ operator: {
219
+ type: "string",
220
+ enum: [...VALID_OPERATOR_LIST],
221
+ description: "Comparison operator used to evaluate the condition."
222
+ },
223
+ value: {
224
+ description: "Value to compare against using the operator."
225
+ }
226
+ }
227
+ }
228
+ }
229
+ }
230
+ }
231
+ }
232
+ }
233
+ };
234
+ }
136
235
 
137
- export { ArrayOperatorSchema, ConditionSchema, ConditionValueSchema, OperatorSchema, OtherOperatorSchema, PolicySchema, RangeValueSchema, ScalarOperatorSchema, SizeValueSchema, StatementSchema, UrlOperatorSchema };
236
+ export { ArrayOperatorSchema, ConditionSchema, ConditionValueSchema, OperatorSchema, OtherOperatorSchema, PolicySchema, RangeValueSchema, ScalarOperatorSchema, SizeValueSchema, StatementSchema, UrlOperatorSchema, VALID_EFFECTS, VALID_OPERATOR_LIST, VALID_VERSIONS, getOpenApiSchema };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@zapier/policy-schema",
3
- "version": "0.7.0",
3
+ "version": "0.8.0",
4
4
  "description": "Zod schemas and TypeScript types for the Zapier policy engine",
5
5
  "type": "module",
6
6
  "main": "dist/index.cjs",