@naturalcycles/nodejs-lib 15.55.0 → 15.56.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -9,10 +9,7 @@ export declare const j: {
|
|
|
9
9
|
infer: typeof objectInfer;
|
|
10
10
|
any(): JsonSchemaObjectBuilder<AnyObject, AnyObject, false>;
|
|
11
11
|
stringMap<S extends JsonSchemaTerminal<any, any, any>>(schema: S): JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>>;
|
|
12
|
-
|
|
13
|
-
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
14
|
-
*/
|
|
15
|
-
withEnumKeys<const T extends readonly (string | number)[] | StringEnum | NumberEnum, S extends JsonSchemaTerminal<any, any, any>, K extends string | number = EnumKeyUnion<T>>(keys: T, schema: S): JsonSchemaObjectBuilder<Record<K, SchemaIn<S>>, Record<K, SchemaOut<S>>>;
|
|
12
|
+
withEnumKeys: typeof withEnumKeys;
|
|
16
13
|
};
|
|
17
14
|
array<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaArrayBuilder<IN, OUT, Opt>;
|
|
18
15
|
set<IN, OUT, Opt>(itemSchema: JsonSchemaAnyBuilder<IN, OUT, Opt>): JsonSchemaSet2Builder<IN, OUT, Opt>;
|
|
@@ -337,6 +334,18 @@ declare function objectDbEntity<IN extends BaseDBEntity & AnyObject, EXTRA_KEYS
|
|
|
337
334
|
} : {
|
|
338
335
|
updated: BuilderFor<IN['updated']>;
|
|
339
336
|
})): JsonSchemaObjectBuilder<IN, IN, false>;
|
|
337
|
+
/**
|
|
338
|
+
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
339
|
+
*/
|
|
340
|
+
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 ? {
|
|
341
|
+
[P in K]?: SchemaIn<S>;
|
|
342
|
+
} : {
|
|
343
|
+
[P in K]: SchemaIn<S>;
|
|
344
|
+
}, Opt extends true ? {
|
|
345
|
+
[P in K]?: SchemaOut<S>;
|
|
346
|
+
} : {
|
|
347
|
+
[P in K]: SchemaOut<S>;
|
|
348
|
+
}, false>;
|
|
340
349
|
type Expand<T> = T extends infer O ? {
|
|
341
350
|
[K in keyof O]: O[K];
|
|
342
351
|
} : never;
|
|
@@ -365,4 +374,5 @@ interface JsonBuilderRuleOpt {
|
|
|
365
374
|
type EnumKeyUnion<T> = T extends readonly (infer U)[] ? U : T extends StringEnum | NumberEnum ? T[keyof T] : never;
|
|
366
375
|
type SchemaIn<S> = S extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never;
|
|
367
376
|
type SchemaOut<S> = S extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never;
|
|
377
|
+
type SchemaOpt<S> = S extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt : false;
|
|
368
378
|
export {};
|
|
@@ -35,30 +35,7 @@ export const j = {
|
|
|
35
35
|
},
|
|
36
36
|
});
|
|
37
37
|
},
|
|
38
|
-
|
|
39
|
-
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
40
|
-
*/
|
|
41
|
-
withEnumKeys(keys, schema) {
|
|
42
|
-
let enumValues;
|
|
43
|
-
if (Array.isArray(keys)) {
|
|
44
|
-
_assert(isEveryItemPrimitive(keys), 'Every item in the key list should be string, number or symbol');
|
|
45
|
-
enumValues = keys;
|
|
46
|
-
}
|
|
47
|
-
else if (typeof keys === 'object') {
|
|
48
|
-
const enumType = getEnumType(keys);
|
|
49
|
-
_assert(enumType === 'NumberEnum' || enumType === 'StringEnum', 'The key list should be StringEnum or NumberEnum');
|
|
50
|
-
if (enumType === 'NumberEnum') {
|
|
51
|
-
enumValues = _numberEnumValues(keys);
|
|
52
|
-
}
|
|
53
|
-
else if (enumType === 'StringEnum') {
|
|
54
|
-
enumValues = _stringEnumValues(keys);
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
_assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum');
|
|
58
|
-
const typedValues = enumValues;
|
|
59
|
-
const props = Object.fromEntries(typedValues.map(key => [key, schema]));
|
|
60
|
-
return new JsonSchemaObjectBuilder(props, { hasIsOfTypeCheck: false });
|
|
61
|
-
},
|
|
38
|
+
withEnumKeys,
|
|
62
39
|
}),
|
|
63
40
|
array(itemSchema) {
|
|
64
41
|
return new JsonSchemaArrayBuilder(itemSchema);
|
|
@@ -241,8 +218,16 @@ export class JsonSchemaStringBuilder extends JsonSchemaAnyBuilder {
|
|
|
241
218
|
* due to how mutability works in Ajv.
|
|
242
219
|
*/
|
|
243
220
|
optional(optionalValues) {
|
|
244
|
-
|
|
245
|
-
|
|
221
|
+
if (!optionalValues) {
|
|
222
|
+
return super.optional();
|
|
223
|
+
}
|
|
224
|
+
const newBuilder = new JsonSchemaStringBuilder().optional();
|
|
225
|
+
const alternativesSchema = j.enum(optionalValues);
|
|
226
|
+
Object.assign(newBuilder.getSchema(), {
|
|
227
|
+
anyOf: [this.build(), alternativesSchema.build()],
|
|
228
|
+
optionalValues,
|
|
229
|
+
});
|
|
230
|
+
return newBuilder;
|
|
246
231
|
}
|
|
247
232
|
regex(pattern, opt) {
|
|
248
233
|
return this.pattern(pattern.source, opt);
|
|
@@ -403,8 +388,16 @@ export class JsonSchemaNumberBuilder extends JsonSchemaAnyBuilder {
|
|
|
403
388
|
* due to how mutability works in Ajv.
|
|
404
389
|
*/
|
|
405
390
|
optional(optionalValues) {
|
|
406
|
-
|
|
407
|
-
|
|
391
|
+
if (!optionalValues) {
|
|
392
|
+
return super.optional();
|
|
393
|
+
}
|
|
394
|
+
const newBuilder = new JsonSchemaNumberBuilder().optional();
|
|
395
|
+
const alternativesSchema = j.enum(optionalValues);
|
|
396
|
+
Object.assign(newBuilder.getSchema(), {
|
|
397
|
+
anyOf: [this.build(), alternativesSchema.build()],
|
|
398
|
+
optionalValues,
|
|
399
|
+
});
|
|
400
|
+
return newBuilder;
|
|
408
401
|
}
|
|
409
402
|
integer() {
|
|
410
403
|
_objectAssign(this.schema, { type: 'integer' });
|
|
@@ -511,10 +504,16 @@ export class JsonSchemaBooleanBuilder extends JsonSchemaAnyBuilder {
|
|
|
511
504
|
* due to how mutability works in Ajv.
|
|
512
505
|
*/
|
|
513
506
|
optional(optionalValue) {
|
|
514
|
-
if (typeof optionalValue
|
|
515
|
-
|
|
507
|
+
if (typeof optionalValue === 'undefined') {
|
|
508
|
+
return super.optional();
|
|
516
509
|
}
|
|
517
|
-
|
|
510
|
+
const newBuilder = new JsonSchemaBooleanBuilder().optional();
|
|
511
|
+
const alternativesSchema = j.enum([optionalValue]);
|
|
512
|
+
Object.assign(newBuilder.getSchema(), {
|
|
513
|
+
anyOf: [this.build(), alternativesSchema.build()],
|
|
514
|
+
optionalValues: [optionalValue],
|
|
515
|
+
});
|
|
516
|
+
return newBuilder;
|
|
518
517
|
}
|
|
519
518
|
}
|
|
520
519
|
export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
|
|
@@ -717,3 +716,27 @@ function objectDbEntity(props) {
|
|
|
717
716
|
...props,
|
|
718
717
|
});
|
|
719
718
|
}
|
|
719
|
+
/**
|
|
720
|
+
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
721
|
+
*/
|
|
722
|
+
function withEnumKeys(keys, schema) {
|
|
723
|
+
let enumValues;
|
|
724
|
+
if (Array.isArray(keys)) {
|
|
725
|
+
_assert(isEveryItemPrimitive(keys), 'Every item in the key list should be string, number or symbol');
|
|
726
|
+
enumValues = keys;
|
|
727
|
+
}
|
|
728
|
+
else if (typeof keys === 'object') {
|
|
729
|
+
const enumType = getEnumType(keys);
|
|
730
|
+
_assert(enumType === 'NumberEnum' || enumType === 'StringEnum', 'The key list should be StringEnum or NumberEnum');
|
|
731
|
+
if (enumType === 'NumberEnum') {
|
|
732
|
+
enumValues = _numberEnumValues(keys);
|
|
733
|
+
}
|
|
734
|
+
else if (enumType === 'StringEnum') {
|
|
735
|
+
enumValues = _stringEnumValues(keys);
|
|
736
|
+
}
|
|
737
|
+
}
|
|
738
|
+
_assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum');
|
|
739
|
+
const typedValues = enumValues;
|
|
740
|
+
const props = Object.fromEntries(typedValues.map(key => [key, schema]));
|
|
741
|
+
return new JsonSchemaObjectBuilder(props, { hasIsOfTypeCheck: false });
|
|
742
|
+
}
|
package/package.json
CHANGED
|
@@ -87,47 +87,7 @@ export const j = {
|
|
|
87
87
|
)
|
|
88
88
|
},
|
|
89
89
|
|
|
90
|
-
|
|
91
|
-
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
92
|
-
*/
|
|
93
|
-
withEnumKeys<
|
|
94
|
-
const T extends readonly (string | number)[] | StringEnum | NumberEnum,
|
|
95
|
-
S extends JsonSchemaTerminal<any, any, any>,
|
|
96
|
-
K extends string | number = EnumKeyUnion<T>,
|
|
97
|
-
>(
|
|
98
|
-
keys: T,
|
|
99
|
-
schema: S,
|
|
100
|
-
): JsonSchemaObjectBuilder<Record<K, SchemaIn<S>>, Record<K, SchemaOut<S>>> {
|
|
101
|
-
let enumValues: readonly (string | number)[] | undefined
|
|
102
|
-
if (Array.isArray(keys)) {
|
|
103
|
-
_assert(
|
|
104
|
-
isEveryItemPrimitive(keys),
|
|
105
|
-
'Every item in the key list should be string, number or symbol',
|
|
106
|
-
)
|
|
107
|
-
enumValues = keys
|
|
108
|
-
} else if (typeof keys === 'object') {
|
|
109
|
-
const enumType = getEnumType(keys)
|
|
110
|
-
_assert(
|
|
111
|
-
enumType === 'NumberEnum' || enumType === 'StringEnum',
|
|
112
|
-
'The key list should be StringEnum or NumberEnum',
|
|
113
|
-
)
|
|
114
|
-
if (enumType === 'NumberEnum') {
|
|
115
|
-
enumValues = _numberEnumValues(keys as NumberEnum)
|
|
116
|
-
} else if (enumType === 'StringEnum') {
|
|
117
|
-
enumValues = _stringEnumValues(keys as StringEnum)
|
|
118
|
-
}
|
|
119
|
-
}
|
|
120
|
-
|
|
121
|
-
_assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum')
|
|
122
|
-
|
|
123
|
-
const typedValues = enumValues as readonly K[]
|
|
124
|
-
const props = Object.fromEntries(typedValues.map(key => [key, schema])) as any
|
|
125
|
-
|
|
126
|
-
return new JsonSchemaObjectBuilder<Record<K, SchemaIn<S>>, Record<K, SchemaOut<S>>, false>(
|
|
127
|
-
props,
|
|
128
|
-
{ hasIsOfTypeCheck: false },
|
|
129
|
-
)
|
|
130
|
-
},
|
|
90
|
+
withEnumKeys,
|
|
131
91
|
}),
|
|
132
92
|
|
|
133
93
|
array<IN, OUT, Opt>(
|
|
@@ -365,12 +325,22 @@ export class JsonSchemaStringBuilder<
|
|
|
365
325
|
override optional(
|
|
366
326
|
optionalValues?: string[],
|
|
367
327
|
): JsonSchemaStringBuilder<IN | undefined, OUT | undefined, true> {
|
|
368
|
-
|
|
369
|
-
|
|
370
|
-
|
|
371
|
-
|
|
372
|
-
|
|
373
|
-
|
|
328
|
+
if (!optionalValues) {
|
|
329
|
+
return super.optional() as unknown as JsonSchemaStringBuilder<
|
|
330
|
+
IN | undefined,
|
|
331
|
+
OUT | undefined,
|
|
332
|
+
true
|
|
333
|
+
>
|
|
334
|
+
}
|
|
335
|
+
|
|
336
|
+
const newBuilder = new JsonSchemaStringBuilder<IN, OUT, Opt>().optional()
|
|
337
|
+
const alternativesSchema = j.enum(optionalValues)
|
|
338
|
+
Object.assign(newBuilder.getSchema(), {
|
|
339
|
+
anyOf: [this.build(), alternativesSchema.build()],
|
|
340
|
+
optionalValues,
|
|
341
|
+
})
|
|
342
|
+
|
|
343
|
+
return newBuilder
|
|
374
344
|
}
|
|
375
345
|
|
|
376
346
|
regex(pattern: RegExp, opt?: JsonBuilderRuleOpt): this {
|
|
@@ -584,12 +554,22 @@ export class JsonSchemaNumberBuilder<
|
|
|
584
554
|
override optional(
|
|
585
555
|
optionalValues?: number[],
|
|
586
556
|
): JsonSchemaNumberBuilder<IN | undefined, OUT | undefined, true> {
|
|
587
|
-
|
|
588
|
-
|
|
589
|
-
|
|
590
|
-
|
|
591
|
-
|
|
592
|
-
|
|
557
|
+
if (!optionalValues) {
|
|
558
|
+
return super.optional() as unknown as JsonSchemaNumberBuilder<
|
|
559
|
+
IN | undefined,
|
|
560
|
+
OUT | undefined,
|
|
561
|
+
true
|
|
562
|
+
>
|
|
563
|
+
}
|
|
564
|
+
|
|
565
|
+
const newBuilder = new JsonSchemaNumberBuilder<IN, OUT, Opt>().optional()
|
|
566
|
+
const alternativesSchema = j.enum(optionalValues)
|
|
567
|
+
Object.assign(newBuilder.getSchema(), {
|
|
568
|
+
anyOf: [this.build(), alternativesSchema.build()],
|
|
569
|
+
optionalValues,
|
|
570
|
+
})
|
|
571
|
+
|
|
572
|
+
return newBuilder
|
|
593
573
|
}
|
|
594
574
|
|
|
595
575
|
integer(): this {
|
|
@@ -731,15 +711,22 @@ export class JsonSchemaBooleanBuilder<
|
|
|
731
711
|
override optional(
|
|
732
712
|
optionalValue?: boolean,
|
|
733
713
|
): JsonSchemaBooleanBuilder<IN | undefined, OUT | undefined, true> {
|
|
734
|
-
if (typeof optionalValue
|
|
735
|
-
|
|
714
|
+
if (typeof optionalValue === 'undefined') {
|
|
715
|
+
return super.optional() as unknown as JsonSchemaBooleanBuilder<
|
|
716
|
+
IN | undefined,
|
|
717
|
+
OUT | undefined,
|
|
718
|
+
true
|
|
719
|
+
>
|
|
736
720
|
}
|
|
737
721
|
|
|
738
|
-
|
|
739
|
-
|
|
740
|
-
|
|
741
|
-
|
|
742
|
-
|
|
722
|
+
const newBuilder = new JsonSchemaBooleanBuilder<IN, OUT, Opt>().optional()
|
|
723
|
+
const alternativesSchema = j.enum([optionalValue])
|
|
724
|
+
Object.assign(newBuilder.getSchema(), {
|
|
725
|
+
anyOf: [this.build(), alternativesSchema.build()],
|
|
726
|
+
optionalValues: [optionalValue],
|
|
727
|
+
})
|
|
728
|
+
|
|
729
|
+
return newBuilder
|
|
743
730
|
}
|
|
744
731
|
}
|
|
745
732
|
|
|
@@ -1160,6 +1147,54 @@ function objectDbEntity(props: AnyObject): any {
|
|
|
1160
1147
|
})
|
|
1161
1148
|
}
|
|
1162
1149
|
|
|
1150
|
+
/**
|
|
1151
|
+
* Builds the object schema with the indicated `keys` and uses the `schema` for their validation.
|
|
1152
|
+
*/
|
|
1153
|
+
function withEnumKeys<
|
|
1154
|
+
const T extends readonly (string | number)[] | StringEnum | NumberEnum,
|
|
1155
|
+
S extends JsonSchemaAnyBuilder<any, any, any>,
|
|
1156
|
+
K extends string | number = EnumKeyUnion<T>,
|
|
1157
|
+
Opt extends boolean = SchemaOpt<S>,
|
|
1158
|
+
>(
|
|
1159
|
+
keys: T,
|
|
1160
|
+
schema: S,
|
|
1161
|
+
): JsonSchemaObjectBuilder<
|
|
1162
|
+
Opt extends true ? { [P in K]?: SchemaIn<S> } : { [P in K]: SchemaIn<S> },
|
|
1163
|
+
Opt extends true ? { [P in K]?: SchemaOut<S> } : { [P in K]: SchemaOut<S> },
|
|
1164
|
+
false
|
|
1165
|
+
> {
|
|
1166
|
+
let enumValues: readonly (string | number)[] | undefined
|
|
1167
|
+
if (Array.isArray(keys)) {
|
|
1168
|
+
_assert(
|
|
1169
|
+
isEveryItemPrimitive(keys),
|
|
1170
|
+
'Every item in the key list should be string, number or symbol',
|
|
1171
|
+
)
|
|
1172
|
+
enumValues = keys
|
|
1173
|
+
} else if (typeof keys === 'object') {
|
|
1174
|
+
const enumType = getEnumType(keys)
|
|
1175
|
+
_assert(
|
|
1176
|
+
enumType === 'NumberEnum' || enumType === 'StringEnum',
|
|
1177
|
+
'The key list should be StringEnum or NumberEnum',
|
|
1178
|
+
)
|
|
1179
|
+
if (enumType === 'NumberEnum') {
|
|
1180
|
+
enumValues = _numberEnumValues(keys as NumberEnum)
|
|
1181
|
+
} else if (enumType === 'StringEnum') {
|
|
1182
|
+
enumValues = _stringEnumValues(keys as StringEnum)
|
|
1183
|
+
}
|
|
1184
|
+
}
|
|
1185
|
+
|
|
1186
|
+
_assert(enumValues, 'The key list should be an array of values, NumberEnum or a StringEnum')
|
|
1187
|
+
|
|
1188
|
+
const typedValues = enumValues as readonly K[]
|
|
1189
|
+
const props = Object.fromEntries(typedValues.map(key => [key, schema])) as any
|
|
1190
|
+
|
|
1191
|
+
return new JsonSchemaObjectBuilder<
|
|
1192
|
+
Opt extends true ? { [P in K]?: SchemaIn<S> } : { [P in K]: SchemaIn<S> },
|
|
1193
|
+
Opt extends true ? { [P in K]?: SchemaOut<S> } : { [P in K]: SchemaOut<S> },
|
|
1194
|
+
false
|
|
1195
|
+
>(props, { hasIsOfTypeCheck: false })
|
|
1196
|
+
}
|
|
1197
|
+
|
|
1163
1198
|
type Expand<T> = T extends infer O ? { [K in keyof O]: O[K] } : never
|
|
1164
1199
|
|
|
1165
1200
|
type ExactMatch<A, B> =
|
|
@@ -1201,3 +1236,4 @@ type EnumKeyUnion<T> =
|
|
|
1201
1236
|
|
|
1202
1237
|
type SchemaIn<S> = S extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never
|
|
1203
1238
|
type SchemaOut<S> = S extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never
|
|
1239
|
+
type SchemaOpt<S> = S extends JsonSchemaAnyBuilder<any, any, infer Opt> ? Opt : false
|