@naturalcycles/nodejs-lib 15.57.0 → 15.57.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.
@@ -49,6 +49,8 @@ export declare class JsonSchemaAnyBuilder<IN, OUT, Opt> extends JsonSchemaTermin
49
49
  *
50
50
  * const schemaBad = j.string().isOfType<number>() // ❌
51
51
  * schemaBad.build() // TypeError: property "build" does not exist on type "never"
52
+ *
53
+ * const result = ajvValidateRequest.body(req, schemaBad) // result will have `unknown` type
52
54
  * ```
53
55
  */
54
56
  isOfType<ExpectedType>(): ExactMatch<ExpectedType, OUT> extends true ? this : never;
@@ -26,8 +26,6 @@ export const j = {
26
26
  },
27
27
  stringMap(schema) {
28
28
  const builtSchema = schema.build();
29
- _assert(!builtSchema?.optionalField, 'In a StringMap schema the value cannot be `undefined`, because `undefined` is not a valid JSON Schema value.');
30
- builtSchema.optionalField = undefined;
31
29
  return new JsonSchemaObjectBuilder({}, {
32
30
  hasIsOfTypeCheck: false,
33
31
  patternProperties: {
@@ -111,7 +109,9 @@ export class JsonSchemaTerminal {
111
109
  * Same as if it would be JSON.stringified.
112
110
  */
113
111
  build() {
114
- return _sortObject(JSON.parse(JSON.stringify(this.schema)), JSON_SCHEMA_ORDER);
112
+ const jsonSchema = _sortObject(JSON.parse(JSON.stringify(this.schema)), JSON_SCHEMA_ORDER);
113
+ delete jsonSchema.optionalField;
114
+ return jsonSchema;
115
115
  }
116
116
  clone() {
117
117
  return new JsonSchemaAnyBuilder(_deepCopy(this.schema));
@@ -140,6 +140,8 @@ export class JsonSchemaAnyBuilder extends JsonSchemaTerminal {
140
140
  *
141
141
  * const schemaBad = j.string().isOfType<number>() // ❌
142
142
  * schemaBad.build() // TypeError: property "build" does not exist on type "never"
143
+ *
144
+ * const result = ajvValidateRequest.body(req, schemaBad) // result will have `unknown` type
143
145
  * ```
144
146
  */
145
147
  isOfType() {
@@ -533,13 +535,11 @@ export class JsonSchemaObjectBuilder extends JsonSchemaAnyBuilder {
533
535
  const properties = {};
534
536
  const required = [];
535
537
  for (const [key, builder] of Object.entries(props)) {
536
- const schema = builder.build();
537
- if (!schema.optionalField) {
538
+ const isOptional = builder.getSchema().optionalField;
539
+ if (!isOptional) {
538
540
  required.push(key);
539
541
  }
540
- else {
541
- schema.optionalField = undefined;
542
- }
542
+ const schema = builder.build();
543
543
  properties[key] = schema;
544
544
  }
545
545
  this.schema.properties = properties;
@@ -595,13 +595,11 @@ export class JsonSchemaObjectInferringBuilder extends JsonSchemaAnyBuilder {
595
595
  const properties = {};
596
596
  const required = [];
597
597
  for (const [key, builder] of Object.entries(props)) {
598
- const schema = builder.build();
599
- if (!schema.optionalField) {
598
+ const isOptional = builder.getSchema().optionalField;
599
+ if (!isOptional) {
600
600
  required.push(key);
601
601
  }
602
- else {
603
- schema.optionalField = undefined;
604
- }
602
+ const schema = builder.build();
605
603
  properties[key] = schema;
606
604
  }
607
605
  this.schema.properties = properties;
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@naturalcycles/nodejs-lib",
3
3
  "type": "module",
4
- "version": "15.57.0",
4
+ "version": "15.57.1",
5
5
  "dependencies": {
6
6
  "@naturalcycles/js-lib": "^15",
7
7
  "@types/js-yaml": "^4",
@@ -70,11 +70,6 @@ export const j = {
70
70
  schema: S,
71
71
  ): JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>> {
72
72
  const builtSchema = schema.build()
73
- _assert(
74
- !builtSchema?.optionalField,
75
- 'In a StringMap schema the value cannot be `undefined`, because `undefined` is not a valid JSON Schema value.',
76
- )
77
- builtSchema.optionalField = undefined
78
73
 
79
74
  return new JsonSchemaObjectBuilder<StringMap<SchemaIn<S>>, StringMap<SchemaOut<S>>>(
80
75
  {},
@@ -193,7 +188,14 @@ export class JsonSchemaTerminal<IN, OUT, Opt> {
193
188
  * Same as if it would be JSON.stringified.
194
189
  */
195
190
  build(): JsonSchema<IN, OUT> {
196
- return _sortObject(JSON.parse(JSON.stringify(this.schema)), JSON_SCHEMA_ORDER)
191
+ const jsonSchema = _sortObject(
192
+ JSON.parse(JSON.stringify(this.schema)),
193
+ JSON_SCHEMA_ORDER,
194
+ ) as JsonSchema<IN, OUT>
195
+
196
+ delete jsonSchema.optionalField
197
+
198
+ return jsonSchema
197
199
  }
198
200
 
199
201
  clone(): JsonSchemaAnyBuilder<IN, OUT, Opt> {
@@ -226,6 +228,8 @@ export class JsonSchemaAnyBuilder<IN, OUT, Opt> extends JsonSchemaTerminal<IN, O
226
228
  *
227
229
  * const schemaBad = j.string().isOfType<number>() // ❌
228
230
  * schemaBad.build() // TypeError: property "build" does not exist on type "never"
231
+ *
232
+ * const result = ajvValidateRequest.body(req, schemaBad) // result will have `unknown` type
229
233
  * ```
230
234
  */
231
235
  isOfType<ExpectedType>(): ExactMatch<ExpectedType, OUT> extends true ? this : never {
@@ -755,12 +759,12 @@ export class JsonSchemaObjectBuilder<
755
759
  const required: string[] = []
756
760
 
757
761
  for (const [key, builder] of Object.entries(props)) {
758
- const schema = builder.build()
759
- if (!schema.optionalField) {
762
+ const isOptional = (builder as JsonSchemaTerminal<any, any, any>).getSchema().optionalField
763
+ if (!isOptional) {
760
764
  required.push(key)
761
- } else {
762
- schema.optionalField = undefined
763
765
  }
766
+
767
+ const schema = builder.build()
764
768
  properties[key] = schema
765
769
  }
766
770
 
@@ -870,12 +874,12 @@ export class JsonSchemaObjectInferringBuilder<
870
874
  const required: string[] = []
871
875
 
872
876
  for (const [key, builder] of Object.entries(props)) {
873
- const schema = builder.build()
874
- if (!schema.optionalField) {
877
+ const isOptional = (builder as JsonSchemaTerminal<any, any, any>).getSchema().optionalField
878
+ if (!isOptional) {
875
879
  required.push(key)
876
- } else {
877
- schema.optionalField = undefined
878
880
  }
881
+
882
+ const schema = builder.build()
879
883
  properties[key] = schema
880
884
  }
881
885