convex-verify 1.0.5 → 1.2.2

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.
Files changed (46) hide show
  1. package/README.md +271 -235
  2. package/dist/core/index.d.mts +14 -85
  3. package/dist/core/index.d.ts +14 -85
  4. package/dist/core/index.js +520 -83
  5. package/dist/core/index.js.map +1 -1
  6. package/dist/core/index.mjs +516 -80
  7. package/dist/core/index.mjs.map +1 -1
  8. package/dist/index.d.mts +9 -6
  9. package/dist/index.d.ts +9 -6
  10. package/dist/index.js +386 -233
  11. package/dist/index.js.map +1 -1
  12. package/dist/index.mjs +383 -226
  13. package/dist/index.mjs.map +1 -1
  14. package/dist/types-B8ZkLuJ2.d.mts +141 -0
  15. package/dist/types-B8ZkLuJ2.d.ts +141 -0
  16. package/dist/utils/index.d.mts +3 -2
  17. package/dist/utils/index.d.ts +3 -2
  18. package/dist/utils/index.js +1 -1
  19. package/dist/utils/index.js.map +1 -1
  20. package/dist/utils/index.mjs +1 -1
  21. package/dist/utils/index.mjs.map +1 -1
  22. package/dist/verifyConfig-CTrtqMr_.d.ts +94 -0
  23. package/dist/verifyConfig-Kn3Ikj00.d.mts +94 -0
  24. package/package.json +5 -22
  25. package/dist/configs/index.d.mts +0 -51
  26. package/dist/configs/index.d.ts +0 -51
  27. package/dist/configs/index.js +0 -38
  28. package/dist/configs/index.js.map +0 -1
  29. package/dist/configs/index.mjs +0 -11
  30. package/dist/configs/index.mjs.map +0 -1
  31. package/dist/plugin-BjJ7yjrc.d.ts +0 -141
  32. package/dist/plugin-mHMV2-SG.d.mts +0 -141
  33. package/dist/plugins/index.d.mts +0 -85
  34. package/dist/plugins/index.d.ts +0 -85
  35. package/dist/plugins/index.js +0 -312
  36. package/dist/plugins/index.js.map +0 -1
  37. package/dist/plugins/index.mjs +0 -284
  38. package/dist/plugins/index.mjs.map +0 -1
  39. package/dist/transforms/index.d.mts +0 -38
  40. package/dist/transforms/index.d.ts +0 -38
  41. package/dist/transforms/index.js +0 -46
  42. package/dist/transforms/index.js.map +0 -1
  43. package/dist/transforms/index.mjs +0 -19
  44. package/dist/transforms/index.mjs.map +0 -1
  45. package/dist/types-_64SXyva.d.mts +0 -151
  46. package/dist/types-_64SXyva.d.ts +0 -151
