@nhtio/validation 0.1.0-master-5ddd6b01

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,157 @@
1
+ import type { AnySchema as JoiAnySchema, StringSchema as JoiStringSchema, BinarySchema as JoiBinarySchema, NumberSchema as JoiNumberSchema, BooleanSchema as JoiBooleanSchema, ObjectSchema as JoiObjectSchema, ArraySchema as JoiArraySchema, DateSchema as JoiDateSchema, Reference, BasicType, CustomHelpers } from 'joi';
2
+ /**
3
+ * Base schema type for all validation schemas.
4
+ *
5
+ * This interface provides the foundation for all validation schemas with
6
+ * comprehensive validation methods and options.
7
+ *
8
+ * @public
9
+ */
10
+ export interface AnySchema<TSchema = any> extends Omit<JoiAnySchema<TSchema>, 'default'> {
11
+ /**
12
+ * Sets a default value if the original value is `undefined`.
13
+ *
14
+ * @param value - The default value. Can be:
15
+ * - A literal value (string, number, object, etc.)
16
+ * - A reference to another schema value
17
+ * - A function that returns the default value with signature `(parent, helpers) => value`
18
+ * - `parent` - A clone of the object containing the value being validated
19
+ * - `helpers` - Validation helper functions for custom validation logic
20
+ *
21
+ * @example
22
+ * ```typescript
23
+ * const schema = joi.string().default('hello world')
24
+ * const schemaWithFunction = joi.number().default((parent, helpers) => parent.someOtherField * 2)
25
+ * ```
26
+ */
27
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
28
+ }
29
+ /**
30
+ * Schema type for string validation.
31
+ *
32
+ * @public
33
+ */
34
+ export interface StringSchema<TSchema = string> extends Omit<JoiStringSchema<TSchema>, 'default'> {
35
+ /**
36
+ * Sets a default value if the original value is `undefined`.
37
+ *
38
+ * @param value - The default value. Can be:
39
+ * - A literal string value
40
+ * - A reference to another schema value
41
+ * - A function that returns the default value with signature `(parent, helpers) => value`
42
+ * - `parent` - A clone of the object containing the value being validated
43
+ * - `helpers` - Validation helper functions for custom validation logic
44
+ */
45
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
46
+ }
47
+ /**
48
+ * Schema type for binary data validation.
49
+ *
50
+ * @public
51
+ */
52
+ export interface BinarySchema<TSchema = Buffer> extends Omit<JoiBinarySchema<TSchema>, 'default'> {
53
+ /**
54
+ * Sets a default value if the original value is `undefined`.
55
+ *
56
+ * @param value - The default value. Can be:
57
+ * - A literal binary value (Buffer, Uint8Array, etc.)
58
+ * - A reference to another schema value
59
+ * - A function that returns the default value with signature `(parent, helpers) => value`
60
+ * - `parent` - A clone of the object containing the value being validated
61
+ * - `helpers` - Validation helper functions for custom validation logic
62
+ */
63
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
64
+ }
65
+ /**
66
+ * Schema type for number validation.
67
+ *
68
+ * @public
69
+ */
70
+ export interface NumberSchema<TSchema = number> extends Omit<JoiNumberSchema<TSchema>, 'default'> {
71
+ /**
72
+ * Sets a default value if the original value is `undefined`.
73
+ *
74
+ * @param value - The default value. Can be:
75
+ * - A literal number value
76
+ * - A reference to another schema value
77
+ * - A function that returns the default value with signature `(parent, helpers) => value`
78
+ * - `parent` - A clone of the object containing the value being validated
79
+ * - `helpers` - Validation helper functions for custom validation logic
80
+ */
81
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
82
+ }
83
+ /**
84
+ * Schema type for boolean validation.
85
+ *
86
+ * @public
87
+ */
88
+ export interface BooleanSchema<TSchema = boolean> extends Omit<JoiBooleanSchema<TSchema>, 'default'> {
89
+ /**
90
+ * Sets a default value if the original value is `undefined`.
91
+ *
92
+ * @param value - The default value. Can be:
93
+ * - A literal boolean value
94
+ * - A reference to another schema value
95
+ * - A function that returns the default value with signature `(parent, helpers) => value`
96
+ * - `parent` - A clone of the object containing the value being validated
97
+ * - `helpers` - Validation helper functions for custom validation logic
98
+ */
99
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
100
+ }
101
+ /**
102
+ * Schema type for object validation.
103
+ *
104
+ * @public
105
+ */
106
+ export interface ObjectSchema<TSchema = any> extends Omit<JoiObjectSchema<TSchema>, 'default'> {
107
+ /**
108
+ * Sets a default value if the original value is `undefined`.
109
+ *
110
+ * @param value - The default value. Can be:
111
+ * - A literal object value
112
+ * - A reference to another schema value
113
+ * - A function that returns the default value with signature `(parent, helpers) => value`
114
+ * - `parent` - A clone of the object containing the value being validated
115
+ * - `helpers` - Validation helper functions for custom validation logic
116
+ *
117
+ * When called without any value on an object schema type, a default value will be
118
+ * automatically generated based on the default values of the object keys.
119
+ */
120
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
121
+ }
122
+ /**
123
+ * Schema type for array validation.
124
+ *
125
+ * @public
126
+ */
127
+ export interface ArraySchema<TSchema = any[]> extends Omit<JoiArraySchema<TSchema>, 'default'> {
128
+ /**
129
+ * Sets a default value if the original value is `undefined`.
130
+ *
131
+ * @param value - The default value. Can be:
132
+ * - A literal array value
133
+ * - A reference to another schema value
134
+ * - A function that returns the default value with signature `(parent, helpers) => value`
135
+ * - `parent` - A clone of the object containing the value being validated
136
+ * - `helpers` - Validation helper functions for custom validation logic
137
+ */
138
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
139
+ }
140
+ /**
141
+ * Schema type for date validation.
142
+ *
143
+ * @public
144
+ */
145
+ export interface DateSchema<TSchema = Date> extends Omit<JoiDateSchema<TSchema>, 'default'> {
146
+ /**
147
+ * Sets a default value if the original value is `undefined`.
148
+ *
149
+ * @param value - The default value. Can be:
150
+ * - A literal date value
151
+ * - A reference to another schema value
152
+ * - A function that returns the default value with signature `(parent, helpers) => value`
153
+ * - `parent` - A clone of the object containing the value being validated
154
+ * - `helpers` - Validation helper functions for custom validation logic
155
+ */
156
+ default(value?: Reference | BasicType | ((parent: any, helpers: CustomHelpers) => Reference | BasicType)): this;
157
+ }
@@ -0,0 +1,69 @@
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;
@@ -0,0 +1,106 @@
1
+ import type { AnySchema } from './schemas';
2
+ /**
3
+ * Encodes a validation schema into a compressed, base64-encoded string for transport between environments.
4
+ *
5
+ * This function serializes a validation schema description along with version information,
6
+ * compresses it using zlib, and encodes it as a base64 string. This enables efficient
7
+ * transfer of validation schemas between frontend and backend environments while
8
+ * maintaining version compatibility.
9
+ *
10
+ * The encoded string includes:
11
+ * - Current library version for compatibility checking
12
+ * - Complete schema description from `schema.describe()`
13
+ * - Zlib compression for minimal payload size
14
+ * - Base64 encoding for web-safe transport
15
+ *
16
+ * @param schema - The validation schema to encode
17
+ * @returns A compressed, base64-encoded string representing the schema
18
+ *
19
+ * @example Basic Usage
20
+ * ```typescript
21
+ * import Joi from 'joi'
22
+ * import { encode } from './utils'
23
+ *
24
+ * const schema = Joi.object({
25
+ * name: Joi.string().required(),
26
+ * age: Joi.number().min(0)
27
+ * })
28
+ *
29
+ * const encoded = encode(schema)
30
+ * // Returns: "eJyrVkosLcmIz8nPS1WyUvJIzcnJT..."
31
+ * ```
32
+ *
33
+ * @example Cross-Environment Transfer
34
+ * ```typescript
35
+ * // Backend
36
+ * const validationSchema = Joi.object({
37
+ * email: Joi.string().email().required()
38
+ * })
39
+ * const encodedSchema = encode(validationSchema)
40
+ *
41
+ * // Send to frontend via API
42
+ * response.json({ schema: encodedSchema })
43
+ *
44
+ * // Frontend can then decode and use the same validation
45
+ * ```
46
+ *
47
+ * @see {@link decode} For decoding schemas from encoded strings
48
+ */
49
+ export declare const encode: (schema: AnySchema) => string;
50
+ /**
51
+ * Decodes a base64-encoded schema string back into a usable validation schema.
52
+ *
53
+ * This function reverses the encoding process, decompressing and parsing the
54
+ * encoded schema while performing version compatibility checks. It ensures
55
+ * that schemas encoded with newer versions of the library are rejected to
56
+ * prevent runtime errors from incompatible schema formats.
57
+ *
58
+ * The function handles:
59
+ * - Base64 decoding and zlib decompression
60
+ * - Version compatibility validation using semantic versioning
61
+ * - Backward compatibility with legacy encoding formats
62
+ * - Input validation and error handling
63
+ * - Schema reconstruction using Joi's build method
64
+ *
65
+ * @param base64 - The base64-encoded schema string
66
+ * @returns A reconstructed validation schema ready for validation
67
+ * @throws {TypeError} When the encoded string is not a valid schema
68
+ * @throws {TypeError} When the schema version is invalid or incompatible
69
+ *
70
+ * @example Basic Usage
71
+ * ```typescript
72
+ * import { decode } from './utils'
73
+ *
74
+ * const encodedSchema = "eJyrVkosLcmIz8nPS1WyUvJIzcnJT..."
75
+ * const schema = decode(encodedSchema)
76
+ *
77
+ * // Use the decoded schema for validation
78
+ * const result = schema.validate({ name: "John", age: 30 })
79
+ * ```
80
+ *
81
+ * @example Error Handling
82
+ * ```typescript
83
+ * try {
84
+ * const schema = decode(untrustedEncodedString)
85
+ * // Use schema safely
86
+ * } catch (error) {
87
+ * if (error instanceof TypeError) {
88
+ * console.error('Invalid or incompatible schema:', error.message)
89
+ * }
90
+ * }
91
+ * ```
92
+ *
93
+ * @example Version Compatibility
94
+ * ```typescript
95
+ * // Schema encoded with v2.1.0, current version is v2.0.0
96
+ * const futureSchema = "..." // Contains version 2.1.0
97
+ * decode(futureSchema) // Throws: Schema version 2.1.0 is not compatible
98
+ *
99
+ * // Schema encoded with v1.9.0, current version is v2.0.0
100
+ * const oldSchema = "..." // Contains version 1.9.0
101
+ * const schema = decode(oldSchema) // Works - backward compatible
102
+ * ```
103
+ *
104
+ * @see {@link encode} For encoding schemas into transportable strings
105
+ */
106
+ export declare const decode: (base64: string) => AnySchema;