@naturalcycles/nodejs-lib 15.92.1 → 15.94.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/jwt/jwt.service.d.ts +4 -4
- package/dist/validation/ajv/from-data/generateJsonSchemaFromData.d.ts +1 -1
- package/dist/validation/ajv/getAjv.d.ts +1 -1
- package/dist/validation/ajv/index.d.ts +1 -1
- package/dist/validation/ajv/index.js +1 -1
- package/dist/validation/ajv/{ajvSchema.d.ts → jSchema.d.ts} +232 -271
- package/dist/validation/ajv/{ajvSchema.js → jSchema.js} +453 -460
- package/dist/validation/ajv/jsonSchemaBuilder.util.d.ts +1 -1
- package/dist/zip/zip.util.d.ts +3 -0
- package/dist/zip/zip.util.js +10 -0
- package/package.json +2 -2
- package/src/jwt/jwt.service.ts +8 -4
- package/src/validation/ajv/from-data/generateJsonSchemaFromData.ts +2 -2
- package/src/validation/ajv/getAjv.ts +5 -5
- package/src/validation/ajv/index.ts +1 -1
- package/src/validation/ajv/{ajvSchema.ts → jSchema.ts} +1447 -1471
- package/src/validation/ajv/jsonSchemaBuilder.util.ts +1 -1
- package/src/zip/zip.util.ts +16 -0
|
@@ -3,134 +3,20 @@ import type { Set2 } from '@naturalcycles/js-lib/object';
|
|
|
3
3
|
import type { AnyObject, BaseDBEntity, IANATimezone, Inclusiveness, IsoDate, IsoDateTime, IsoMonth, NumberEnum, StringEnum, StringMap, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
|
|
4
4
|
import type { Ajv } from 'ajv';
|
|
5
5
|
import { AjvValidationError } from './ajvValidationError.js';
|
|
6
|
-
/**
|
|
7
|
-
* On creation - compiles ajv validation function.
|
|
8
|
-
* Provides convenient methods, error reporting, etc.
|
|
9
|
-
*/
|
|
10
|
-
export declare class AjvSchema<OUT> {
|
|
11
|
-
schema: JsonSchema<OUT>;
|
|
12
|
-
private constructor();
|
|
13
|
-
/**
|
|
14
|
-
* Shortcut for AjvSchema.create(schema, { lazy: true })
|
|
15
|
-
*/
|
|
16
|
-
static createLazy<OUT>(schema: SchemaHandledByAjv<OUT>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<OUT>;
|
|
17
|
-
/**
|
|
18
|
-
* Conveniently allows to pass either JsonSchema or JsonSchemaBuilder, or existing AjvSchema.
|
|
19
|
-
* If it's already an AjvSchema - it'll just return it without any processing.
|
|
20
|
-
* If it's a Builder - will call `build` before proceeding.
|
|
21
|
-
* Otherwise - will construct AjvSchema instance ready to be used.
|
|
22
|
-
*
|
|
23
|
-
* Implementation note: JsonSchemaBuilder goes first in the union type, otherwise TypeScript fails to infer <T> type
|
|
24
|
-
* correctly for some reason.
|
|
25
|
-
*/
|
|
26
|
-
static create<OUT>(schema: SchemaHandledByAjv<OUT>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<OUT>;
|
|
27
|
-
static isJsonSchemaBuilder<OUT>(schema: unknown): schema is JsonSchemaTerminal<OUT, any>;
|
|
28
|
-
readonly cfg: AjvSchemaCfg;
|
|
29
|
-
/**
|
|
30
|
-
* It returns the original object just for convenience.
|
|
31
|
-
* Reminder: Ajv will MUTATE your object under 2 circumstances:
|
|
32
|
-
* 1. `useDefaults` option (enabled by default!), which will set missing/empty values that have `default` set in the schema.
|
|
33
|
-
* 2. `coerceTypes` (false by default).
|
|
34
|
-
*
|
|
35
|
-
* Returned object is always the same object (`===`) that was passed, so it is returned just for convenience.
|
|
36
|
-
*/
|
|
37
|
-
validate(input: unknown, opt?: AjvValidationOptions): OUT;
|
|
38
|
-
isValid(input: unknown, opt?: AjvValidationOptions): boolean;
|
|
39
|
-
getValidationResult(input: unknown, opt?: AjvValidationOptions): ValidationFunctionResult<OUT, AjvValidationError>;
|
|
40
|
-
getValidationFunction(): ValidationFunction<OUT, AjvValidationError>;
|
|
41
|
-
static isSchemaWithCachedAjvSchema<Base, OUT>(schema: Base): schema is WithCachedAjvSchema<Base, OUT>;
|
|
42
|
-
static cacheAjvSchema<Base extends AnyObject, OUT>(schema: Base, ajvSchema: AjvSchema<OUT>): WithCachedAjvSchema<Base, OUT>;
|
|
43
|
-
static requireCachedAjvSchema<Base, OUT>(schema: WithCachedAjvSchema<Base, OUT>): AjvSchema<OUT>;
|
|
44
|
-
private getAJVValidateFunction;
|
|
45
|
-
private static requireValidJsonSchema;
|
|
46
|
-
private applyImprovementsOnErrorMessages;
|
|
47
|
-
/**
|
|
48
|
-
* Filters out noisy errors produced by nullable anyOf patterns.
|
|
49
|
-
* When `nullable()` wraps a schema in `anyOf: [realSchema, { type: 'null' }]`,
|
|
50
|
-
* AJV produces "must be null" and "must match a schema in anyOf" errors
|
|
51
|
-
* that are confusing. This method splices them out, keeping only the real errors.
|
|
52
|
-
*/
|
|
53
|
-
private filterNullableAnyOfErrors;
|
|
54
|
-
/**
|
|
55
|
-
* Navigates the schema tree using an AJV schemaPath (e.g. "#/properties/foo/anyOf")
|
|
56
|
-
* and returns the parent schema containing the last keyword.
|
|
57
|
-
*/
|
|
58
|
-
private resolveSchemaPath;
|
|
59
|
-
private getErrorMessageForInstancePath;
|
|
60
|
-
private traverseSchemaPath;
|
|
61
|
-
private getChildSchema;
|
|
62
|
-
private getArrayItemSchema;
|
|
63
|
-
private getObjectPropertySchema;
|
|
64
|
-
}
|
|
65
|
-
export declare const HIDDEN_AJV_SCHEMA: unique symbol;
|
|
66
|
-
export type WithCachedAjvSchema<Base, OUT> = Base & {
|
|
67
|
-
[HIDDEN_AJV_SCHEMA]: AjvSchema<OUT>;
|
|
68
|
-
};
|
|
69
|
-
export interface AjvValidationOptions {
|
|
70
|
-
/**
|
|
71
|
-
* Defaults to true,
|
|
72
|
-
* because that's how AJV works by default,
|
|
73
|
-
* and what gives it performance advantage.
|
|
74
|
-
* (Because we have found that deep-clone is surprisingly slow,
|
|
75
|
-
* nearly as slow as Joi validation).
|
|
76
|
-
*
|
|
77
|
-
* If set to true - AJV will mutate the input in case it needs to apply transformations
|
|
78
|
-
* (strip unknown properties, convert types, etc).
|
|
79
|
-
*
|
|
80
|
-
* If false - it will deep-clone (using JSON.stringify+parse) the input to prevent its mutation.
|
|
81
|
-
* Will return the cloned/mutated object.
|
|
82
|
-
* Please note that JSON.stringify+parse has side-effects,
|
|
83
|
-
* e.g it will transform Buffer into a weird object.
|
|
84
|
-
*/
|
|
85
|
-
mutateInput?: boolean;
|
|
86
|
-
inputName?: string;
|
|
87
|
-
inputId?: string;
|
|
88
|
-
/**
|
|
89
|
-
* Function that returns "original input".
|
|
90
|
-
* What is original input?
|
|
91
|
-
* It's an input in its original non-mutated form.
|
|
92
|
-
* Why is it needed?
|
|
93
|
-
* Because we mutates the Input here. And after its been mutated - we no longer
|
|
94
|
-
* can include it "how it was" in an error message. So, for that reason we'll use
|
|
95
|
-
* `getOriginalInput()`, if it's provided.
|
|
96
|
-
*/
|
|
97
|
-
getOriginalInput?: () => unknown;
|
|
98
|
-
}
|
|
99
|
-
export interface AjvSchemaCfg {
|
|
100
|
-
/**
|
|
101
|
-
* Pass Ajv instance, otherwise Ajv will be created with
|
|
102
|
-
* AjvSchema default (not the same as Ajv defaults) parameters
|
|
103
|
-
*/
|
|
104
|
-
ajv: Ajv;
|
|
105
|
-
inputName?: string;
|
|
106
|
-
/**
|
|
107
|
-
* Option of Ajv.
|
|
108
|
-
* If set to true - will mutate your input objects!
|
|
109
|
-
* Defaults to false.
|
|
110
|
-
*
|
|
111
|
-
* This option is a "shortcut" to skip creating and passing Ajv instance.
|
|
112
|
-
*/
|
|
113
|
-
/**
|
|
114
|
-
* If true - schema will be compiled on-demand (lazily).
|
|
115
|
-
* Default: false.
|
|
116
|
-
*/
|
|
117
|
-
lazy?: boolean;
|
|
118
|
-
}
|
|
119
|
-
export type SchemaHandledByAjv<OUT> = JsonSchemaTerminal<OUT, any> | JsonSchema<OUT> | AjvSchema<OUT>;
|
|
120
6
|
export declare const j: {
|
|
121
7
|
/**
|
|
122
8
|
* Matches literally any value - equivalent to TypeScript's `any` type.
|
|
123
9
|
* Use sparingly, as it bypasses type validation entirely.
|
|
124
10
|
*/
|
|
125
|
-
any():
|
|
126
|
-
string():
|
|
127
|
-
number():
|
|
128
|
-
boolean():
|
|
11
|
+
any(): JBuilder<any, false>;
|
|
12
|
+
string(): JString<string, false>;
|
|
13
|
+
number(): JNumber<number, false>;
|
|
14
|
+
boolean(): JBoolean<boolean, false>;
|
|
129
15
|
object: typeof object & {
|
|
130
16
|
dbEntity: typeof objectDbEntity;
|
|
131
17
|
infer: typeof objectInfer;
|
|
132
|
-
any():
|
|
133
|
-
stringMap<S extends
|
|
18
|
+
any(): JObject<AnyObject, false>;
|
|
19
|
+
stringMap<S extends JSchema<any, any>>(schema: S): JObject<StringMap<SchemaOut<S>>, false>;
|
|
134
20
|
/**
|
|
135
21
|
* @experimental Look around, maybe you find a rule that is better for your use-case.
|
|
136
22
|
*
|
|
@@ -166,11 +52,11 @@ export declare const j: {
|
|
|
166
52
|
withEnumKeys: typeof withEnumKeys;
|
|
167
53
|
withRegexKeys: typeof withRegexKeys;
|
|
168
54
|
};
|
|
169
|
-
array<OUT, Opt>(itemSchema:
|
|
170
|
-
tuple<const S extends
|
|
171
|
-
set<OUT, Opt>(itemSchema:
|
|
172
|
-
buffer():
|
|
173
|
-
enum<const T extends any>(input: T, opt?: JsonBuilderRuleOpt | undefined):
|
|
55
|
+
array<OUT, Opt>(itemSchema: JBuilder<OUT, Opt>): JArray<OUT, Opt>;
|
|
56
|
+
tuple<const S extends JBuilder<any, any>[]>(items: S): JTuple<S>;
|
|
57
|
+
set<OUT, Opt>(itemSchema: JBuilder<OUT, Opt>): JSet2Builder<OUT, Opt>;
|
|
58
|
+
buffer(): JBuilder<Buffer<ArrayBufferLike>, false>;
|
|
59
|
+
enum<const T extends any>(input: T, opt?: JsonBuilderRuleOpt | undefined): JEnum<T extends readonly (infer U)[] ? U : T extends StringEnum ? T[keyof T] : T extends NumberEnum ? T[keyof T] : never, false>;
|
|
174
60
|
/**
|
|
175
61
|
* Use only with primitive values, otherwise this function will throw to avoid bugs.
|
|
176
62
|
* To validate objects, use `anyOfBy`.
|
|
@@ -182,7 +68,7 @@ export declare const j: {
|
|
|
182
68
|
* Use `anyOf` when schemas may overlap (e.g., AccountId | PartnerId with same format).
|
|
183
69
|
* Use `oneOf` when schemas are mutually exclusive.
|
|
184
70
|
*/
|
|
185
|
-
oneOf<B extends readonly
|
|
71
|
+
oneOf<B extends readonly JBuilder<any, boolean>[], OUT = BuilderOutUnion<B>>(items: [...B]): JBuilder<OUT, false>;
|
|
186
72
|
/**
|
|
187
73
|
* Use only with primitive values, otherwise this function will throw to avoid bugs.
|
|
188
74
|
* To validate objects, use `anyOfBy` or `anyOfThese`.
|
|
@@ -194,7 +80,7 @@ export declare const j: {
|
|
|
194
80
|
* Use `anyOf` when schemas may overlap (e.g., AccountId | PartnerId with same format).
|
|
195
81
|
* Use `oneOf` when schemas are mutually exclusive.
|
|
196
82
|
*/
|
|
197
|
-
anyOf<B extends readonly
|
|
83
|
+
anyOf<B extends readonly JBuilder<any, boolean>[], OUT = BuilderOutUnion<B>>(items: [...B]): JBuilder<OUT, false>;
|
|
198
84
|
/**
|
|
199
85
|
* Pick validation schema for an object based on the value of a specific property.
|
|
200
86
|
*
|
|
@@ -207,7 +93,7 @@ export declare const j: {
|
|
|
207
93
|
* const schema = j.anyOfBy('success', schemaMap)
|
|
208
94
|
* ```
|
|
209
95
|
*/
|
|
210
|
-
anyOfBy<D extends Record<PropertyKey,
|
|
96
|
+
anyOfBy<D extends Record<PropertyKey, JSchema<any, any>>, OUT = AnyOfByOut<D>>(propertyName: string, schemaDictionary: D): JBuilder<OUT, false>;
|
|
211
97
|
/**
|
|
212
98
|
* Custom version of `anyOf` which - in contrast to the original function - does not mutate the input.
|
|
213
99
|
* This comes with a performance penalty, so do not use it where performance matters.
|
|
@@ -216,17 +102,38 @@ export declare const j: {
|
|
|
216
102
|
* const schema = j.anyOfThese([successSchema, errorSchema])
|
|
217
103
|
* ```
|
|
218
104
|
*/
|
|
219
|
-
anyOfThese<B extends readonly
|
|
105
|
+
anyOfThese<B extends readonly JBuilder<any, boolean>[], OUT = BuilderOutUnion<B>>(items: [...B]): JBuilder<OUT, false>;
|
|
220
106
|
and(): {
|
|
221
107
|
silentBob: () => never;
|
|
222
108
|
};
|
|
223
|
-
literal<const V extends string | number | boolean | null>(v: V):
|
|
109
|
+
literal<const V extends string | number | boolean | null>(v: V): JEnum<V, false>;
|
|
110
|
+
/**
|
|
111
|
+
* Create a JSchema from a plain JsonSchema object.
|
|
112
|
+
* Useful when the schema is loaded from a JSON file or generated externally.
|
|
113
|
+
*
|
|
114
|
+
* Optionally accepts a custom Ajv instance and/or inputName for error messages.
|
|
115
|
+
*/
|
|
116
|
+
fromSchema<OUT>(schema: JsonSchema<OUT>, cfg?: {
|
|
117
|
+
ajv?: Ajv | undefined;
|
|
118
|
+
inputName?: string | undefined;
|
|
119
|
+
} | undefined): JSchema<OUT, false>;
|
|
224
120
|
};
|
|
225
|
-
export declare
|
|
121
|
+
export declare const HIDDEN_AJV_SCHEMA: unique symbol;
|
|
122
|
+
export type WithCachedAjvSchema<Base, OUT> = Base & {
|
|
123
|
+
[HIDDEN_AJV_SCHEMA]: AjvSchema<OUT>;
|
|
124
|
+
};
|
|
125
|
+
export declare class JSchema<OUT, Opt> {
|
|
226
126
|
protected [HIDDEN_AJV_SCHEMA]: AjvSchema<any> | undefined;
|
|
227
127
|
protected schema: JsonSchema;
|
|
228
|
-
|
|
229
|
-
|
|
128
|
+
private _cfg?;
|
|
129
|
+
constructor(schema: JsonSchema, cfg?: {
|
|
130
|
+
ajv?: Ajv;
|
|
131
|
+
inputName?: string;
|
|
132
|
+
});
|
|
133
|
+
private _builtSchema?;
|
|
134
|
+
private _compiledFns?;
|
|
135
|
+
private _getBuiltSchema;
|
|
136
|
+
private _getCompiled;
|
|
230
137
|
getSchema(): JsonSchema;
|
|
231
138
|
/**
|
|
232
139
|
* Produces a "clean schema object" without methods.
|
|
@@ -248,29 +155,20 @@ export declare class JsonSchemaTerminal<OUT, Opt> {
|
|
|
248
155
|
*
|
|
249
156
|
* If you throw an error from this function, it will show up as an error in the validation.
|
|
250
157
|
*/
|
|
251
|
-
postValidation<OUT2 = OUT>(fn: PostValidatonFn<OUT, OUT2>):
|
|
158
|
+
postValidation<OUT2 = OUT>(fn: PostValidatonFn<OUT, OUT2>): JSchema<OUT2, Opt>;
|
|
252
159
|
/**
|
|
253
160
|
* @experimental
|
|
254
161
|
*/
|
|
255
162
|
out: OUT;
|
|
256
163
|
opt: Opt;
|
|
257
164
|
}
|
|
258
|
-
export declare class
|
|
165
|
+
export declare class JBuilder<OUT, Opt> extends JSchema<OUT, Opt> {
|
|
259
166
|
protected setErrorMessage(ruleName: string, errorMessage: string | undefined): void;
|
|
260
167
|
/**
|
|
261
168
|
* A helper function that takes a type parameter and compares it with the type inferred from the schema.
|
|
262
169
|
*
|
|
263
170
|
* When the type inferred from the schema differs from the passed-in type,
|
|
264
171
|
* the schema becomes unusable, by turning its type into `never`.
|
|
265
|
-
*
|
|
266
|
-
* ```ts
|
|
267
|
-
* const schemaGood = j.string().isOfType<string>() // ✅
|
|
268
|
-
*
|
|
269
|
-
* const schemaBad = j.string().isOfType<number>() // ❌
|
|
270
|
-
* schemaBad.build() // TypeError: property "build" does not exist on type "never"
|
|
271
|
-
*
|
|
272
|
-
* const result = ajvValidateRequest.body(req, schemaBad) // result will have `unknown` type
|
|
273
|
-
* ```
|
|
274
172
|
*/
|
|
275
173
|
isOfType<ExpectedType>(): ExactMatch<ExpectedType, OUT> extends true ? this : never;
|
|
276
174
|
$schema($schema: string): this;
|
|
@@ -282,24 +180,24 @@ export declare class JsonSchemaAnyBuilder<OUT, Opt> extends JsonSchemaTerminal<O
|
|
|
282
180
|
type(type: string): this;
|
|
283
181
|
default(v: any): this;
|
|
284
182
|
instanceof(of: string): this;
|
|
285
|
-
optional():
|
|
286
|
-
nullable():
|
|
183
|
+
optional(): JBuilder<OUT | undefined, true>;
|
|
184
|
+
nullable(): JBuilder<OUT | null, Opt>;
|
|
287
185
|
/**
|
|
288
186
|
* @deprecated
|
|
289
187
|
* The usage of this function is discouraged as it defeats the purpose of having type-safe validation.
|
|
290
188
|
*/
|
|
291
|
-
castAs<T>():
|
|
189
|
+
castAs<T>(): JBuilder<T, Opt>;
|
|
292
190
|
/**
|
|
293
191
|
* Locks the given schema chain and no other modification can be done to it.
|
|
294
192
|
*/
|
|
295
|
-
final():
|
|
193
|
+
final(): JSchema<OUT, Opt>;
|
|
296
194
|
/**
|
|
297
195
|
*
|
|
298
196
|
* @param validator A validator function that returns an error message or undefined.
|
|
299
197
|
*
|
|
300
198
|
* You may add multiple custom validators and they will be executed in the order you added them.
|
|
301
199
|
*/
|
|
302
|
-
custom<OUT2 = OUT>(validator: CustomValidatorFn):
|
|
200
|
+
custom<OUT2 = OUT>(validator: CustomValidatorFn): JBuilder<OUT2, Opt>;
|
|
303
201
|
/**
|
|
304
202
|
*
|
|
305
203
|
* @param converter A converter function that returns a new value.
|
|
@@ -310,9 +208,9 @@ export declare class JsonSchemaAnyBuilder<OUT, Opt> extends JsonSchemaTerminal<O
|
|
|
310
208
|
* This feature only works when the current schema is nested in an object or array schema,
|
|
311
209
|
* due to how mutability works in Ajv.
|
|
312
210
|
*/
|
|
313
|
-
convert<OUT2>(converter: CustomConverterFn<OUT2>):
|
|
211
|
+
convert<OUT2>(converter: CustomConverterFn<OUT2>): JBuilder<OUT2, Opt>;
|
|
314
212
|
}
|
|
315
|
-
export declare class
|
|
213
|
+
export declare class JString<OUT extends string | undefined = string, Opt extends boolean = false> extends JBuilder<OUT, Opt> {
|
|
316
214
|
constructor();
|
|
317
215
|
/**
|
|
318
216
|
* @param optionalValues List of values that should be considered/converted as `undefined`.
|
|
@@ -322,10 +220,10 @@ export declare class JsonSchemaStringBuilder<OUT extends string | undefined = st
|
|
|
322
220
|
*
|
|
323
221
|
* Make sure this `optional()` call is at the end of your call chain.
|
|
324
222
|
*
|
|
325
|
-
* When `null` is included in optionalValues, the return type becomes `
|
|
223
|
+
* When `null` is included in optionalValues, the return type becomes `JSchema`
|
|
326
224
|
* (no further chaining allowed) because the schema is wrapped in an anyOf structure.
|
|
327
225
|
*/
|
|
328
|
-
optional<T extends readonly (string | null)[] | undefined = undefined>(optionalValues?: T): T extends readonly (infer U)[] ? null extends U ?
|
|
226
|
+
optional<T extends readonly (string | null)[] | undefined = undefined>(optionalValues?: T): T extends readonly (infer U)[] ? null extends U ? JSchema<OUT | undefined, true> : JString<OUT | undefined, true> : JString<OUT | undefined, true>;
|
|
329
227
|
regex(pattern: RegExp, opt?: JsonBuilderRuleOpt): this;
|
|
330
228
|
pattern(pattern: string, opt?: JsonBuilderRuleOpt): this;
|
|
331
229
|
minLength(minLength: number): this;
|
|
@@ -337,16 +235,16 @@ export declare class JsonSchemaStringBuilder<OUT extends string | undefined = st
|
|
|
337
235
|
toLowerCase(): this;
|
|
338
236
|
toUpperCase(): this;
|
|
339
237
|
truncate(toLength: number): this;
|
|
340
|
-
branded<B extends string>():
|
|
238
|
+
branded<B extends string>(): JString<B, Opt>;
|
|
341
239
|
/**
|
|
342
240
|
* Validates that the input is a fully-specified YYYY-MM-DD formatted valid IsoDate value.
|
|
343
241
|
*
|
|
344
242
|
* All previous expectations in the schema chain are dropped - including `.optional()` -
|
|
345
243
|
* because this call effectively starts a new schema chain.
|
|
346
244
|
*/
|
|
347
|
-
isoDate():
|
|
348
|
-
isoDateTime():
|
|
349
|
-
isoMonth():
|
|
245
|
+
isoDate(): JIsoDate;
|
|
246
|
+
isoDateTime(): JString<IsoDateTime, Opt>;
|
|
247
|
+
isoMonth(): JBuilder<IsoMonth, false>;
|
|
350
248
|
/**
|
|
351
249
|
* Validates the string format to be JWT.
|
|
352
250
|
* Expects the JWT to be signed!
|
|
@@ -366,14 +264,14 @@ export declare class JsonSchemaStringBuilder<OUT extends string | undefined = st
|
|
|
366
264
|
* All previous expectations in the schema chain are dropped - including `.optional()` -
|
|
367
265
|
* because this call effectively starts a new schema chain as an `enum` validation.
|
|
368
266
|
*/
|
|
369
|
-
ianaTimezone():
|
|
267
|
+
ianaTimezone(): JEnum<IANATimezone, false>;
|
|
370
268
|
base64Url(): this;
|
|
371
269
|
uuid(): this;
|
|
372
270
|
}
|
|
373
271
|
export interface JsonSchemaStringEmailOptions {
|
|
374
272
|
checkTLD: boolean;
|
|
375
273
|
}
|
|
376
|
-
export declare class
|
|
274
|
+
export declare class JIsoDate<Opt extends boolean = false> extends JBuilder<IsoDate, Opt> {
|
|
377
275
|
constructor();
|
|
378
276
|
/**
|
|
379
277
|
* @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
|
|
@@ -381,10 +279,10 @@ export declare class JsonSchemaIsoDateBuilder<Opt extends boolean = false> exten
|
|
|
381
279
|
* This `null` feature only works when the current schema is nested in an object or array schema,
|
|
382
280
|
* due to how mutability works in Ajv.
|
|
383
281
|
*
|
|
384
|
-
* When `null` is passed, the return type becomes `
|
|
282
|
+
* When `null` is passed, the return type becomes `JSchema`
|
|
385
283
|
* (no further chaining allowed) because the schema is wrapped in an anyOf structure.
|
|
386
284
|
*/
|
|
387
|
-
optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ?
|
|
285
|
+
optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JSchema<IsoDate | undefined, true> : JBuilder<IsoDate | undefined, true>;
|
|
388
286
|
before(date: string): this;
|
|
389
287
|
sameOrBefore(date: string): this;
|
|
390
288
|
after(date: string): this;
|
|
@@ -397,12 +295,9 @@ export interface JsonSchemaIsoDateOptions {
|
|
|
397
295
|
after?: string;
|
|
398
296
|
sameOrAfter?: string;
|
|
399
297
|
}
|
|
400
|
-
export declare class JsonSchemaIsoMonthBuilder<Opt extends boolean = false> extends JsonSchemaAnyBuilder<IsoMonth, Opt> {
|
|
401
|
-
constructor();
|
|
402
|
-
}
|
|
403
298
|
export interface JsonSchemaIsoMonthOptions {
|
|
404
299
|
}
|
|
405
|
-
export declare class
|
|
300
|
+
export declare class JNumber<OUT extends number | undefined = number, Opt extends boolean = false> extends JBuilder<OUT, Opt> {
|
|
406
301
|
constructor();
|
|
407
302
|
/**
|
|
408
303
|
* @param optionalValues List of values that should be considered/converted as `undefined`.
|
|
@@ -412,12 +307,12 @@ export declare class JsonSchemaNumberBuilder<OUT extends number | undefined = nu
|
|
|
412
307
|
*
|
|
413
308
|
* Make sure this `optional()` call is at the end of your call chain.
|
|
414
309
|
*
|
|
415
|
-
* When `null` is included in optionalValues, the return type becomes `
|
|
310
|
+
* When `null` is included in optionalValues, the return type becomes `JSchema`
|
|
416
311
|
* (no further chaining allowed) because the schema is wrapped in an anyOf structure.
|
|
417
312
|
*/
|
|
418
|
-
optional<T extends readonly (number | null)[] | undefined = undefined>(optionalValues?: T): T extends readonly (infer U)[] ? null extends U ?
|
|
313
|
+
optional<T extends readonly (number | null)[] | undefined = undefined>(optionalValues?: T): T extends readonly (infer U)[] ? null extends U ? JSchema<OUT | undefined, true> : JNumber<OUT | undefined, true> : JNumber<OUT | undefined, true>;
|
|
419
314
|
integer(): this;
|
|
420
|
-
branded<B extends number>():
|
|
315
|
+
branded<B extends number>(): JNumber<B, Opt>;
|
|
421
316
|
multipleOf(multipleOf: number): this;
|
|
422
317
|
min(minimum: number): this;
|
|
423
318
|
exclusiveMin(exclusiveMinimum: number): this;
|
|
@@ -433,10 +328,10 @@ export declare class JsonSchemaNumberBuilder<OUT extends number | undefined = nu
|
|
|
433
328
|
int64(): this;
|
|
434
329
|
float(): this;
|
|
435
330
|
double(): this;
|
|
436
|
-
unixTimestamp():
|
|
437
|
-
unixTimestamp2000():
|
|
438
|
-
unixTimestampMillis():
|
|
439
|
-
unixTimestamp2000Millis():
|
|
331
|
+
unixTimestamp(): JNumber<UnixTimestamp, Opt>;
|
|
332
|
+
unixTimestamp2000(): JNumber<UnixTimestamp, Opt>;
|
|
333
|
+
unixTimestampMillis(): JNumber<UnixTimestampMillis, Opt>;
|
|
334
|
+
unixTimestamp2000Millis(): JNumber<UnixTimestampMillis, Opt>;
|
|
440
335
|
utcOffset(): this;
|
|
441
336
|
utcOffsetHour(): this;
|
|
442
337
|
/**
|
|
@@ -446,7 +341,7 @@ export declare class JsonSchemaNumberBuilder<OUT extends number | undefined = nu
|
|
|
446
341
|
*/
|
|
447
342
|
precision(numberOfDigits: number): this;
|
|
448
343
|
}
|
|
449
|
-
export declare class
|
|
344
|
+
export declare class JBoolean<OUT extends boolean | undefined = boolean, Opt extends boolean = false> extends JBuilder<OUT, Opt> {
|
|
450
345
|
constructor();
|
|
451
346
|
/**
|
|
452
347
|
* @param optionalValue One of the two possible boolean values that should be considered/converted as `undefined`.
|
|
@@ -454,29 +349,28 @@ export declare class JsonSchemaBooleanBuilder<OUT extends boolean | undefined =
|
|
|
454
349
|
* This `optionalValue` feature only works when the current schema is nested in an object or array schema,
|
|
455
350
|
* due to how mutability works in Ajv.
|
|
456
351
|
*/
|
|
457
|
-
optional(optionalValue?: boolean):
|
|
352
|
+
optional(optionalValue?: boolean): JBoolean<OUT | undefined, true>;
|
|
458
353
|
}
|
|
459
|
-
export declare class
|
|
460
|
-
constructor(props?: AnyObject, opt?:
|
|
461
|
-
addProperties(props: AnyObject): this;
|
|
354
|
+
export declare class JObject<OUT extends AnyObject, Opt extends boolean = false> extends JBuilder<OUT, Opt> {
|
|
355
|
+
constructor(props?: AnyObject, opt?: JObjectOpts);
|
|
462
356
|
/**
|
|
463
357
|
* @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
|
|
464
358
|
*
|
|
465
359
|
* This `null` feature only works when the current schema is nested in an object or array schema,
|
|
466
360
|
* due to how mutability works in Ajv.
|
|
467
361
|
*
|
|
468
|
-
* When `null` is passed, the return type becomes `
|
|
362
|
+
* When `null` is passed, the return type becomes `JSchema`
|
|
469
363
|
* (no further chaining allowed) because the schema is wrapped in an anyOf structure.
|
|
470
364
|
*/
|
|
471
|
-
optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ?
|
|
365
|
+
optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JSchema<OUT | undefined, true> : JBuilder<OUT | undefined, true>;
|
|
472
366
|
/**
|
|
473
367
|
* When set, the validation will not strip away properties that are not specified explicitly in the schema.
|
|
474
368
|
*/
|
|
475
369
|
allowAdditionalProperties(): this;
|
|
476
|
-
extend<P extends Record<string,
|
|
477
|
-
[K in keyof P as P[K] extends
|
|
370
|
+
extend<P extends Record<string, JBuilder<any, any>>>(props: P): JObject<Override<OUT, {
|
|
371
|
+
[K in keyof P as P[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? never : K : never]: P[K] extends JBuilder<infer OUT2, any> ? OUT2 : never;
|
|
478
372
|
} & {
|
|
479
|
-
[K in keyof P as P[K] extends
|
|
373
|
+
[K in keyof P as P[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: P[K] extends JBuilder<infer OUT2, any> ? OUT2 : never;
|
|
480
374
|
}>, false>;
|
|
481
375
|
/**
|
|
482
376
|
* Concatenates another schema to the current schema.
|
|
@@ -484,23 +378,12 @@ export declare class JsonSchemaObjectBuilder<OUT extends AnyObject, Opt extends
|
|
|
484
378
|
* It expects you to use `isOfType<T>()` in the chain,
|
|
485
379
|
* otherwise the validation will throw. This is to ensure
|
|
486
380
|
* that the schemas you concatenated match the intended final type.
|
|
487
|
-
*
|
|
488
|
-
* ```ts
|
|
489
|
-
* interface Foo { foo: string }
|
|
490
|
-
* const fooSchema = j.object<Foo>({ foo: j.string() })
|
|
491
|
-
*
|
|
492
|
-
* interface Bar { bar: number }
|
|
493
|
-
* const barSchema = j.object<Bar>({ bar: j.number() })
|
|
494
|
-
*
|
|
495
|
-
* interface Shu { foo: string, bar: number }
|
|
496
|
-
* const shuSchema = fooSchema.concat(barSchema).isOfType<Shu>() // important
|
|
497
|
-
* ```
|
|
498
381
|
*/
|
|
499
|
-
concat<OUT2 extends AnyObject>(other:
|
|
382
|
+
concat<OUT2 extends AnyObject>(other: JObject<OUT2, any>): JObject<OUT & OUT2, false>;
|
|
500
383
|
/**
|
|
501
384
|
* Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
|
|
502
385
|
*/
|
|
503
|
-
dbEntity():
|
|
386
|
+
dbEntity(): JObject<Override<OUT, {
|
|
504
387
|
id: string;
|
|
505
388
|
created: any;
|
|
506
389
|
updated: any;
|
|
@@ -509,54 +392,53 @@ export declare class JsonSchemaObjectBuilder<OUT extends AnyObject, Opt extends
|
|
|
509
392
|
maxProperties(maxProperties: number): this;
|
|
510
393
|
exclusiveProperties(propNames: readonly (keyof OUT & string)[]): this;
|
|
511
394
|
}
|
|
512
|
-
interface
|
|
395
|
+
interface JObjectOpts {
|
|
513
396
|
hasIsOfTypeCheck?: false;
|
|
514
397
|
patternProperties?: StringMap<JsonSchema<any>>;
|
|
515
398
|
keySchema?: JsonSchema;
|
|
516
399
|
}
|
|
517
|
-
export declare class
|
|
518
|
-
[K in keyof PROPS as PROPS[K] extends
|
|
400
|
+
export declare class JObjectInfer<PROPS extends Record<string, JBuilder<any, any>>, Opt extends boolean = false> extends JBuilder<Expand<{
|
|
401
|
+
[K in keyof PROPS as PROPS[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JBuilder<infer OUT, any> ? OUT : never;
|
|
519
402
|
} & {
|
|
520
|
-
[K in keyof PROPS as PROPS[K] extends
|
|
403
|
+
[K in keyof PROPS as PROPS[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JBuilder<infer OUT, any> ? OUT : never;
|
|
521
404
|
}>, Opt> {
|
|
522
405
|
constructor(props?: PROPS);
|
|
523
|
-
addProperties(props: PROPS): this;
|
|
524
406
|
/**
|
|
525
407
|
* @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
|
|
526
408
|
*
|
|
527
409
|
* This `null` feature only works when the current schema is nested in an object or array schema,
|
|
528
410
|
* due to how mutability works in Ajv.
|
|
529
411
|
*
|
|
530
|
-
* When `null` is passed, the return type becomes `
|
|
412
|
+
* When `null` is passed, the return type becomes `JSchema`
|
|
531
413
|
* (no further chaining allowed) because the schema is wrapped in an anyOf structure.
|
|
532
414
|
*/
|
|
533
|
-
optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ?
|
|
534
|
-
[K in keyof PROPS as PROPS[K] extends
|
|
415
|
+
optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JSchema<Expand<{
|
|
416
|
+
[K in keyof PROPS as PROPS[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JBuilder<infer OUT, any> ? OUT : never;
|
|
535
417
|
} & {
|
|
536
|
-
[K in keyof PROPS as PROPS[K] extends
|
|
537
|
-
}> | undefined, true> :
|
|
538
|
-
[K in keyof PROPS as PROPS[K] extends
|
|
418
|
+
[K in keyof PROPS as PROPS[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JBuilder<infer OUT, any> ? OUT : never;
|
|
419
|
+
}> | undefined, true> : JBuilder<Expand<{
|
|
420
|
+
[K in keyof PROPS as PROPS[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JBuilder<infer OUT, any> ? OUT : never;
|
|
539
421
|
} & {
|
|
540
|
-
[K in keyof PROPS as PROPS[K] extends
|
|
422
|
+
[K in keyof PROPS as PROPS[K] extends JBuilder<any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JBuilder<infer OUT, any> ? OUT : never;
|
|
541
423
|
}> | undefined, true>;
|
|
542
424
|
/**
|
|
543
425
|
* When set, the validation will not strip away properties that are not specified explicitly in the schema.
|
|
544
426
|
*/
|
|
545
427
|
allowAdditionalProperties(): this;
|
|
546
|
-
extend<NEW_PROPS extends Record<string,
|
|
428
|
+
extend<NEW_PROPS extends Record<string, JBuilder<any, any>>>(props: NEW_PROPS): JObjectInfer<{
|
|
547
429
|
[K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS ? NEW_PROPS[K] : K extends keyof PROPS ? PROPS[K] : never;
|
|
548
430
|
}, Opt>;
|
|
549
431
|
/**
|
|
550
432
|
* Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
|
|
551
433
|
*/
|
|
552
|
-
dbEntity():
|
|
553
|
-
id:
|
|
554
|
-
created:
|
|
555
|
-
updated:
|
|
434
|
+
dbEntity(): JObjectInfer<{ [K in "created" | "id" | "updated" | keyof PROPS]: K extends "created" | "id" | "updated" ? {
|
|
435
|
+
id: JString<string, false>;
|
|
436
|
+
created: JNumber<UnixTimestamp, false>;
|
|
437
|
+
updated: JNumber<UnixTimestamp, false>;
|
|
556
438
|
}[K] : K extends keyof PROPS ? PROPS[K] : never; }, Opt>;
|
|
557
439
|
}
|
|
558
|
-
export declare class
|
|
559
|
-
constructor(itemsSchema:
|
|
440
|
+
export declare class JArray<OUT, Opt> extends JBuilder<OUT[], Opt> {
|
|
441
|
+
constructor(itemsSchema: JBuilder<OUT, Opt>);
|
|
560
442
|
minLength(minItems: number): this;
|
|
561
443
|
maxLength(maxItems: number): this;
|
|
562
444
|
length(exactLength: number): this;
|
|
@@ -564,28 +446,138 @@ export declare class JsonSchemaArrayBuilder<OUT, Opt> extends JsonSchemaAnyBuild
|
|
|
564
446
|
exactLength(length: number): this;
|
|
565
447
|
unique(): this;
|
|
566
448
|
}
|
|
567
|
-
|
|
568
|
-
constructor(itemsSchema:
|
|
449
|
+
declare class JSet2Builder<OUT, Opt> extends JBuilder<Set2<OUT>, Opt> {
|
|
450
|
+
constructor(itemsSchema: JBuilder<OUT, Opt>);
|
|
569
451
|
min(minItems: number): this;
|
|
570
452
|
max(maxItems: number): this;
|
|
571
453
|
}
|
|
572
|
-
export declare class
|
|
573
|
-
constructor();
|
|
574
|
-
}
|
|
575
|
-
export declare class JsonSchemaEnumBuilder<OUT extends string | number | boolean | null, Opt extends boolean = false> extends JsonSchemaAnyBuilder<OUT, Opt> {
|
|
454
|
+
export declare class JEnum<OUT extends string | number | boolean | null, Opt extends boolean = false> extends JBuilder<OUT, Opt> {
|
|
576
455
|
constructor(enumValues: readonly OUT[], baseType: EnumBaseType, opt?: JsonBuilderRuleOpt);
|
|
577
|
-
branded<B extends OUT>():
|
|
456
|
+
branded<B extends OUT>(): JEnum<B, Opt>;
|
|
578
457
|
}
|
|
579
|
-
export declare class
|
|
458
|
+
export declare class JTuple<ITEMS extends JBuilder<any, any>[]> extends JBuilder<TupleOut<ITEMS>, false> {
|
|
580
459
|
constructor(items: ITEMS);
|
|
581
460
|
}
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
585
|
-
|
|
586
|
-
|
|
461
|
+
declare function object(props: AnyObject): never;
|
|
462
|
+
declare function object<OUT extends AnyObject>(props: {
|
|
463
|
+
[K in keyof Required<OUT>]-?: JSchema<OUT[K], any>;
|
|
464
|
+
}): JObject<OUT, false>;
|
|
465
|
+
declare function objectInfer<P extends Record<string, JBuilder<any, any>>>(props: P): JObjectInfer<P, false>;
|
|
466
|
+
declare function objectDbEntity(props: AnyObject): never;
|
|
467
|
+
declare function objectDbEntity<OUT extends BaseDBEntity, EXTRA_KEYS extends Exclude<keyof OUT, keyof BaseDBEntity> = Exclude<keyof OUT, keyof BaseDBEntity>>(props: {
|
|
468
|
+
[K in EXTRA_KEYS]-?: BuilderFor<OUT[K]>;
|
|
469
|
+
} & (ExactMatch<OUT['id'], BaseDBEntity['id']> extends true ? {
|
|
470
|
+
id?: BuilderFor<BaseDBEntity['id']>;
|
|
471
|
+
} : {
|
|
472
|
+
id: BuilderFor<OUT['id']>;
|
|
473
|
+
}) & (ExactMatch<OUT['created'], BaseDBEntity['created']> extends true ? {
|
|
474
|
+
created?: BuilderFor<BaseDBEntity['created']>;
|
|
475
|
+
} : {
|
|
476
|
+
created: BuilderFor<OUT['created']>;
|
|
477
|
+
}) & (ExactMatch<OUT['updated'], BaseDBEntity['updated']> extends true ? {
|
|
478
|
+
updated?: BuilderFor<BaseDBEntity['updated']>;
|
|
479
|
+
} : {
|
|
480
|
+
updated: BuilderFor<OUT['updated']>;
|
|
481
|
+
})): JObject<OUT, false>;
|
|
482
|
+
declare function record<KS extends JBuilder<any, any>, VS extends JBuilder<any, any>, Opt extends boolean = SchemaOpt<VS>>(keySchema: KS, valueSchema: VS): JObject<Opt extends true ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>> : Record<SchemaOut<KS>, SchemaOut<VS>>, false>;
|
|
483
|
+
declare function withRegexKeys<S extends JBuilder<any, any>>(keyRegex: RegExp | string, schema: S): JObject<StringMap<SchemaOut<S>>, false>;
|
|
484
|
+
/**
|
|
485
|
+
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
486
|
+
*/
|
|
487
|
+
declare function withEnumKeys<const T extends readonly (string | number)[] | StringEnum | NumberEnum, S extends JBuilder<any, any>, K extends string | number = EnumKeyUnion<T>, Opt extends boolean = SchemaOpt<S>>(keys: T, schema: S): JObject<Opt extends true ? {
|
|
488
|
+
[P in K]?: SchemaOut<S>;
|
|
489
|
+
} : {
|
|
490
|
+
[P in K]: SchemaOut<S>;
|
|
491
|
+
}, false>;
|
|
492
|
+
/**
|
|
493
|
+
* On creation - compiles ajv validation function.
|
|
494
|
+
* Provides convenient methods, error reporting, etc.
|
|
495
|
+
*/
|
|
496
|
+
export declare class AjvSchema<OUT> {
|
|
497
|
+
schema: JsonSchema<OUT>;
|
|
498
|
+
private constructor();
|
|
499
|
+
/**
|
|
500
|
+
* Shortcut for AjvSchema.create(schema, { lazy: true })
|
|
501
|
+
*/
|
|
502
|
+
static createLazy<OUT>(schema: SchemaHandledByAjv<OUT>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<OUT>;
|
|
503
|
+
/**
|
|
504
|
+
* Conveniently allows to pass either JsonSchema or JSchema builder, or existing AjvSchema.
|
|
505
|
+
* If it's already an AjvSchema - it'll just return it without any processing.
|
|
506
|
+
* If it's a Builder - will call `build` before proceeding.
|
|
507
|
+
* Otherwise - will construct AjvSchema instance ready to be used.
|
|
508
|
+
*/
|
|
509
|
+
static create<OUT>(schema: SchemaHandledByAjv<OUT>, cfg?: Partial<AjvSchemaCfg>): AjvSchema<OUT>;
|
|
510
|
+
/**
|
|
511
|
+
* Creates a minimal AjvSchema wrapper from a pre-compiled validate function.
|
|
512
|
+
* Used internally by JSchema to cache a compatible AjvSchema instance.
|
|
513
|
+
*/
|
|
514
|
+
static _wrap<OUT>(schema: JsonSchema<OUT>, compiledFn: any): AjvSchema<OUT>;
|
|
515
|
+
static isSchemaWithCachedAjvSchema<Base, OUT>(schema: Base): schema is WithCachedAjvSchema<Base, OUT>;
|
|
516
|
+
static cacheAjvSchema<Base extends AnyObject, OUT>(schema: Base, ajvSchema: AjvSchema<OUT>): WithCachedAjvSchema<Base, OUT>;
|
|
517
|
+
static requireCachedAjvSchema<Base, OUT>(schema: WithCachedAjvSchema<Base, OUT>): AjvSchema<OUT>;
|
|
518
|
+
readonly cfg: AjvSchemaCfg;
|
|
519
|
+
private _compiledFn;
|
|
520
|
+
private _getValidateFn;
|
|
521
|
+
/**
|
|
522
|
+
* It returns the original object just for convenience.
|
|
523
|
+
*/
|
|
524
|
+
validate(input: unknown, opt?: AjvValidationOptions): OUT;
|
|
525
|
+
isValid(input: unknown, opt?: AjvValidationOptions): boolean;
|
|
526
|
+
getValidationResult(input: unknown, opt?: AjvValidationOptions): ValidationFunctionResult<OUT, AjvValidationError>;
|
|
527
|
+
getValidationFunction(): ValidationFunction<OUT, AjvValidationError>;
|
|
528
|
+
private static requireValidJsonSchema;
|
|
587
529
|
}
|
|
588
530
|
type EnumBaseType = 'string' | 'number' | 'other';
|
|
531
|
+
export interface AjvValidationOptions {
|
|
532
|
+
/**
|
|
533
|
+
* Custom Ajv instance to use for this validation.
|
|
534
|
+
* Overrides the default Ajv or any Ajv set at construction time.
|
|
535
|
+
* Compiled functions are cached per Ajv instance.
|
|
536
|
+
*/
|
|
537
|
+
ajv?: Ajv;
|
|
538
|
+
/**
|
|
539
|
+
* Defaults to true,
|
|
540
|
+
* because that's how AJV works by default,
|
|
541
|
+
* and what gives it performance advantage.
|
|
542
|
+
* (Because we have found that deep-clone is surprisingly slow,
|
|
543
|
+
* nearly as slow as Joi validation).
|
|
544
|
+
*
|
|
545
|
+
* If set to true - AJV will mutate the input in case it needs to apply transformations
|
|
546
|
+
* (strip unknown properties, convert types, etc).
|
|
547
|
+
*
|
|
548
|
+
* If false - it will deep-clone (using JSON.stringify+parse) the input to prevent its mutation.
|
|
549
|
+
* Will return the cloned/mutated object.
|
|
550
|
+
* Please note that JSON.stringify+parse has side-effects,
|
|
551
|
+
* e.g it will transform Buffer into a weird object.
|
|
552
|
+
*/
|
|
553
|
+
mutateInput?: boolean;
|
|
554
|
+
inputName?: string;
|
|
555
|
+
inputId?: string;
|
|
556
|
+
/**
|
|
557
|
+
* Function that returns "original input".
|
|
558
|
+
* What is original input?
|
|
559
|
+
* It's an input in its original non-mutated form.
|
|
560
|
+
* Why is it needed?
|
|
561
|
+
* Because we mutates the Input here. And after its been mutated - we no longer
|
|
562
|
+
* can include it "how it was" in an error message. So, for that reason we'll use
|
|
563
|
+
* `getOriginalInput()`, if it's provided.
|
|
564
|
+
*/
|
|
565
|
+
getOriginalInput?: () => unknown;
|
|
566
|
+
}
|
|
567
|
+
export interface AjvSchemaCfg {
|
|
568
|
+
/**
|
|
569
|
+
* Pass Ajv instance, otherwise Ajv will be created with
|
|
570
|
+
* AjvSchema default (not the same as Ajv defaults) parameters
|
|
571
|
+
*/
|
|
572
|
+
ajv: Ajv;
|
|
573
|
+
inputName?: string;
|
|
574
|
+
/**
|
|
575
|
+
* If true - schema will be compiled on-demand (lazily).
|
|
576
|
+
* Default: false.
|
|
577
|
+
*/
|
|
578
|
+
lazy?: boolean;
|
|
579
|
+
}
|
|
580
|
+
export type SchemaHandledByAjv<OUT> = JSchema<OUT, any> | JsonSchema<OUT> | AjvSchema<OUT>;
|
|
589
581
|
export interface JsonSchema<OUT = unknown> {
|
|
590
582
|
readonly out?: OUT;
|
|
591
583
|
$schema?: string;
|
|
@@ -662,37 +654,9 @@ export interface JsonSchema<OUT = unknown> {
|
|
|
662
654
|
customConversions?: CustomConverterFn<any>[];
|
|
663
655
|
postValidation?: PostValidatonFn<any, OUT>;
|
|
664
656
|
}
|
|
665
|
-
|
|
666
|
-
|
|
667
|
-
|
|
668
|
-
}): JsonSchemaObjectBuilder<OUT, false>;
|
|
669
|
-
declare function objectInfer<P extends Record<string, JsonSchemaAnyBuilder<any, any>>>(props: P): JsonSchemaObjectInferringBuilder<P, false>;
|
|
670
|
-
declare function objectDbEntity(props: AnyObject): never;
|
|
671
|
-
declare function objectDbEntity<OUT extends BaseDBEntity, EXTRA_KEYS extends Exclude<keyof OUT, keyof BaseDBEntity> = Exclude<keyof OUT, keyof BaseDBEntity>>(props: {
|
|
672
|
-
[K in EXTRA_KEYS]-?: BuilderFor<OUT[K]>;
|
|
673
|
-
} & (ExactMatch<OUT['id'], BaseDBEntity['id']> extends true ? {
|
|
674
|
-
id?: BuilderFor<BaseDBEntity['id']>;
|
|
675
|
-
} : {
|
|
676
|
-
id: BuilderFor<OUT['id']>;
|
|
677
|
-
}) & (ExactMatch<OUT['created'], BaseDBEntity['created']> extends true ? {
|
|
678
|
-
created?: BuilderFor<BaseDBEntity['created']>;
|
|
679
|
-
} : {
|
|
680
|
-
created: BuilderFor<OUT['created']>;
|
|
681
|
-
}) & (ExactMatch<OUT['updated'], BaseDBEntity['updated']> extends true ? {
|
|
682
|
-
updated?: BuilderFor<BaseDBEntity['updated']>;
|
|
683
|
-
} : {
|
|
684
|
-
updated: BuilderFor<OUT['updated']>;
|
|
685
|
-
})): JsonSchemaObjectBuilder<OUT, false>;
|
|
686
|
-
declare function record<KS extends JsonSchemaAnyBuilder<any, any>, VS extends JsonSchemaAnyBuilder<any, any>, Opt extends boolean = SchemaOpt<VS>>(keySchema: KS, valueSchema: VS): JsonSchemaObjectBuilder<Opt extends true ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>> : Record<SchemaOut<KS>, SchemaOut<VS>>, false>;
|
|
687
|
-
declare function withRegexKeys<S extends JsonSchemaAnyBuilder<any, any>>(keyRegex: RegExp | string, schema: S): JsonSchemaObjectBuilder<StringMap<SchemaOut<S>>, false>;
|
|
688
|
-
/**
|
|
689
|
-
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
690
|
-
*/
|
|
691
|
-
declare function withEnumKeys<const T extends readonly (string | number)[] | StringEnum | NumberEnum, S extends JsonSchemaAnyBuilder<any, any>, K extends string | number = EnumKeyUnion<T>, Opt extends boolean = SchemaOpt<S>>(keys: T, schema: S): JsonSchemaObjectBuilder<Opt extends true ? {
|
|
692
|
-
[P in K]?: SchemaOut<S>;
|
|
693
|
-
} : {
|
|
694
|
-
[P in K]: SchemaOut<S>;
|
|
695
|
-
}, false>;
|
|
657
|
+
export type PostValidatonFn<OUT, OUT2> = (v: OUT) => OUT2;
|
|
658
|
+
export type CustomValidatorFn = (v: any) => string | undefined;
|
|
659
|
+
export type CustomConverterFn<OUT> = (v: any) => OUT;
|
|
696
660
|
type Expand<T> = {
|
|
697
661
|
[K in keyof T]: T[K];
|
|
698
662
|
};
|
|
@@ -711,14 +675,14 @@ type IsAny<T> = 0 extends 1 & T ? true : false;
|
|
|
711
675
|
type IsAssignableRelaxed<A, B> = IsAny<RelaxIndexSignature<A>> extends true ? true : [RelaxIndexSignature<A>] extends [B] ? true : false;
|
|
712
676
|
type ExactMatchBase<A, B> = (<T>() => T extends A ? 1 : 2) extends <T>() => (T extends B ? 1 : 2) ? (<T>() => T extends B ? 1 : 2) extends <T>() => (T extends A ? 1 : 2) ? true : false : false;
|
|
713
677
|
type ExactMatch<A, B> = HasAllowExtraKeys<B> extends true ? IsAssignableRelaxed<B, A> : ExactMatchBase<Expand<A>, Expand<B>> extends true ? true : ExactMatchBase<Expand<StripIndexSignatureDeep<A>>, Expand<StripIndexSignatureDeep<B>>>;
|
|
714
|
-
type BuilderOutUnion<B extends readonly
|
|
715
|
-
[K in keyof B]: B[K] extends
|
|
678
|
+
type BuilderOutUnion<B extends readonly JBuilder<any, any>[]> = {
|
|
679
|
+
[K in keyof B]: B[K] extends JBuilder<infer O, any> ? O : never;
|
|
716
680
|
}[number];
|
|
717
|
-
type AnyOfByOut<D extends Record<PropertyKey,
|
|
718
|
-
[K in keyof D]: D[K] extends
|
|
681
|
+
type AnyOfByOut<D extends Record<PropertyKey, JSchema<any, any>>> = {
|
|
682
|
+
[K in keyof D]: D[K] extends JSchema<infer O, any> ? O : never;
|
|
719
683
|
}[keyof D];
|
|
720
|
-
type BuilderFor<T> =
|
|
721
|
-
interface JsonBuilderRuleOpt {
|
|
684
|
+
type BuilderFor<T> = JBuilder<T, any>;
|
|
685
|
+
export interface JsonBuilderRuleOpt {
|
|
722
686
|
/**
|
|
723
687
|
* Text of error message to return when the validation fails for the given rule:
|
|
724
688
|
*
|
|
@@ -733,12 +697,9 @@ interface JsonBuilderRuleOpt {
|
|
|
733
697
|
name?: string;
|
|
734
698
|
}
|
|
735
699
|
type EnumKeyUnion<T> = T extends readonly (infer U)[] ? U : T extends StringEnum | NumberEnum ? T[keyof T] : never;
|
|
736
|
-
type SchemaOut<S> = S extends
|
|
737
|
-
type SchemaOpt<S> = S extends
|
|
738
|
-
type TupleOut<T extends readonly
|
|
739
|
-
[K in keyof T]: T[K] extends
|
|
700
|
+
type SchemaOut<S> = S extends JBuilder<infer OUT, any> ? OUT : never;
|
|
701
|
+
type SchemaOpt<S> = S extends JBuilder<any, infer Opt> ? (Opt extends true ? true : false) : false;
|
|
702
|
+
type TupleOut<T extends readonly JBuilder<any, any>[]> = {
|
|
703
|
+
[K in keyof T]: T[K] extends JBuilder<infer O, any> ? O : never;
|
|
740
704
|
};
|
|
741
|
-
export type PostValidatonFn<OUT, OUT2> = (v: OUT) => OUT2;
|
|
742
|
-
export type CustomValidatorFn = (v: any) => string | undefined;
|
|
743
|
-
export type CustomConverterFn<OUT> = (v: any) => OUT;
|
|
744
705
|
export {};
|