@naturalcycles/nodejs-lib 15.84.0 → 15.85.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.
@@ -293,6 +293,16 @@ export declare class JsonSchemaBooleanBuilder<IN extends boolean | undefined = b
293
293
  export declare class JsonSchemaObjectBuilder<IN extends AnyObject, OUT extends AnyObject, Opt extends boolean = false> extends JsonSchemaAnyBuilder<IN, OUT, Opt> {
294
294
  constructor(props?: AnyObject, opt?: JsonSchemaObjectBuilderOpts);
295
295
  addProperties(props: AnyObject): this;
296
+ /**
297
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
298
+ *
299
+ * This `null` 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
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
303
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
304
+ */
305
+ optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JsonSchemaTerminal<IN | undefined, OUT | undefined, true> : JsonSchemaAnyBuilder<IN | undefined, OUT | undefined, true>;
296
306
  /**
297
307
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
298
308
  */
@@ -355,6 +365,32 @@ export declare class JsonSchemaObjectInferringBuilder<PROPS extends Record<strin
355
365
  }>, Opt> {
356
366
  constructor(props?: PROPS);
357
367
  addProperties(props: PROPS): this;
368
+ /**
369
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
370
+ *
371
+ * This `null` feature only works when the current schema is nested in an object or array schema,
372
+ * due to how mutability works in Ajv.
373
+ *
374
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
375
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
376
+ */
377
+ optional<N extends null | undefined = undefined>(nullValue?: N): N extends null ? JsonSchemaTerminal<Expand<{
378
+ [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;
379
+ } & {
380
+ [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;
381
+ }> | undefined, Expand<{
382
+ [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;
383
+ } & {
384
+ [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;
385
+ }> | undefined, true> : JsonSchemaAnyBuilder<Expand<{
386
+ [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;
387
+ } & {
388
+ [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;
389
+ }> | undefined, Expand<{
390
+ [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;
391
+ } & {
392
+ [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;
393
+ }> | undefined, true>;
358
394
  /**
359
395
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
360
396
  */
@@ -687,6 +687,28 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
687
687
  this.schema.required = _uniq(required).sort();
688
688
  return this;
689
689
  }
690
+ /**
691
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
692
+ *
693
+ * This `null` feature only works when the current schema is nested in an object or array schema,
694
+ * due to how mutability works in Ajv.
695
+ *
696
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
697
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
698
+ */
699
+ optional(nullValue) {
700
+ if (typeof nullValue === 'undefined') {
701
+ return super.optional();
702
+ }
703
+ // When `null` is specified, we want `null` to be stripped and the value to become `undefined`,
704
+ // so we must allow `null` values to be parsed by Ajv,
705
+ // but the typing should not reflect that.
706
+ // We also cannot accept more rules attached, since we're not building an ObjectSchema anymore.
707
+ return new JsonSchemaTerminal({
708
+ anyOf: [{ type: 'null', optionalValues: [null] }, this.build()],
709
+ optionalField: true,
710
+ });
711
+ }
690
712
  /**
691
713
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
692
714
  */
@@ -773,6 +795,29 @@ export class JsonSchemaObjectInferringBuilder extends JsonSchemaAnyBuilder {
773
795
  this.schema.required = _uniq(required).sort();
774
796
  return this;
775
797
  }
798
+ /**
799
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
800
+ *
801
+ * This `null` feature only works when the current schema is nested in an object or array schema,
802
+ * due to how mutability works in Ajv.
803
+ *
804
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
805
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
806
+ */
807
+ // @ts-expect-error override adds optional parameter which is compatible but TS can't verify complex mapped types
808
+ optional(nullValue) {
809
+ if (nullValue === undefined) {
810
+ return super.optional();
811
+ }
812
+ // When `null` is specified, we want `null` to be stripped and the value to become `undefined`,
813
+ // so we must allow `null` values to be parsed by Ajv,
814
+ // but the typing should not reflect that.
815
+ // We also cannot accept more rules attached, since we're not building an ObjectSchema anymore.
816
+ return new JsonSchemaTerminal({
817
+ anyOf: [{ type: 'null', optionalValues: [null] }, this.build()],
818
+ optionalField: true,
819
+ });
820
+ }
776
821
  /**
777
822
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
778
823
  */
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.84.0",
4
+ "version": "15.85.0",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -968,6 +968,34 @@ export class JsonSchemaObjectBuilder<
968
968
  return this
969
969
  }
970
970
 
971
+ /**
972
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
973
+ *
974
+ * This `null` feature only works when the current schema is nested in an object or array schema,
975
+ * due to how mutability works in Ajv.
976
+ *
977
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
978
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
979
+ */
980
+ override optional<N extends null | undefined = undefined>(
981
+ nullValue?: N,
982
+ ): N extends null
983
+ ? JsonSchemaTerminal<IN | undefined, OUT | undefined, true>
984
+ : JsonSchemaAnyBuilder<IN | undefined, OUT | undefined, true> {
985
+ if (typeof nullValue === 'undefined') {
986
+ return super.optional()
987
+ }
988
+
989
+ // When `null` is specified, we want `null` to be stripped and the value to become `undefined`,
990
+ // so we must allow `null` values to be parsed by Ajv,
991
+ // but the typing should not reflect that.
992
+ // We also cannot accept more rules attached, since we're not building an ObjectSchema anymore.
993
+ return new JsonSchemaTerminal({
994
+ anyOf: [{ type: 'null', optionalValues: [null] }, this.build()],
995
+ optionalField: true,
996
+ }) as any
997
+ }
998
+
971
999
  /**
972
1000
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
973
1001
  */
@@ -1146,6 +1174,103 @@ export class JsonSchemaObjectInferringBuilder<
1146
1174
  return this
1147
1175
  }
1148
1176
 
1177
+ /**
1178
+ * @param nullValue Pass `null` to have `null` values be considered/converted as `undefined`.
1179
+ *
1180
+ * This `null` feature only works when the current schema is nested in an object or array schema,
1181
+ * due to how mutability works in Ajv.
1182
+ *
1183
+ * When `null` is passed, the return type becomes `JsonSchemaTerminal`
1184
+ * (no further chaining allowed) because the schema is wrapped in an anyOf structure.
1185
+ */
1186
+ // @ts-expect-error override adds optional parameter which is compatible but TS can't verify complex mapped types
1187
+ override optional<N extends null | undefined = undefined>(
1188
+ nullValue?: N,
1189
+ ): N extends null
1190
+ ? JsonSchemaTerminal<
1191
+ | Expand<
1192
+ {
1193
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1194
+ ? IsOpt extends true
1195
+ ? never
1196
+ : K
1197
+ : never]: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never
1198
+ } & {
1199
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1200
+ ? IsOpt extends true
1201
+ ? K
1202
+ : never
1203
+ : never]?: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never
1204
+ }
1205
+ >
1206
+ | undefined,
1207
+ | Expand<
1208
+ {
1209
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1210
+ ? IsOpt extends true
1211
+ ? never
1212
+ : K
1213
+ : never]: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never
1214
+ } & {
1215
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1216
+ ? IsOpt extends true
1217
+ ? K
1218
+ : never
1219
+ : never]?: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never
1220
+ }
1221
+ >
1222
+ | undefined,
1223
+ true
1224
+ >
1225
+ : JsonSchemaAnyBuilder<
1226
+ | Expand<
1227
+ {
1228
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1229
+ ? IsOpt extends true
1230
+ ? never
1231
+ : K
1232
+ : never]: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never
1233
+ } & {
1234
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1235
+ ? IsOpt extends true
1236
+ ? K
1237
+ : never
1238
+ : never]?: PROPS[K] extends JsonSchemaAnyBuilder<infer IN, any, any> ? IN : never
1239
+ }
1240
+ >
1241
+ | undefined,
1242
+ | Expand<
1243
+ {
1244
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1245
+ ? IsOpt extends true
1246
+ ? never
1247
+ : K
1248
+ : never]: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never
1249
+ } & {
1250
+ [K in keyof PROPS as PROPS[K] extends JsonSchemaAnyBuilder<any, any, infer IsOpt>
1251
+ ? IsOpt extends true
1252
+ ? K
1253
+ : never
1254
+ : never]?: PROPS[K] extends JsonSchemaAnyBuilder<any, infer OUT, any> ? OUT : never
1255
+ }
1256
+ >
1257
+ | undefined,
1258
+ true
1259
+ > {
1260
+ if (nullValue === undefined) {
1261
+ return super.optional() as any
1262
+ }
1263
+
1264
+ // When `null` is specified, we want `null` to be stripped and the value to become `undefined`,
1265
+ // so we must allow `null` values to be parsed by Ajv,
1266
+ // but the typing should not reflect that.
1267
+ // We also cannot accept more rules attached, since we're not building an ObjectSchema anymore.
1268
+ return new JsonSchemaTerminal({
1269
+ anyOf: [{ type: 'null', optionalValues: [null] }, this.build()],
1270
+ optionalField: true,
1271
+ }) as any
1272
+ }
1273
+
1149
1274
  /**
1150
1275
  * When set, the validation will not strip away properties that are not specified explicitly in the schema.
1151
1276
  */