@nhtio/validation 0.1.0-master-fe5983b7 → 0.1.0-master-b4207fd6

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.
@@ -1,69 +0,0 @@
1
- /**
2
- * # Table of tokens
3
- *
4
- * (The example values below come from this time `2014-08-06T13:07:04.054`
5
- * considered as a local time in America/New_York).
6
- *
7
- * Note that many tokens supported by the formatter are **not** supported by the parser.
8
- *
9
- * | Standalone token | Format token | Description | Example |
10
- * | ---------------- | ------------ | ----------------------------------------------------------------- | --------------------------- |
11
- * | S | | millisecond, no padding | `54` |
12
- * | SSS | | millisecond, padded to 3 | `054` |
13
- * | u | | fractional seconds, (5 is a half second, 54 is slightly more) | `54` |
14
- * | uu | | fractional seconds, (one or two digits) | `05` |
15
- * | uuu | | fractional seconds, (only one digit) | `5` |
16
- * | s | | second, no padding | `4` |
17
- * | ss | | second, padded to 2 padding | `04` |
18
- * | m | | minute, no padding | `7` |
19
- * | mm | | minute, padded to 2 | `07` |
20
- * | h | | hour in 12-hour time, no padding | `1` |
21
- * | hh | | hour in 12-hour time, padded to 2 | `01` |
22
- * | H | | hour in 24-hour time, no padding | `13` |
23
- * | HH | | hour in 24-hour time, padded to 2 | `13` |
24
- * | Z | | narrow offset | `+5` |
25
- * | ZZ | | short offset | `+05:00` |
26
- * | ZZZ | | techie offset | `+0500` |
27
- * | z | | IANA zone | `America/New_York` |
28
- * | a | | meridiem | `AM` |
29
- * | d | | day of the month, no padding | `6` |
30
- * | dd | | day of the month, padded to 2 | `06` |
31
- * | E | c | day of the week, as number from 1-7 (Monday is 1, Sunday is 7) | `3` |
32
- * | EEE | ccc | day of the week, as an abbreviate localized string | `Wed` |
33
- * | EEEE | cccc | day of the week, as an unabbreviated localized string | `Wednesday` |
34
- * | M | L | month as an unpadded number | `8` |
35
- * | MM | LL | month as an padded number | `08` |
36
- * | MMM | LLL | month as an abbreviated localized string | `Aug` |
37
- * | MMMM | LLLL | month as an unabbreviated localized string | `August` |
38
- * | y | | year, 1-6 digits, very literally | `2014` |
39
- * | yy | | two-digit year, interpreted as > 1960 by default (also accepts 4) | `14` |
40
- * | yyyy | | four-digit year | `2014` |
41
- * | yyyyy | | four- to six-digit years | `10340` |
42
- * | yyyyyy | | six-digit years | `010340` |
43
- * | G | | abbreviated localized era | `AD` |
44
- * | GG | | unabbreviated localized era | `Anno Domini` |
45
- * | GGGGG | | one-letter localized era | `A` |
46
- * | kk | | ISO week year, unpadded | `17` |
47
- * | kkkk | | ISO week year, padded to 4 | `2014` |
48
- * | W | | ISO week number, unpadded | `32` |
49
- * | WW | | ISO week number, padded to 2 | `32` |
50
- * | o | | ordinal (day of year), unpadded | `218` |
51
- * | ooo | | ordinal (day of year), padded to 3 | `218` |
52
- * | q | | quarter, no padding | `3` |
53
- * | D | | localized numeric date | `9/6/2014` |
54
- * | DD | | localized date with abbreviated month | `Aug 6, 2014` |
55
- * | DDD | | localized date with full month | `August 6, 2014` |
56
- * | DDDD | | localized date with full month and weekday | `Wednesday, August 6, 2014` |
57
- * | t | | localized time | `1:07 AM` |
58
- * | tt | | localized time with seconds | `1:07:04 PM` |
59
- * | T | | localized 24-hour time | `13:07` |
60
- * | TT | | localized 24-hour time with seconds | `13:07:04` |
61
- * | f | | short localized date and time | `8/6/2014, 1:07 PM` |
62
- * | ff | | less short localized date and time | `Aug 6, 2014, 1:07 PM` |
63
- * | F | | short localized date and time with seconds | `8/6/2014, 1:07:04 PM` |
64
- * | FF | | less short localized date and time with seconds | `Aug 6, 2014, 1:07:04 PM` |
65
- * | ' | | literal start/end, characters between are not tokenized | `'T'` |
66
- *
67
- * Sourced from [here](https://moment.github.io/luxon/#/parsing?id=table-of-tokens).
68
- */
69
- export type Tokens = string;
@@ -1,186 +0,0 @@
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
- };
69
- /**
70
- * Encodes a validation schema into a compressed, base64-encoded string for transport between environments.
71
- *
72
- * This function serializes a validation schema description along with version information,
73
- * compresses it using zlib, and encodes it as a base64 string. This enables efficient
74
- * transfer of validation schemas between frontend and backend environments while
75
- * maintaining version compatibility.
76
- *
77
- * The encoded string includes:
78
- * - Current library version for compatibility checking
79
- * - Complete schema description from `schema.describe()`
80
- * - Zlib compression for minimal payload size
81
- * - Base64 encoding for web-safe transport
82
- *
83
- * @param schema - The validation schema to encode
84
- * @returns A compressed, base64-encoded string representing the schema
85
- *
86
- * @example Basic Usage
87
- * ```typescript
88
- * import Joi from 'joi'
89
- * import { encode } from './utils'
90
- *
91
- * const schema = Joi.object({
92
- * name: Joi.string().required(),
93
- * age: Joi.number().min(0)
94
- * })
95
- *
96
- * const encoded = encode(schema)
97
- * // Returns: "eJyrVkosLcmIz8nPS1WyUvJIzcnJT..."
98
- * ```
99
- *
100
- * @example Cross-Environment Transfer
101
- * ```typescript
102
- * // Backend
103
- * const validationSchema = Joi.object({
104
- * email: Joi.string().email().required()
105
- * })
106
- * const encodedSchema = encode(validationSchema)
107
- *
108
- * // Send to frontend via API
109
- * response.json({ schema: encodedSchema })
110
- *
111
- * // Frontend can then decode and use the same validation
112
- * ```
113
- *
114
- * @see {@link decode} For decoding schemas from encoded strings
115
- */
116
- export declare const encode: (schema: Schema, options?: EncoderOptions) => string;
117
- /**
118
- * Decodes a base64-encoded schema string back into a usable validation schema.
119
- *
120
- * This function reverses the encoding process, decompressing and parsing the
121
- * encoded schema while performing version compatibility checks. It ensures
122
- * that schemas encoded with newer versions of the library are rejected to
123
- * prevent runtime errors from incompatible schema formats.
124
- *
125
- * The function handles:
126
- * - Base64 decoding and zlib decompression
127
- * - Version compatibility validation using semantic versioning
128
- * - Backward compatibility with legacy encoding formats
129
- * - Input validation and error handling
130
- * - Schema reconstruction using Joi's build method
131
- *
132
- * @param base64 - The base64-encoded schema string
133
- * @param options - Decoding options
134
- * @returns A reconstructed validation schema ready for validation
135
- * @throws {TypeError} When the encoded string is not a valid schema
136
- * @throws {TypeError} When the schema version is invalid or incompatible
137
- *
138
- * @example Basic Usage
139
- * ```typescript
140
- * import { decode } from './utils'
141
- *
142
- * const encodedSchema = "eJyrVkosLcmIz8nPS1WyUvJIzcnJT..."
143
- * const schema = decode(encodedSchema)
144
- *
145
- * // Use the decoded schema for validation
146
- * const result = schema.validate({ name: "John", age: 30 })
147
- * ```
148
- *
149
- * @example Error Handling
150
- * ```typescript
151
- * try {
152
- * const schema = decode(untrustedEncodedString)
153
- * // Use schema safely
154
- * } catch (error) {
155
- * if (error instanceof TypeError) {
156
- * console.error('Invalid or incompatible schema:', error.message)
157
- * }
158
- * }
159
- * ```
160
- *
161
- * @example Version Compatibility
162
- * ```typescript
163
- * // Schema encoded with v2.1.0, current version is v2.0.0
164
- * const futureSchema = "..." // Contains version 2.1.0
165
- * decode(futureSchema) // Throws: Schema version 2.1.0 is not compatible
166
- *
167
- * // Schema encoded with v1.9.0, current version is v2.0.0
168
- * const oldSchema = "..." // Contains version 1.9.0
169
- * const schema = decode(oldSchema) // Works - backward compatible
170
- * ```
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
- *
184
- * @see {@link encode} For encoding schemas into transportable strings
185
- */
186
- export declare const decode: (base64: string, options?: EncoderOptions) => Schema;