@nhtio/validation 1.20251028.0 → 1.20251029.1

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nhtio/validation",
3
- "version": "1.20251028.0",
3
+ "version": "1.20251029.1",
4
4
  "description": "A powerful schema description language and data validator",
5
5
  "keywords": [],
6
6
  "author": "Jak Giveon <jak@nht.io>",
@@ -21,7 +21,8 @@
21
21
  },
22
22
  "onlyBuiltDependencies": [
23
23
  "es5-ext",
24
- "esbuild"
24
+ "esbuild",
25
+ "sqlite3"
25
26
  ]
26
27
  },
27
28
  "packageManager": "pnpm@10.8.0+sha512.0e82714d1b5b43c74610193cb20734897c1d00de89d0e18420aebc5977fa13d780a9cb05734624e81ebd81cc876cd464794850641c48b9544326b5622ca29971",
@@ -1,4 +1,6 @@
1
+ import type { Knex } from 'knex';
1
2
  import type { SchemaTypeDefinititionMiddlewareFn } from '../core/root';
3
+ import type { KnexSchemaConnection } from '../schemas';
2
4
  export declare const knexMessages: {
3
5
  'knex.unique': string;
4
6
  'knex.exists': string;
@@ -7,4 +9,8 @@ export declare const knexMessages: {
7
9
  'knex.invalidColumn': string;
8
10
  'knex.internal': string;
9
11
  };
12
+ export declare const resolveQueryBuilder: (connection: KnexSchemaConnection, table: string) => {
13
+ client: Knex;
14
+ query: Knex.QueryBuilder;
15
+ };
10
16
  export declare const knex: SchemaTypeDefinititionMiddlewareFn;
@@ -1,4 +1,71 @@
1
1
  import type { Schema } from './schemas';
2
+ /**
3
+ * Options for encoding and decoding validation schemas.
4
+ *
5
+ * These options control what gets serialized when encoding schemas and how
6
+ * they're reconstructed during decoding. The defaults prioritize security
7
+ * and are designed for the most common use case: sharing validation rules
8
+ * between frontend and backend environments.
9
+ */
10
+ export type EncoderOptions = {
11
+ /**
12
+ * Include database connections and database validation rules in the encoded schema.
13
+ *
14
+ * When `false` (default):
15
+ * - Database connections (`.knex()`, `.db()`) are stripped from the encoded schema
16
+ * - Database validation rules (`.uniqueInDb()`, `.existsInDb()`) are converted to no-ops
17
+ * - Filter functions in database rules are replaced with empty functions
18
+ * - This prevents credential leakage and reduces payload size
19
+ *
20
+ * When `true`:
21
+ * - Database connection configurations are serialized (if they're plain objects)
22
+ * - Database validation rules and their filter functions are preserved
23
+ * - **Security warning**: Only use this in trusted backend-to-backend communication
24
+ * - Encoded schemas will require re-attaching database connections after decoding
25
+ *
26
+ * **Why this defaults to `false`:**
27
+ * 1. **Security**: Prevents accidental exposure of database credentials in client-side code
28
+ * 2. **Use case alignment**: Frontend validation typically doesn't need database rules
29
+ * 3. **Credential safety**: Database connections often contain sensitive connection strings
30
+ * 4. **Function security**: Custom filter functions may contain business logic or credentials
31
+ *
32
+ * @default false
33
+ *
34
+ * @example Frontend/Backend Schema Sharing (default)
35
+ * ```typescript
36
+ * // Backend
37
+ * const schema = joi.object({
38
+ * email: joi.string().email().uniqueInDb('users', 'email')
39
+ * }).knex(db)
40
+ *
41
+ * const encoded = encode(schema) // withDatabase defaults to false
42
+ * // Database rules are stripped, safe to send to frontend
43
+ *
44
+ * // Frontend receives and decodes
45
+ * const frontendSchema = decode(encoded)
46
+ * // Only gets email format validation, no database checks
47
+ * ```
48
+ *
49
+ * @example Backend-to-Backend Schema Transfer
50
+ * ```typescript
51
+ * // Service A
52
+ * const schema = joi.object({
53
+ * username: joi.string().uniqueInDb('users', 'username', {
54
+ * filter: async (query) => query.where('tenant_id', tenantId)
55
+ * })
56
+ * }).knex({ client: 'pg', connection: { host: 'localhost', ... } })
57
+ *
58
+ * const encoded = encode(schema, { withDatabase: true })
59
+ * // Sends to trusted Service B
60
+ *
61
+ * // Service B
62
+ * const decoded = decode(encoded, { withDatabase: true })
63
+ * const rehydrated = decoded.knex(serviceB_db_connection)
64
+ * // Now has full database validation capabilities
65
+ * ```
66
+ */
67
+ withDatabase?: boolean;
68
+ };
2
69
  /**
3
70
  * Encodes a validation schema into a compressed, base64-encoded string for transport between environments.
4
71
  *
@@ -46,7 +113,7 @@ import type { Schema } from './schemas';
46
113
  *
47
114
  * @see {@link decode} For decoding schemas from encoded strings
48
115
  */
49
- export declare const encode: (schema: Schema) => string;
116
+ export declare const encode: (schema: Schema, options?: EncoderOptions) => string;
50
117
  /**
51
118
  * Decodes a base64-encoded schema string back into a usable validation schema.
52
119
  *
@@ -63,6 +130,7 @@ export declare const encode: (schema: Schema) => string;
63
130
  * - Schema reconstruction using Joi's build method
64
131
  *
65
132
  * @param base64 - The base64-encoded schema string
133
+ * @param options - Decoding options
66
134
  * @returns A reconstructed validation schema ready for validation
67
135
  * @throws {TypeError} When the encoded string is not a valid schema
68
136
  * @throws {TypeError} When the schema version is invalid or incompatible
@@ -101,6 +169,18 @@ export declare const encode: (schema: Schema) => string;
101
169
  * const schema = decode(oldSchema) // Works - backward compatible
102
170
  * ```
103
171
  *
172
+ * @security
173
+ * **IMPORTANT: Only decode schemas from trusted sources.**
174
+ *
175
+ * Decoded schemas may contain serialized functions that are evaluated using the
176
+ * Function constructor. Never decode schemas from:
177
+ * - User input or user-controlled data
178
+ * - Untrusted third-party APIs
179
+ * - Any source you do not control
180
+ *
181
+ * If the schema source is not under your direct control, validate its integrity
182
+ * using cryptographic signatures or checksums before decoding.
183
+ *
104
184
  * @see {@link encode} For encoding schemas into transportable strings
105
185
  */
106
- export declare const decode: (base64: string) => Schema;
186
+ export declare const decode: (base64: string, options?: EncoderOptions) => Schema;