@naturalcycles/nodejs-lib 15.90.2 → 15.91.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.
@@ -1,9 +1,8 @@
1
1
  import type { ValidationFunction, ValidationFunctionResult } from '@naturalcycles/js-lib';
2
- import type { AnyObject } from '@naturalcycles/js-lib/types';
2
+ import type { Set2 } from '@naturalcycles/js-lib/object';
3
+ import type { AnyObject, BaseDBEntity, IANATimezone, Inclusiveness, IsoDate, IsoDateTime, IsoMonth, NumberEnum, StringEnum, StringMap, UnixTimestamp, UnixTimestampMillis } from '@naturalcycles/js-lib/types';
3
4
  import type { Ajv } from 'ajv';
4
5
  import { AjvValidationError } from './ajvValidationError.js';
5
- import { JsonSchemaTerminal } from './jsonSchemaBuilder.js';
6
- import type { JsonSchema } from './jsonSchemaBuilder.js';
7
6
  /**
8
7
  * On creation - compiles ajv validation function.
9
8
  * Provides convenient methods, error reporting, etc.
@@ -106,3 +105,660 @@ export interface AjvSchemaCfg {
106
105
  lazy?: boolean;
107
106
  }
108
107
  export type SchemaHandledByAjv<IN, OUT = IN> = JsonSchemaTerminal<IN, OUT, any> | JsonSchema<IN, OUT> | AjvSchema<IN, OUT>;
108
+ export declare const j: {
109
+ /**
110
+ * Matches literally any value - equivalent to TypeScript's `any` type.
111
+ * Use sparingly, as it bypasses type validation entirely.
112
+ */
113
+ any(): JsonSchemaAnyBuilder<any, any, false>;
114
+ string(): JsonSchemaStringBuilder<string, string, false>;
115
+ number(): JsonSchemaNumberBuilder<number, number, false>;
116
+ boolean(): JsonSchemaBooleanBuilder<boolean, boolean, false>;
117
+ object: typeof object & {
118
+ dbEntity: typeof objectDbEntity;
119
+ infer: typeof objectInfer;
120
+ any(): JsonSchemaObjectBuilder<AnyObject, AnyObject, false>;
121
+ stringMap<S extends JsonSchemaTerminal<any, any, any>>(schema: S): JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>, false>;
122
+ /**
123
+ * @experimental Look around, maybe you find a rule that is better for your use-case.
124
+ *
125
+ * For Record<K, V> type of validations.
126
+ * ```ts
127
+ * const schema = j.object
128
+ * .record(
129
+ * j
130
+ * .string()
131
+ * .regex(/^\d{3,4}$/)
132
+ * .branded<B>(),
133
+ * j.number().nullable(),
134
+ * )
135
+ * .isOfType<Record<B, number | null>>()
136
+ * ```
137
+ *
138
+ * When the keys of the Record are values from an Enum, prefer `j.object.withEnumKeys`!
139
+ *
140
+ * Non-matching keys will be stripped from the object, i.e. they will not cause an error.
141
+ *
142
+ * Caveat: This rule first validates values of every properties of the object, and only then validates the keys.
143
+ * A consequence of that is that the validation will throw when there is an unexpected property with a value not matching the value schema.
144
+ */
145
+ record: typeof record;
146
+ /**
147
+ * For Record<ENUM, V> type of validations.
148
+ *
149
+ * When the keys of the Record are values from an Enum,
150
+ * this helper is more performant and behaves in a more conventional manner than `j.object.record` would.
151
+ *
152
+ *
153
+ */
154
+ withEnumKeys: typeof withEnumKeys;
155
+ withRegexKeys: typeof withRegexKeys;
156
+ };
157
+ array<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaArrayBuilder<IN, OUT, Opt>;
158
+ tuple<const S extends JsonSchemaAnyBuilder<any, any, any>[]>(items: S): JsonSchemaTupleBuilder<S>;
159
+ set<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaSet2Builder<IN, OUT, Opt>;
160
+ buffer(): JsonSchemaBufferBuilder;
161
+ enum<const T extends any>(input: T, opt?: JsonBuilderRuleOpt | undefined): JsonSchemaEnumBuilder<T extends readonly (infer U)[] ? U : T extends StringEnum ? T[keyof T] : T extends NumberEnum ? T[keyof T] : never, T extends readonly (infer U)[] ? U : T extends StringEnum ? T[keyof T] : T extends NumberEnum ? T[keyof T] : never, false>;
162
+ /**
163
+ * Use only with primitive values, otherwise this function will throw to avoid bugs.
164
+ * To validate objects, use `anyOfBy`.
165
+ *
166
+ * Our Ajv is configured to strip unexpected properties from objects,
167
+ * and since Ajv is mutating the input, this means that it cannot
168
+ * properly validate the same data over multiple schemas.
169
+ *
170
+ * Use `anyOf` when schemas may overlap (e.g., AccountId | PartnerId with same format).
171
+ * Use `oneOf` when schemas are mutually exclusive.
172
+ */
173
+ oneOf<B extends readonly JsonSchemaAnyBuilder<any, any, boolean>[], IN = BuilderInUnion<B>, OUT = BuilderOutUnion<B>>(items: [...B]): JsonSchemaAnyBuilder<IN, OUT, false>;
174
+ /**
175
+ * Use only with primitive values, otherwise this function will throw to avoid bugs.
176
+ * To validate objects, use `anyOfBy` or `anyOfThese`.
177
+ *
178
+ * Our Ajv is configured to strip unexpected properties from objects,
179
+ * and since Ajv is mutating the input, this means that it cannot
180
+ * properly validate the same data over multiple schemas.
181
+ *
182
+ * Use `anyOf` when schemas may overlap (e.g., AccountId | PartnerId with same format).
183
+ * Use `oneOf` when schemas are mutually exclusive.
184
+ */
185
+ anyOf<B extends readonly JsonSchemaAnyBuilder<any, any, boolean>[], IN = BuilderInUnion<B>, OUT = BuilderOutUnion<B>>(items: [...B]): JsonSchemaAnyBuilder<IN, OUT, false>;
186
+ /**
187
+ * Pick validation schema for an object based on the value of a specific property.
188
+ *
189
+ * ```
190
+ * const schemaMap = {
191
+ * true: successSchema,
192
+ * false: errorSchema
193
+ * }
194
+ *
195
+ * const schema = j.anyOfBy('success', schemaMap)
196
+ * ```
197
+ */
198
+ anyOfBy<P extends string, D extends Record<PropertyKey, JsonSchemaTerminal<any, any, any>>, IN = AnyOfByIn<D>, OUT = AnyOfByOut<D>>(propertyName: P, schemaDictionary: D): JsonSchemaAnyOfByBuilder<IN, OUT, P>;
199
+ /**
200
+ * Custom version of `anyOf` which - in contrast to the original function - does not mutate the input.
201
+ * This comes with a performance penalty, so do not use it where performance matters.
202
+ *
203
+ * ```
204
+ * const schema = j.anyOfThese([successSchema, errorSchema])
205
+ * ```
206
+ */
207
+ anyOfThese<B extends readonly JsonSchemaAnyBuilder<any, any, boolean>[], IN = BuilderInUnion<B>, OUT = BuilderOutUnion<B>>(items: [...B]): JsonSchemaAnyBuilder<IN, OUT, false>;
208
+ and(): {
209
+ silentBob: () => never;
210
+ };
211
+ literal<const V extends string | number | boolean | null>(v: V): JsonSchemaEnumBuilder<V, V, false>;
212
+ };
213
+ export declare class JsonSchemaTerminal<IN, OUT, Opt> {
214
+ protected [HIDDEN_AJV_SCHEMA]: AjvSchema<any, any> | undefined;
215
+ protected schema: JsonSchema;
216
+ constructor(schema: JsonSchema);
217
+ get ajvSchema(): AjvSchema<any, any>;
218
+ getSchema(): JsonSchema;
219
+ /**
220
+ * Produces a "clean schema object" without methods.
221
+ * Same as if it would be JSON.stringified.
222
+ */
223
+ build(): JsonSchema<IN, OUT>;
224
+ clone(): this;
225
+ cloneAndUpdateSchema(schema: Partial<JsonSchema>): this;
226
+ validate(input: any, opt?: AjvValidationOptions<IN>): OUT;
227
+ isValid(input: any, opt?: AjvValidationOptions<IN>): boolean;
228
+ getValidationResult(input: any, opt?: AjvValidationOptions<IN>): ValidationFunctionResult<OUT, AjvValidationError>;
229
+ getValidationFunction(): ValidationFunction<any, OUT, AjvValidationError>;
230
+ /**
231
+ * @experimental
232
+ */
233
+ in: IN;
234
+ out: OUT;
235
+ opt: Opt;
236
+ }
237
+ export declare class JsonSchemaAnyBuilder<IN, OUT, Opt> extends JsonSchemaTerminal<IN, OUT, Opt> {
238
+ protected setErrorMessage(ruleName: string, errorMessage: string | undefined): void;
239
+ /**
240
+ * A helper function that takes a type parameter and compares it with the type inferred from the schema.
241
+ *
242
+ * When the type inferred from the schema differs from the passed-in type,
243
+ * the schema becomes unusable, by turning its type into `never`.
244
+ *
245
+ * ```ts
246
+ * const schemaGood = j.string().isOfType<string>() // ✅
247
+ *
248
+ * const schemaBad = j.string().isOfType<number>() // ❌
249
+ * schemaBad.build() // TypeError: property "build" does not exist on type "never"
250
+ *
251
+ * const result = ajvValidateRequest.body(req, schemaBad) // result will have `unknown` type
252
+ * ```
253
+ */
254
+ isOfType<ExpectedType>(): ExactMatch<ExpectedType, OUT> extends true ? this : never;
255
+ $schema($schema: string): this;
256
+ $schemaDraft7(): this;
257
+ $id($id: string): this;
258
+ title(title: string): this;
259
+ description(description: string): this;
260
+ deprecated(deprecated?: boolean): this;
261
+ type(type: string): this;
262
+ default(v: any): this;
263
+ instanceof(of: string): this;
264
+ optional(): JsonSchemaAnyBuilder<IN | undefined, OUT | undefined, true>;
265
+ nullable(): JsonSchemaAnyBuilder<IN | null, OUT | null, Opt>;
266
+ /**
267
+ * @deprecated
268
+ * The usage of this function is discouraged as it defeats the purpose of having type-safe validation.
269
+ */
270
+ castAs<T>(): JsonSchemaAnyBuilder<T, T, Opt>;
271
+ /**
272
+ * Locks the given schema chain and no other modification can be done to it.
273
+ */
274
+ final(): JsonSchemaTerminal<IN, OUT, Opt>;
275
+ /**
276
+ *
277
+ * @param validator A validator function that returns an error message or undefined.
278
+ *
279
+ * You may add multiple custom validators and they will be executed in the order you added them.
280
+ */
281
+ custom<OUT2 = OUT>(validator: CustomValidatorFn): JsonSchemaAnyBuilder<IN, OUT2, Opt>;
282
+ /**
283
+ *
284
+ * @param converter A converter function that returns a new value.
285
+ *
286
+ * You may add multiple converters and they will be executed in the order you added them,
287
+ * each converter receiving the result from the previous one.
288
+ *
289
+ * This feature only works when the current schema is nested in an object or array schema,
290
+ * due to how mutability works in Ajv.
291
+ */
292
+ convert<OUT2>(converter: CustomConverterFn<OUT2>): JsonSchemaAnyBuilder<IN, OUT2, Opt>;
293
+ }
294
+ export declare class JsonSchemaStringBuilder<IN extends string | undefined = string, OUT = IN, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
295
+ constructor();
296
+ /**
297
+ * @param optionalValues List of values that should be considered/converted as `undefined`.
298
+ *
299
+ * This `optionalValues` feature only works when the current schema is nested in an object or array schema,
300
+ * due to how mutability works in Ajv.
301
+ *
302
+ * Make sure this `optional()` call is at the end of your call chain.
303
+ *
304
+ * When `null` is included in optionalValues, the return type becomes `JsonSchemaTerminal`
305
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
306
+ */
307
+ optional<T extends readonly (string | null)[] | undefined = undefined>(optionalValues?: T): T extends readonly (infer U)[] ? null extends U ? JsonSchemaTerminal<IN | undefined, OUT | undefined, true> : JsonSchemaStringBuilder<IN | undefined, OUT | undefined, true> : JsonSchemaStringBuilder<IN | undefined, OUT | undefined, true>;
308
+ regex(pattern: RegExp, opt?: JsonBuilderRuleOpt): this;
309
+ pattern(pattern: string, opt?: JsonBuilderRuleOpt): this;
310
+ minLength(minLength: number): this;
311
+ maxLength(maxLength: number): this;
312
+ length(exactLength: number): this;
313
+ length(minLength: number, maxLength: number): this;
314
+ email(opt?: Partial<JsonSchemaStringEmailOptions>): this;
315
+ trim(): this;
316
+ toLowerCase(): this;
317
+ toUpperCase(): this;
318
+ truncate(toLength: number): this;
319
+ branded<B extends string>(): JsonSchemaStringBuilder<B, B, Opt>;
320
+ /**
321
+ * Validates that the input is a fully-specified YYYY-MM-DD formatted valid IsoDate value.
322
+ *
323
+ * All previous expectations in the schema chain are dropped - including `.optional()` -
324
+ * because this call effectively starts a new schema chain.
325
+ */
326
+ isoDate(): JsonSchemaIsoDateBuilder;
327
+ isoDateTime(): JsonSchemaStringBuilder<IsoDateTime | IN, IsoDateTime, Opt>;
328
+ isoMonth(): JsonSchemaIsoMonthBuilder;
329
+ /**
330
+ * Validates the string format to be JWT.
331
+ * Expects the JWT to be signed!
332
+ */
333
+ jwt(): this;
334
+ url(): this;
335
+ ipv4(): this;
336
+ ipv6(): this;
337
+ slug(): this;
338
+ semVer(): this;
339
+ languageTag(): this;
340
+ countryCode(): this;
341
+ currency(): this;
342
+ /**
343
+ * Validates that the input is a valid IANATimzone value.
344
+ *
345
+ * All previous expectations in the schema chain are dropped - including `.optional()` -
346
+ * because this call effectively starts a new schema chain as an `enum` validation.
347
+ */
348
+ ianaTimezone(): JsonSchemaEnumBuilder<string | IANATimezone, IANATimezone, false>;
349
+ base64Url(): this;
350
+ uuid(): this;
351
+ }
352
+ export interface JsonSchemaStringEmailOptions {
353
+ checkTLD: boolean;
354
+ }
355
+ export declare class JsonSchemaIsoDateBuilder<Opt extends boolean = false> extends JsonSchemaAnyBuilder<string | IsoDate, IsoDate, Opt> {
356
+ constructor();
357
+ /**
358
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
359
+ *
360
+ * This `null` feature only works when the current schema is nested in an object or array schema,
361
+ * due to how mutability works in Ajv.
362
+ *
363
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
364
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
365
+ */
366
+ optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JsonSchemaTerminal<string | IsoDate | null | undefined, IsoDate | undefined, true> : JsonSchemaAnyBuilder<string | IsoDate | undefined, IsoDate | undefined, true>;
367
+ before(date: string): this;
368
+ sameOrBefore(date: string): this;
369
+ after(date: string): this;
370
+ sameOrAfter(date: string): this;
371
+ between(fromDate: string, toDate: string, incl: Inclusiveness): this;
372
+ }
373
+ export interface JsonSchemaIsoDateOptions {
374
+ before?: string;
375
+ sameOrBefore?: string;
376
+ after?: string;
377
+ sameOrAfter?: string;
378
+ }
379
+ export declare class JsonSchemaIsoMonthBuilder<Opt extends boolean = false> extends JsonSchemaAnyBuilder<string | IsoDate, IsoMonth, Opt> {
380
+ constructor();
381
+ }
382
+ export interface JsonSchemaIsoMonthOptions {
383
+ }
384
+ export declare class JsonSchemaNumberBuilder<IN extends number | undefined = number, OUT = IN, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
385
+ constructor();
386
+ /**
387
+ * @param optionalValues List of values that should be considered/converted as `undefined`.
388
+ *
389
+ * This `optionalValues` feature only works when the current schema is nested in an object or array schema,
390
+ * due to how mutability works in Ajv.
391
+ *
392
+ * Make sure this `optional()` call is at the end of your call chain.
393
+ *
394
+ * When `null` is included in optionalValues, the return type becomes `JsonSchemaTerminal`
395
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
396
+ */
397
+ optional<T extends readonly (number | null)[] | undefined = undefined>(optionalValues?: T): T extends readonly (infer U)[] ? null extends U ? JsonSchemaTerminal<IN | undefined, OUT | undefined, true> : JsonSchemaNumberBuilder<IN | undefined, OUT | undefined, true> : JsonSchemaNumberBuilder<IN | undefined, OUT | undefined, true>;
398
+ integer(): this;
399
+ branded<B extends number>(): JsonSchemaNumberBuilder<B, B, Opt>;
400
+ multipleOf(multipleOf: number): this;
401
+ min(minimum: number): this;
402
+ exclusiveMin(exclusiveMinimum: number): this;
403
+ max(maximum: number): this;
404
+ exclusiveMax(exclusiveMaximum: number): this;
405
+ lessThan(value: number): this;
406
+ lessThanOrEqual(value: number): this;
407
+ moreThan(value: number): this;
408
+ moreThanOrEqual(value: number): this;
409
+ equal(value: number): this;
410
+ range(minimum: number, maximum: number, incl: Inclusiveness): this;
411
+ int32(): this;
412
+ int64(): this;
413
+ float(): this;
414
+ double(): this;
415
+ unixTimestamp(): JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, Opt>;
416
+ unixTimestamp2000(): JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, Opt>;
417
+ unixTimestampMillis(): JsonSchemaNumberBuilder<UnixTimestampMillis, UnixTimestampMillis, Opt>;
418
+ unixTimestamp2000Millis(): JsonSchemaNumberBuilder<UnixTimestampMillis, UnixTimestampMillis, Opt>;
419
+ utcOffset(): this;
420
+ utcOffsetHour(): this;
421
+ /**
422
+ * Specify the precision of the floating point numbers by the number of digits after the ".".
423
+ * Excess digits will be cut-off when the current schema is nested in an object or array schema,
424
+ * due to how mutability works in Ajv.
425
+ */
426
+ precision(numberOfDigits: number): this;
427
+ }
428
+ export declare class JsonSchemaBooleanBuilder<IN extends boolean | undefined = boolean, OUT = IN, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
429
+ constructor();
430
+ /**
431
+ * @param optionalValue One of the two possible boolean values that should be considered/converted as `undefined`.
432
+ *
433
+ * This `optionalValue` feature only works when the current schema is nested in an object or array schema,
434
+ * due to how mutability works in Ajv.
435
+ */
436
+ optional(optionalValue?: boolean): JsonSchemaBooleanBuilder<IN | undefined, OUT | undefined, true>;
437
+ }
438
+ export declare class JsonSchemaObjectBuilder<IN extends AnyObject, OUT extends AnyObject, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
439
+ constructor(props?: AnyObject, opt?: JsonSchemaObjectBuilderOpts);
440
+ addProperties(props: AnyObject): this;
441
+ /**
442
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
443
+ *
444
+ * This `null` feature only works when the current schema is nested in an object or array schema,
445
+ * due to how mutability works in Ajv.
446
+ *
447
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
448
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
449
+ */
450
+ optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JsonSchemaTerminal<IN | null | undefined, OUT | undefined, true> : JsonSchemaAnyBuilder<IN | undefined, OUT | undefined, true>;
451
+ /**
452
+ * When set, the validation will not strip away properties that are not specified explicitly in the schema.
453
+ */
454
+ allowAdditionalProperties(): this;
455
+ extend<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectBuilder<RelaxIndexSignature<OptionalDbEntityFields<Override<IN, {
456
+ [K in keyof P]?: P[K] extends JsonSchemaAnyBuilder<infer IN2, any, any> ? IN2 : never;
457
+ }>>>, Override<OUT, {
458
+ [K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: P[K] extends JsonSchemaAnyBuilder<any, infer OUT2, any> ? OUT2 : never;
459
+ } & {
460
+ [K in keyof P as P[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: P[K] extends JsonSchemaAnyBuilder<any, infer OUT2, any> ? OUT2 : never;
461
+ }>, false>;
462
+ /**
463
+ * Concatenates another schema to the current schema.
464
+ *
465
+ * It expects you to use `isOfType<T>()` in the chain,
466
+ * otherwise the validation will throw. This is to ensure
467
+ * that the schemas you concatenated match the intended final type.
468
+ *
469
+ * ```ts
470
+ * interface Foo { foo: string }
471
+ * const fooSchema = j.object<Foo>({ foo: j.string() })
472
+ *
473
+ * interface Bar { bar: number }
474
+ * const barSchema = j.object<Bar>({ bar: j.number() })
475
+ *
476
+ * interface Shu { foo: string, bar: number }
477
+ * const shuSchema = fooSchema.concat(barSchema).isOfType<Shu>() // important
478
+ * ```
479
+ */
480
+ concat<IN2 extends AnyObject, OUT2 extends AnyObject>(other: JsonSchemaObjectBuilder<IN2, OUT2, any>): JsonSchemaObjectBuilder<IN & IN2, OUT & OUT2, false>;
481
+ /**
482
+ * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
483
+ */
484
+ dbEntity(): JsonSchemaObjectBuilder<RelaxIndexSignature<OptionalDbEntityFields<Override<IN, {
485
+ id?: string | undefined;
486
+ created?: any;
487
+ updated?: any;
488
+ }>>>, Override<OUT, {
489
+ id: string;
490
+ created: any;
491
+ updated: any;
492
+ } & {}>, false>;
493
+ minProperties(minProperties: number): this;
494
+ maxProperties(maxProperties: number): this;
495
+ exclusiveProperties(propNames: readonly (keyof IN & string)[]): this;
496
+ }
497
+ interface JsonSchemaObjectBuilderOpts {
498
+ hasIsOfTypeCheck?: false;
499
+ patternProperties?: StringMap<JsonSchema<any, any>>;
500
+ keySchema?: JsonSchema;
501
+ }
502
+ export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>, Opt extends boolean = false> extends JsonSchemaAnyBuilder<Expand<{
503
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
504
+ } & {
505
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
506
+ }>, Expand<{
507
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
508
+ } & {
509
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
510
+ }>, Opt> {
511
+ constructor(props?: PROPS);
512
+ addProperties(props: PROPS): this;
513
+ /**
514
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
515
+ *
516
+ * This `null` feature only works when the current schema is nested in an object or array schema,
517
+ * due to how mutability works in Ajv.
518
+ *
519
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
520
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
521
+ */
522
+ optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JsonSchemaTerminal<Expand<{
523
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
524
+ } & {
525
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
526
+ }> | undefined, Expand<{
527
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
528
+ } & {
529
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
530
+ }> | undefined, true> : JsonSchemaAnyBuilder<Expand<{
531
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
532
+ } & {
533
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
534
+ }> | undefined, Expand<{
535
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? never : K : never]: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
536
+ } & {
537
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt> ? IsOpt extends true ? K : never : never]?: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
538
+ }> | undefined, true>;
539
+ /**
540
+ * When set, the validation will not strip away properties that are not specified explicitly in the schema.
541
+ */
542
+ allowAdditionalProperties(): this;
543
+ extend<NEW_PROPS extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: NEW_PROPS): JsonSchemaObjectInferringBuilder<{
544
+ [K in keyof PROPS | keyof NEW_PROPS]: K extends keyof NEW_PROPS ? NEW_PROPS[K] : K extends keyof PROPS ? PROPS[K] : never;
545
+ }, Opt>;
546
+ /**
547
+ * Extends the current schema with `id`, `created` and `updated` according to NC DB conventions.
548
+ */
549
+ dbEntity(): JsonSchemaObjectInferringBuilder<{ [K in "created" | "id" | "updated" | keyof PROPS]: K extends "created" | "id" | "updated" ? {
550
+ id: JsonSchemaStringBuilder<string, string, false>;
551
+ created: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
552
+ updated: JsonSchemaNumberBuilder<UnixTimestamp, UnixTimestamp, false>;
553
+ }[K] : K extends keyof PROPS ? PROPS[K] : never; }, Opt>;
554
+ }
555
+ export declare class JsonSchemaArrayBuilder<IN, OUT, Opt> extends JsonSchemaAnyBuilder<IN[], OUT[], Opt> {
556
+ constructor(itemsSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>);
557
+ minLength(minItems: number): this;
558
+ maxLength(maxItems: number): this;
559
+ length(exactLength: number): this;
560
+ length(minItems: number, maxItems: number): this;
561
+ exactLength(length: number): this;
562
+ unique(): this;
563
+ }
564
+ export declare class JsonSchemaSet2Builder<IN, OUT, Opt> extends JsonSchemaAnyBuilder<Iterable<IN>, Set2<OUT>, Opt> {
565
+ constructor(itemsSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>);
566
+ min(minItems: number): this;
567
+ max(maxItems: number): this;
568
+ }
569
+ export declare class JsonSchemaBufferBuilder extends JsonSchemaAnyBuilder<string | any[] | ArrayBuffer | Buffer, Buffer, false> {
570
+ constructor();
571
+ }
572
+ export declare class JsonSchemaEnumBuilder<IN extends string | number | boolean | null, OUT extends IN = IN, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
573
+ constructor(enumValues: readonly IN[], baseType: EnumBaseType, opt?: JsonBuilderRuleOpt);
574
+ branded<B extends IN>(): JsonSchemaEnumBuilder<B | IN, B, Opt>;
575
+ }
576
+ export declare class JsonSchemaTupleBuilder<ITEMS extends JsonSchemaAnyBuilder<any, any, any>[]> extends JsonSchemaAnyBuilder<TupleIn<ITEMS>, TupleOut<ITEMS>, false> {
577
+ private readonly _items;
578
+ constructor(items: ITEMS);
579
+ }
580
+ export declare class JsonSchemaAnyOfByBuilder<IN, OUT, _P extends string = string> extends JsonSchemaAnyBuilder<AnyOfByInput<IN, _P> | IN, OUT, false> {
581
+ in: IN;
582
+ constructor(propertyName: string, schemaDictionary: Record<PropertyKey, JsonSchemaTerminal<any, any, any>>);
583
+ }
584
+ export declare class JsonSchemaAnyOfTheseBuilder<IN, OUT, _P extends string = string> extends JsonSchemaAnyBuilder<AnyOfByInput<IN, _P> | IN, OUT, false> {
585
+ in: IN;
586
+ constructor(propertyName: string, schemaDictionary: Record<PropertyKey, JsonSchemaTerminal<any, any, any>>);
587
+ }
588
+ type EnumBaseType = 'string' | 'number' | 'other';
589
+ export interface JsonSchema<IN = unknown, OUT = IN> {
590
+ readonly in?: IN;
591
+ readonly out?: OUT;
592
+ $schema?: string;
593
+ $id?: string;
594
+ title?: string;
595
+ description?: string;
596
+ deprecated?: boolean;
597
+ readOnly?: boolean;
598
+ writeOnly?: boolean;
599
+ type?: string | string[];
600
+ items?: JsonSchema;
601
+ prefixItems?: JsonSchema[];
602
+ properties?: {
603
+ [K in keyof IN & keyof OUT]: JsonSchema<IN[K], OUT[K]>;
604
+ };
605
+ patternProperties?: StringMap<JsonSchema<any, any>>;
606
+ required?: string[];
607
+ additionalProperties?: boolean;
608
+ minProperties?: number;
609
+ maxProperties?: number;
610
+ default?: IN;
611
+ if?: JsonSchema;
612
+ then?: JsonSchema;
613
+ else?: JsonSchema;
614
+ anyOf?: JsonSchema[];
615
+ oneOf?: JsonSchema[];
616
+ /**
617
+ * This is a temporary "intermediate AST" field that is used inside the parser.
618
+ * In the final schema this field will NOT be present.
619
+ */
620
+ optionalField?: true;
621
+ pattern?: string;
622
+ minLength?: number;
623
+ maxLength?: number;
624
+ format?: string;
625
+ contentMediaType?: string;
626
+ contentEncoding?: string;
627
+ multipleOf?: number;
628
+ minimum?: number;
629
+ exclusiveMinimum?: number;
630
+ maximum?: number;
631
+ exclusiveMaximum?: number;
632
+ minItems?: number;
633
+ maxItems?: number;
634
+ uniqueItems?: boolean;
635
+ enum?: any;
636
+ hasIsOfTypeCheck?: boolean;
637
+ email?: JsonSchemaStringEmailOptions;
638
+ Set2?: JsonSchema;
639
+ Buffer?: true;
640
+ IsoDate?: JsonSchemaIsoDateOptions;
641
+ IsoDateTime?: true;
642
+ IsoMonth?: JsonSchemaIsoMonthOptions;
643
+ instanceof?: string | string[];
644
+ transform?: {
645
+ trim?: true;
646
+ toLowerCase?: true;
647
+ toUpperCase?: true;
648
+ truncate?: number;
649
+ };
650
+ errorMessages?: StringMap<string>;
651
+ optionalValues?: (string | number | boolean | null)[];
652
+ keySchema?: JsonSchema;
653
+ isUndefined?: true;
654
+ minProperties2?: number;
655
+ exclusiveProperties?: (readonly string[])[];
656
+ anyOfBy?: {
657
+ propertyName: string;
658
+ schemaDictionary: Record<string, JsonSchema>;
659
+ };
660
+ anyOfThese?: JsonSchema[];
661
+ precision?: number;
662
+ customValidations?: CustomValidatorFn[];
663
+ customConversions?: CustomConverterFn<any>[];
664
+ }
665
+ declare function object(props: AnyObject): never;
666
+ declare function object<IN extends AnyObject>(props: {
667
+ [K in keyof Required<IN>]-?: JsonSchemaTerminal<any, IN[K], any>;
668
+ }): JsonSchemaObjectBuilder<IN, IN, false>;
669
+ declare function objectInfer<P extends Record<string, JsonSchemaAnyBuilder<any, any, any>>>(props: P): JsonSchemaObjectInferringBuilder<P, false>;
670
+ declare function objectDbEntity(props: AnyObject): never;
671
+ declare function objectDbEntity<IN extends BaseDBEntity, EXTRA_KEYS extends Exclude<keyof IN, keyof BaseDBEntity> = Exclude<keyof IN, keyof BaseDBEntity>>(props: {
672
+ [K in EXTRA_KEYS]-?: BuilderFor<IN[K]>;
673
+ } & (ExactMatch<IN['id'], BaseDBEntity['id']> extends true ? {
674
+ id?: BuilderFor<BaseDBEntity['id']>;
675
+ } : {
676
+ id: BuilderFor<IN['id']>;
677
+ }) & (ExactMatch<IN['created'], BaseDBEntity['created']> extends true ? {
678
+ created?: BuilderFor<BaseDBEntity['created']>;
679
+ } : {
680
+ created: BuilderFor<IN['created']>;
681
+ }) & (ExactMatch<IN['updated'], BaseDBEntity['updated']> extends true ? {
682
+ updated?: BuilderFor<BaseDBEntity['updated']>;
683
+ } : {
684
+ updated: BuilderFor<IN['updated']>;
685
+ })): JsonSchemaObjectBuilder<IN, IN, false>;
686
+ declare function record<KS extends JsonSchemaAnyBuilder<any, any, any>, VS extends JsonSchemaAnyBuilder<any, any, any>, Opt extends boolean = SchemaOpt<VS>>(keySchema: KS, valueSchema: VS): JsonSchemaObjectBuilder<Opt extends true ? Partial<Record<SchemaIn<KS>, SchemaIn<VS>>> : Record<SchemaIn<KS>, SchemaIn<VS>>, Opt extends true ? Partial<Record<SchemaOut<KS>, SchemaOut<VS>>> : Record<SchemaOut<KS>, SchemaOut<VS>>, false>;
687
+ declare function withRegexKeys<S extends JsonSchemaAnyBuilder<any, any, any>, Opt extends boolean = SchemaOpt<S>>(keyRegex: RegExp | string, schema: S): JsonSchemaObjectBuilder<Opt extends true ? StringMap<SchemaIn<S>> : StringMap<SchemaIn<S>>, Opt extends true ? StringMap<SchemaOut<S>> : 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, any>, K extends string | number = EnumKeyUnion<T>, Opt extends boolean = SchemaOpt<S>>(keys: T, schema: S): JsonSchemaObjectBuilder<Opt extends true ? {
692
+ [P in K]?: SchemaIn<S>;
693
+ } : {
694
+ [P in K]: SchemaIn<S>;
695
+ }, Opt extends true ? {
696
+ [P in K]?: SchemaOut<S>;
697
+ } : {
698
+ [P in K]: SchemaOut<S>;
699
+ }, false>;
700
+ type Expand<T> = {
701
+ [K in keyof T]: T[K];
702
+ };
703
+ type StripIndexSignatureDeep<T> = T extends readonly unknown[] ? T : T extends Record<string, any> ? {
704
+ [K in keyof T as string extends K ? never : number extends K ? never : symbol extends K ? never : K]: StripIndexSignatureDeep<T[K]>;
705
+ } : T;
706
+ type RelaxIndexSignature<T> = T extends readonly unknown[] ? T : T extends AnyObject ? {
707
+ [K in keyof T]: RelaxIndexSignature<T[K]>;
708
+ } : T;
709
+ type Override<T, U> = Omit<T, keyof U> & U;
710
+ declare const allowExtraKeysSymbol: unique symbol;
711
+ type HasAllowExtraKeys<T> = T extends {
712
+ readonly [allowExtraKeysSymbol]?: true;
713
+ } ? true : false;
714
+ type IsAny<T> = 0 extends 1 & T ? true : false;
715
+ type IsAssignableRelaxed<A, B> = IsAny<RelaxIndexSignature<A>> extends true ? true : [RelaxIndexSignature<A>] extends [B] ? true : false;
716
+ type OptionalDbEntityFields<T> = T extends BaseDBEntity ? Omit<T, keyof BaseDBEntity> & Partial<Pick<T, keyof BaseDBEntity>> : T;
717
+ 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;
718
+ 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>>>;
719
+ type BuilderOutUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> = {
720
+ [K in keyof B]: B[K] extends JsonSchemaAnyBuilder<any, infer O, any> ? O : never;
721
+ }[number];
722
+ type BuilderInUnion<B extends readonly JsonSchemaAnyBuilder<any, any, any>[]> = {
723
+ [K in keyof B]: B[K] extends JsonSchemaAnyBuilder<infer I, any, any> ? I : never;
724
+ }[number];
725
+ type AnyOfByIn<D extends Record<PropertyKey, JsonSchemaTerminal<any, any, any>>> = {
726
+ [K in keyof D]: D[K] extends JsonSchemaTerminal<infer I, any, any> ? I : never;
727
+ }[keyof D];
728
+ type AnyOfByOut<D extends Record<PropertyKey, JsonSchemaTerminal<any, any, any>>> = {
729
+ [K in keyof D]: D[K] extends JsonSchemaTerminal<any, infer O, any> ? O : never;
730
+ }[keyof D];
731
+ type AnyOfByDiscriminant<IN, P extends string> = IN extends {
732
+ [K in P]: infer V;
733
+ } ? V : never;
734
+ type AnyOfByInput<IN, P extends string, D = AnyOfByDiscriminant<IN, P>> = IN extends unknown ? Omit<Partial<IN>, P> & {
735
+ [K in P]?: D;
736
+ } : never;
737
+ type BuilderFor<T> = JsonSchemaAnyBuilder<any, T, any>;
738
+ interface JsonBuilderRuleOpt {
739
+ /**
740
+ * Text of error message to return when the validation fails for the given rule:
741
+ *
742
+ * `{ msg: "is not a valid Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
743
+ */
744
+ msg?: string;
745
+ /**
746
+ * A friendly name for what we are validating, that will be used in error messages:
747
+ *
748
+ * `{ name: "Oompa-loompa" } => "Object.property is not a valid Oompa-loompa"`
749
+ */
750
+ name?: string;
751
+ }
752
+ type EnumKeyUnion<T> = T extends readonly (infer U)[] ? U : T extends StringEnum | NumberEnum ? T[keyof T] : never;
753
+ type SchemaIn<S> = S extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
754
+ type SchemaOut<S> = S extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
755
+ type SchemaOpt<S> = S extends JsonSchemaAnyBuilder<any, any, infer Opt> ? (Opt extends true ? true : false) : false;
756
+ type TupleIn<T extends readonly JsonSchemaAnyBuilder<any, any, any>[]> = {
757
+ [K in keyof T]: T[K] extends JsonSchemaAnyBuilder<infer I, any, any> ? I : never;
758
+ };
759
+ type TupleOut<T extends readonly JsonSchemaAnyBuilder<any, any, any>[]> = {
760
+ [K in keyof T]: T[K] extends JsonSchemaAnyBuilder<any, infer O, any> ? O : never;
761
+ };
762
+ export type CustomValidatorFn = (v: any) => string | undefined;
763
+ export type CustomConverterFn<OUT> = (v: any) => OUT;
764
+ export {};