@@ -1,141 +0,0 @@
1
- import { GenericMutationCtx, SchemaDefinition, GenericSchema } from 'convex/server';
2
- import { GenericId } from 'convex/values';
3
- import { a as OnFailCallback } from './types-_64SXyva.js';
4
-
5
- /**
6
- * Context passed to validate plugin functions.
7
- *
8
- * Provides access to:
9
- * - `ctx` - Full Convex mutation context (includes `ctx.db` for queries)
10
- * - `tableName` - The table being operated on
11
- * - `operation` - 'insert' or 'patch'
12
- * - `patchId` - Document ID (only for patch operations)
13
- * - `onFail` - Callback to report validation failures before throwing
14
- * - `schema` - Optional schema reference (if provided by verifyConfig)
15
- */
16
- type ValidateContext<TN extends string = string> = {
17
- /** Full Convex mutation context - use ctx.db for database queries */
18
- ctx: Omit<GenericMutationCtx<any>, never>;
19
- /** Table name being operated on */
20
- tableName: TN;
21
- /** Operation type: 'insert' or 'patch' */
22
- operation: 'insert' | 'patch';
23
- /** Document ID (only available for patch operations) */
24
- patchId?: GenericId<any>;
25
- /** Callback for validation failures - call before throwing to provide details */
26
- onFail?: OnFailCallback<any>;
27
- /** Schema reference (if provided to verifyConfig) */
28
- schema?: SchemaDefinition<GenericSchema, boolean>;
29
- };
30
- /**
31
- * A validate plugin that can check data during insert/patch operations.
32
- *
33
- * Validate plugins:
34
- * - Run AFTER transform plugins (like defaultValues)
35
- * - Can be async (use await for API calls, db queries, etc.)
36
- * - Can throw errors to prevent the operation
37
- * - Should return the data unchanged (validation only, no transformation)
38
- * - Do NOT affect the TypeScript types of the input data
39
- *
40
- * @example
41
- * ```ts
42
- * // Simple sync plugin
43
- * const requiredFields = createValidatePlugin(
44
- * 'requiredFields',
45
- * { fields: ['title', 'content'] },
46
- * {
47
- * insert: (context, data) => {
48
- * for (const field of config.fields) {
49
- * if (!data[field]) {
50
- * throw new ConvexError({ message: `Missing required field: ${field}` });
51
- * }
52
- * }
53
- * return data;
54
- * },
55
- * }
56
- * );
57
- *
58
- * // Async plugin with database query
59
- * const checkOwnership = createValidatePlugin(
60
- * 'checkOwnership',
61
- * {},
62
- * {
63
- * patch: async (context, data) => {
64
- * const existing = await context.ctx.db.get(context.patchId);
65
- * if (existing?.ownerId !== getCurrentUserId()) {
66
- * throw new ConvexError({ message: 'Not authorized' });
67
- * }
68
- * return data;
69
- * },
70
- * }
71
- * );
72
- * ```
73
- */
74
- interface ValidatePlugin<Type extends string = string, Config = unknown> {
75
- /** Unique identifier for this plugin */
76
- readonly _type: Type;
77
- /** Plugin configuration */
78
- readonly config: Config;
79
- /** Verify functions for insert and/or patch operations */
80
- verify: {
81
- /**
82
- * Validate data for insert operations.
83
- * Can be sync or async.
84
- *
85
- * @param context - Plugin context with ctx, tableName, schema, etc.
86
- * @param data - The data to validate (after transforms applied)
87
- * @returns The data unchanged (or Promise resolving to data)
88
- * @throws ConvexError if validation fails
89
- */
90
- insert?: (context: ValidateContext, data: any) => Promise<any> | any;
91
- /**
92
- * Validate data for patch operations.
93
- * Can be sync or async.
94
- *
95
- * @param context - Plugin context with ctx, tableName, patchId, schema, etc.
96
- * @param data - The partial data to validate
97
- * @returns The data unchanged (or Promise resolving to data)
98
- * @throws ConvexError if validation fails
99
- */
100
- patch?: (context: ValidateContext, data: any) => Promise<any> | any;
101
- };
102
- }
103
- /**
104
- * Type guard to check if something is a ValidatePlugin
105
- */
106
- declare function isValidatePlugin(obj: unknown): obj is ValidatePlugin;
107
- /**
108
- * A collection of validate plugins
109
- */
110
- type ValidatePluginRecord = Record<string, ValidatePlugin>;
111
- /**
112
- * Run all validate plugins for an operation.
113
- * Plugins are run in order and each receives the output of the previous.
114
- * All plugin verify functions are awaited (supports async plugins).
115
- */
116
- declare function runValidatePlugins(plugins: ValidatePlugin[], context: ValidateContext, data: any): Promise<any>;
117
- /**
118
- * Helper to create a validate plugin with proper typing.
119
- *
120
- * @param type - Unique identifier for this plugin type
121
- * @param config - Plugin configuration data
122
- * @param verify - Object with insert and/or patch verify functions
123
- * @returns A ValidatePlugin instance
124
- *
125
- * @example
126
- * ```ts
127
- * const myPlugin = createValidatePlugin(
128
- * 'myPlugin',
129
- * { maxLength: 100 },
130
- * {
131
- * insert: async (context, data) => {
132
- * // Validation logic here
133
- * return data;
134
- * },
135
- * }
136
- * );
137
- * ```
138
- */
139
- declare function createValidatePlugin<Type extends string, Config>(type: Type, config: Config, verify: ValidatePlugin<Type, Config>['verify']): ValidatePlugin<Type, Config>;
140
-
141
- export { type ValidateContext as V, type ValidatePlugin as a, type ValidatePluginRecord as b, createValidatePlugin as c, isValidatePlugin as i, runValidatePlugins as r };
@@ -1,141 +0,0 @@
1
- import { GenericMutationCtx, SchemaDefinition, GenericSchema } from 'convex/server';
2
- import { GenericId } from 'convex/values';
3
- import { a as OnFailCallback } from './types-_64SXyva.mjs';
4
-
5
- /**
6
- * Context passed to validate plugin functions.
7
- *
8
- * Provides access to:
9
- * - `ctx` - Full Convex mutation context (includes `ctx.db` for queries)
10
- * - `tableName` - The table being operated on
11
- * - `operation` - 'insert' or 'patch'
12
- * - `patchId` - Document ID (only for patch operations)
13
- * - `onFail` - Callback to report validation failures before throwing
14
- * - `schema` - Optional schema reference (if provided by verifyConfig)
15
- */
16
- type ValidateContext<TN extends string = string> = {
17
- /** Full Convex mutation context - use ctx.db for database queries */
18
- ctx: Omit<GenericMutationCtx<any>, never>;
19
- /** Table name being operated on */
20
- tableName: TN;
21
- /** Operation type: 'insert' or 'patch' */
22
- operation: 'insert' | 'patch';
23
- /** Document ID (only available for patch operations) */
24
- patchId?: GenericId<any>;
25
- /** Callback for validation failures - call before throwing to provide details */
26
- onFail?: OnFailCallback<any>;
27
- /** Schema reference (if provided to verifyConfig) */
28
- schema?: SchemaDefinition<GenericSchema, boolean>;
29
- };
30
- /**
31
- * A validate plugin that can check data during insert/patch operations.
32
- *
33
- * Validate plugins:
34
- * - Run AFTER transform plugins (like defaultValues)
35
- * - Can be async (use await for API calls, db queries, etc.)
36
- * - Can throw errors to prevent the operation
37
- * - Should return the data unchanged (validation only, no transformation)
38
- * - Do NOT affect the TypeScript types of the input data
39
- *
40
- * @example
41
- * ```ts
42
- * // Simple sync plugin
43
- * const requiredFields = createValidatePlugin(
44
- * 'requiredFields',
45
- * { fields: ['title', 'content'] },
46
- * {
47
- * insert: (context, data) => {
48
- * for (const field of config.fields) {
49
- * if (!data[field]) {
50
- * throw new ConvexError({ message: `Missing required field: ${field}` });
51
- * }
52
- * }
53
- * return data;
54
- * },
55
- * }
56
- * );
57
- *
58
- * // Async plugin with database query
59
- * const checkOwnership = createValidatePlugin(
60
- * 'checkOwnership',
61
- * {},
62
- * {
63
- * patch: async (context, data) => {
64
- * const existing = await context.ctx.db.get(context.patchId);
65
- * if (existing?.ownerId !== getCurrentUserId()) {
66
- * throw new ConvexError({ message: 'Not authorized' });
67
- * }
68
- * return data;
69
- * },
70
- * }
71
- * );
72
- * ```
73
- */
74
- interface ValidatePlugin<Type extends string = string, Config = unknown> {
75
- /** Unique identifier for this plugin */
76
- readonly _type: Type;
77
- /** Plugin configuration */
78
- readonly config: Config;
79
- /** Verify functions for insert and/or patch operations */
80
- verify: {
81
- /**
82
- * Validate data for insert operations.
83
- * Can be sync or async.
84
- *
85
- * @param context - Plugin context with ctx, tableName, schema, etc.
86
- * @param data - The data to validate (after transforms applied)
87
- * @returns The data unchanged (or Promise resolving to data)
88
- * @throws ConvexError if validation fails
89
- */
90
- insert?: (context: ValidateContext, data: any) => Promise<any> | any;
91
- /**
92
- * Validate data for patch operations.
93
- * Can be sync or async.
94
- *
95
- * @param context - Plugin context with ctx, tableName, patchId, schema, etc.
96
- * @param data - The partial data to validate
97
- * @returns The data unchanged (or Promise resolving to data)
98
- * @throws ConvexError if validation fails
99
- */
100
- patch?: (context: ValidateContext, data: any) => Promise<any> | any;
101
- };
102
- }
103
- /**
104
- * Type guard to check if something is a ValidatePlugin
105
- */
106
- declare function isValidatePlugin(obj: unknown): obj is ValidatePlugin;
107
- /**
108
- * A collection of validate plugins
109
- */
110
- type ValidatePluginRecord = Record<string, ValidatePlugin>;
111
- /**
112
- * Run all validate plugins for an operation.
113
- * Plugins are run in order and each receives the output of the previous.
114
- * All plugin verify functions are awaited (supports async plugins).
115
- */
116
- declare function runValidatePlugins(plugins: ValidatePlugin[], context: ValidateContext, data: any): Promise<any>;
117
- /**
118
- * Helper to create a validate plugin with proper typing.
119
- *
120
- * @param type - Unique identifier for this plugin type
121
- * @param config - Plugin configuration data
122
- * @param verify - Object with insert and/or patch verify functions
123
- * @returns A ValidatePlugin instance
124
- *
125
- * @example
126
- * ```ts
127
- * const myPlugin = createValidatePlugin(
128
- * 'myPlugin',
129
- * { maxLength: 100 },
130
- * {
131
- * insert: async (context, data) => {
132
- * // Validation logic here
133
- * return data;
134
- * },
135
- * }
136
- * );
137
- * ```
138
- */
139
- declare function createValidatePlugin<Type extends string, Config>(type: Type, config: Config, verify: ValidatePlugin<Type, Config>['verify']): ValidatePlugin<Type, Config>;
140
-
141
- export { type ValidateContext as V, type ValidatePlugin as a, type ValidatePluginRecord as b, createValidatePlugin as c, isValidatePlugin as i, runValidatePlugins as r };
@@ -1,85 +0,0 @@
1
- import { SchemaDefinition, GenericSchema, DataModelFromSchemaDefinition } from 'convex/server';
2
- import { a as ValidatePlugin } from '../plugin-mHMV2-SG.mjs';
3
- import { U as UniqueRowConfigData, g as UniqueColumnConfigData } from '../types-_64SXyva.mjs';
4
- export { h as UniqueColumnConfigEntry, i as UniqueColumnConfigOptions, e as UniqueRowConfigEntry, f as UniqueRowConfigOptions } from '../types-_64SXyva.mjs';
5
- import 'convex/values';
6
-
7
- /**
8
- * Creates a validate plugin that enforces row uniqueness based on database indexes.
9
- *
10
- * This plugin checks that the combination of column values defined in your indexes
11
- * doesn't already exist in the database before allowing insert/patch operations.
12
- *
13
- * @param schema - Your Convex schema definition
14
- * @param config - Object mapping table names to arrays of index configs
15
- * @returns A ValidatePlugin for use with verifyConfig
16
- *
17
- * @example
18
- * ```ts
19
- * // Simple shorthand - just index names
20
- * const uniqueRow = uniqueRowConfig(schema, {
21
- * posts: ['by_slug'],
22
- * users: ['by_email', 'by_username'],
23
- * });
24
- *
25
- * // With options
26
- * const uniqueRow = uniqueRowConfig(schema, {
27
- * posts: [
28
- * { index: 'by_author_slug', identifiers: ['_id', 'authorId'] },
29
- * ],
30
- * });
31
- *
32
- * // Use with verifyConfig
33
- * const { insert, patch } = verifyConfig(schema, {
34
- * plugins: [uniqueRow],
35
- * });
36
- * ```
37
- */
38
- declare const uniqueRowConfig: <S extends SchemaDefinition<GenericSchema, boolean>, DataModel extends DataModelFromSchemaDefinition<S>, const C extends UniqueRowConfigData<DataModel>>(schema: S, config: C) => ValidatePlugin<"uniqueRow", C>;
39
-
40
- /**
41
- * Creates a validate plugin that enforces column uniqueness using single-column indexes.
42
- *
43
- * This is useful when you have a column that must be unique across all rows,
44
- * like usernames or email addresses.
45
- *
46
- * The column name is derived from the index name by removing the 'by_' prefix.
47
- * For example, 'by_username' checks the 'username' column.
48
- *
49
- * @param schema - Your Convex schema definition
50
- * @param config - Object mapping table names to arrays of index configs
51
- * @returns A ValidatePlugin for use with verifyConfig
52
- *
53
- * @example
54
- * ```ts
55
- * // Shorthand: just pass index names as strings
56
- * const uniqueColumn = uniqueColumnConfig(schema, {
57
- * users: ['by_username', 'by_email'],
58
- * organizations: ['by_slug'],
59
- * });
60
- *
61
- * // Full config: pass objects with options
62
- * const uniqueColumn = uniqueColumnConfig(schema, {
63
- * users: [
64
- * { index: 'by_username', identifiers: ['_id', 'userId'] },
65
- * { index: 'by_email', identifiers: ['_id'] },
66
- * ],
67
- * });
68
- *
69
- * // Mix and match
70
- * const uniqueColumn = uniqueColumnConfig(schema, {
71
- * users: [
72
- * 'by_username', // shorthand
73
- * { index: 'by_email', identifiers: ['_id', 'clerkId'] }, // full config
74
- * ],
75
- * });
76
- *
77
- * // Use with verifyConfig
78
- * const { insert, patch } = verifyConfig(schema, {
79
- * plugins: [uniqueColumn],
80
- * });
81
- * ```
82
- */
83
- declare const uniqueColumnConfig: <S extends SchemaDefinition<GenericSchema, boolean>, DataModel extends DataModelFromSchemaDefinition<S>, const C extends UniqueColumnConfigData<DataModel>>(_schema: S, config: C) => ValidatePlugin<"uniqueColumn", C>;
84
-
85
- export { UniqueColumnConfigData, UniqueRowConfigData, uniqueColumnConfig, uniqueRowConfig };
@@ -1,85 +0,0 @@
1
- import { SchemaDefinition, GenericSchema, DataModelFromSchemaDefinition } from 'convex/server';
2
- import { a as ValidatePlugin } from '../plugin-BjJ7yjrc.js';
3
- import { U as UniqueRowConfigData, g as UniqueColumnConfigData } from '../types-_64SXyva.js';
4
- export { h as UniqueColumnConfigEntry, i as UniqueColumnConfigOptions, e as UniqueRowConfigEntry, f as UniqueRowConfigOptions } from '../types-_64SXyva.js';
5
- import 'convex/values';
6
-
7
- /**
8
- * Creates a validate plugin that enforces row uniqueness based on database indexes.
9
- *
10
- * This plugin checks that the combination of column values defined in your indexes
11
- * doesn't already exist in the database before allowing insert/patch operations.
12
- *
13
- * @param schema - Your Convex schema definition
14
- * @param config - Object mapping table names to arrays of index configs
15
- * @returns A ValidatePlugin for use with verifyConfig
16
- *
17
- * @example
18
- * ```ts
19
- * // Simple shorthand - just index names
20
- * const uniqueRow = uniqueRowConfig(schema, {
21
- * posts: ['by_slug'],
22
- * users: ['by_email', 'by_username'],
23
- * });
24
- *
25
- * // With options
26
- * const uniqueRow = uniqueRowConfig(schema, {
27
- * posts: [
28
- * { index: 'by_author_slug', identifiers: ['_id', 'authorId'] },
29
- * ],
30
- * });
31
- *
32
- * // Use with verifyConfig
33
- * const { insert, patch } = verifyConfig(schema, {
34
- * plugins: [uniqueRow],
35
- * });
36
- * ```
37
- */
38
- declare const uniqueRowConfig: <S extends SchemaDefinition<GenericSchema, boolean>, DataModel extends DataModelFromSchemaDefinition<S>, const C extends UniqueRowConfigData<DataModel>>(schema: S, config: C) => ValidatePlugin<"uniqueRow", C>;
39
-
40
- /**
41
- * Creates a validate plugin that enforces column uniqueness using single-column indexes.
42
- *
43
- * This is useful when you have a column that must be unique across all rows,
44
- * like usernames or email addresses.
45
- *
46
- * The column name is derived from the index name by removing the 'by_' prefix.
47
- * For example, 'by_username' checks the 'username' column.
48
- *
49
- * @param schema - Your Convex schema definition
50
- * @param config - Object mapping table names to arrays of index configs
51
- * @returns A ValidatePlugin for use with verifyConfig
52
- *
53
- * @example
54
- * ```ts
55
- * // Shorthand: just pass index names as strings
56
- * const uniqueColumn = uniqueColumnConfig(schema, {
57
- * users: ['by_username', 'by_email'],
58
- * organizations: ['by_slug'],
59
- * });
60
- *
61
- * // Full config: pass objects with options
62
- * const uniqueColumn = uniqueColumnConfig(schema, {
63
- * users: [
64
- * { index: 'by_username', identifiers: ['_id', 'userId'] },
65
- * { index: 'by_email', identifiers: ['_id'] },
66
- * ],
67
- * });
68
- *
69
- * // Mix and match
70
- * const uniqueColumn = uniqueColumnConfig(schema, {
71
- * users: [
72
- * 'by_username', // shorthand
73
- * { index: 'by_email', identifiers: ['_id', 'clerkId'] }, // full config
74
- * ],
75
- * });
76
- *
77
- * // Use with verifyConfig
78
- * const { insert, patch } = verifyConfig(schema, {
79
- * plugins: [uniqueColumn],
80
- * });
81
- * ```
82
- */
83
- declare const uniqueColumnConfig: <S extends SchemaDefinition<GenericSchema, boolean>, DataModel extends DataModelFromSchemaDefinition<S>, const C extends UniqueColumnConfigData<DataModel>>(_schema: S, config: C) => ValidatePlugin<"uniqueColumn", C>;
84
-
85
- export { UniqueColumnConfigData, UniqueRowConfigData, uniqueColumnConfig, uniqueRowConfig };