@trayio/commons 4.2.3 → 4.4.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,14 +1,16 @@
1
1
  import * as E from 'fp-ts/Either';
2
+ import * as O from 'fp-ts/Option';
2
3
  import * as t from 'io-ts';
3
4
  import { DynamicObject } from '../dynamictype/DynamicType';
4
5
  declare const dynamicSchemaTypeDescriptor: t.BrandC<t.Type<DynamicObject, DynamicObject, unknown>, DynamicSchemaBrand>;
5
6
  export type DynamicSchema = t.TypeOf<typeof dynamicSchemaTypeDescriptor>;
7
+ export declare const recursivelyRemoveEmptyRequiredAndAddAdditionalProperties: (schema: DynamicObject, strict: O.Option<boolean>) => void;
6
8
  export interface DynamicSchemaBrand {
7
9
  readonly DynamicSchema: unique symbol;
8
10
  }
9
11
  export interface DynamicSchemaInterface {
10
12
  fromDynamicObject: (schemaObj: DynamicObject) => E.Either<Error, DynamicSchema>;
11
- validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject) => E.Either<Error, undefined>;
13
+ validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict: O.Option<boolean>) => E.Either<Error, undefined>;
12
14
  }
13
15
  export declare const DynamicSchema: DynamicSchemaInterface;
14
16
  export {};
@@ -1 +1 @@
1
- {"version":3,"file":"DynamicSchema.d.ts","sourceRoot":"","sources":["../../src/dynamicschema/DynamicSchema.ts"],"names":[],"mappings":"AAEA,OAAO,KAAK,CAAC,MAAM,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAE3B,OAAO,EACN,aAAa,EAEb,MAAM,4BAA4B,CAAC;AAqBpC,QAAA,MAAM,2BAA2B,6EAKhC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,2BAA2B,CAAC,CAAC;AAMzE,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,aAAa,EAAE,OAAO,MAAM,CAAC;CACtC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Aa,EAAE,sBA6B3B,CAAC"}
@@ -26,12 +26,14 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
26
26
  return (mod && mod.__esModule) ? mod : { "default": mod };
27
27
  };
28
28
  Object.defineProperty(exports, "__esModule", { value: true });
29
- exports.DynamicSchema = void 0;
29
+ exports.DynamicSchema = exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = void 0;
30
30
  const ajv_draft_04_1 = __importDefault(require("ajv-draft-04"));
31
31
  const ajv_formats_1 = __importDefault(require("ajv-formats"));
32
32
  const E = __importStar(require("fp-ts/Either"));
33
+ const O = __importStar(require("fp-ts/Option"));
33
34
  const t = __importStar(require("io-ts"));
34
35
  const function_1 = require("fp-ts/function");
36
+ const deep_copy_ts_1 = require("deep-copy-ts");
35
37
  const DynamicType_1 = require("../dynamictype/DynamicType");
36
38
  const TypeCodec_1 = require("../codec/TypeCodec");
37
39
  const Try_1 = require("../try/Try");
@@ -52,10 +54,44 @@ const isDynamicSchema = (o) => {
52
54
  };
53
55
  const dynamicSchemaTypeDescriptor = t.brand(DynamicType_1.dynamicObjectTypeDescriptor, (input) => isDynamicSchema(input), 'DynamicSchema');
54
56
  const dynamicSchemaTypeCodec = TypeCodec_1.TypeCodec.fromDescriptor(dynamicSchemaTypeDescriptor);
57
+ const recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = (schema, strict) => {
58
+ const hasStrict = O.isSome(strict);
59
+ if (schema.oneOf || schema.anyOf || schema.allOf) {
60
+ const schemas = (schema.oneOf ||
61
+ schema.anyOf ||
62
+ schema.allOf);
63
+ schemas.forEach((subSchema) => {
64
+ (0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(subSchema, strict);
65
+ });
66
+ }
67
+ if (schema.properties) {
68
+ if (hasStrict) {
69
+ schema.additionalProperties = !strict.value;
70
+ }
71
+ if (schema.required &&
72
+ Array.isArray(schema.required) &&
73
+ schema.required.length === 0) {
74
+ delete schema.required;
75
+ }
76
+ const properties = schema.properties;
77
+ Object.keys(properties).forEach((key) => {
78
+ const property = properties[key];
79
+ if (property.type === 'object' || property.type === 'array') {
80
+ (0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(property, strict);
81
+ }
82
+ });
83
+ }
84
+ if (schema.type === 'array') {
85
+ (0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema.items, strict);
86
+ }
87
+ };
88
+ exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = recursivelyRemoveEmptyRequiredAndAddAdditionalProperties;
55
89
  exports.DynamicSchema = {
56
90
  fromDynamicObject: (schemaObj) => (0, function_1.pipe)(dynamicSchemaTypeCodec.decode(schemaObj), E.mapLeft((e) => new Error(`Failed to create DynamicSchema: ${e}`))),
57
- validate: (schema, value) => {
58
- const validateE = (0, Try_1.tryToEither)(() => ajv.compile(schema));
91
+ validate: (schema, value, strict) => {
92
+ const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
93
+ (0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(copiedSchema, strict);
94
+ const validateE = (0, Try_1.tryToEither)(() => ajv.compile(copiedSchema));
59
95
  return (0, function_1.pipe)(validateE, E.chain((validate) => {
60
96
  if (validate(value)) {
61
97
  return E.right(undefined);
@@ -24,6 +24,7 @@ var __importStar = (this && this.__importStar) || function (mod) {
24
24
  };
25
25
  Object.defineProperty(exports, "__esModule", { value: true });
26
26
  const E = __importStar(require("fp-ts/Either"));
27
+ const O = __importStar(require("fp-ts/Option"));
27
28
  const DynamicSchema_1 = require("./DynamicSchema");
28
29
  describe('DynamicSchema', () => {
29
30
  let validSchema;
@@ -82,7 +83,17 @@ describe('DynamicSchema', () => {
82
83
  it('should return a Right undefined when the value matches the schema', () => {
83
84
  const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
84
85
  if (E.isRight(schema)) {
85
- const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value);
86
+ const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, O.none);
87
+ expect(E.isRight(result)).toBe(true);
88
+ if (E.isRight(result)) {
89
+ expect(result.right).toBeUndefined();
90
+ }
91
+ }
92
+ });
93
+ it('should return a Right undefined when strict is false and value matches the schema', () => {
94
+ const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
95
+ if (E.isRight(schema)) {
96
+ const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, O.some(false));
86
97
  expect(E.isRight(result)).toBe(true);
87
98
  if (E.isRight(result)) {
88
99
  expect(result.right).toBeUndefined();
@@ -92,7 +103,7 @@ describe('DynamicSchema', () => {
92
103
  it('should return a Left Error when the value does not match the schema', () => {
93
104
  const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
94
105
  if (E.isRight(schema)) {
95
- const result = DynamicSchema_1.DynamicSchema.validate(schema.right, invalidSchema);
106
+ const result = DynamicSchema_1.DynamicSchema.validate(schema.right, invalidSchema, O.none);
96
107
  expect(E.isLeft(result)).toBe(true);
97
108
  if (E.isLeft(result)) {
98
109
  expect(result.left).toBeInstanceOf(Error);
@@ -100,4 +111,332 @@ describe('DynamicSchema', () => {
100
111
  }
101
112
  });
102
113
  });
114
+ describe('recursivelyRemoveEmptyRequiredAndAddAdditionalProperties', () => {
115
+ it('should correctly remove empty required arrays and not add additionalProperties when strict is not set', () => {
116
+ const schema = {
117
+ type: 'object',
118
+ properties: {
119
+ name: {
120
+ type: 'string',
121
+ },
122
+ age: {
123
+ type: 'number',
124
+ },
125
+ },
126
+ required: [],
127
+ advanced: [],
128
+ };
129
+ const expectedSchema = {
130
+ type: 'object',
131
+ properties: {
132
+ name: {
133
+ type: 'string',
134
+ },
135
+ age: {
136
+ type: 'number',
137
+ },
138
+ },
139
+ advanced: [],
140
+ };
141
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.none);
142
+ expect(schema).toEqual(expectedSchema);
143
+ });
144
+ it('should correctly keep populated required arrays and not add additionalProperties when strict is not set', () => {
145
+ const schema = {
146
+ type: 'object',
147
+ properties: {
148
+ name: {
149
+ type: 'string',
150
+ },
151
+ age: {
152
+ type: 'number',
153
+ },
154
+ },
155
+ required: ['age', 'name'],
156
+ advanced: [],
157
+ };
158
+ const expectedSchema = {
159
+ type: 'object',
160
+ properties: {
161
+ name: {
162
+ type: 'string',
163
+ },
164
+ age: {
165
+ type: 'number',
166
+ },
167
+ },
168
+ required: ['age', 'name'],
169
+ advanced: [],
170
+ };
171
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.none);
172
+ expect(schema).toEqual(expectedSchema);
173
+ });
174
+ it('should correctly remove empty required and add additionalProperties=true to object schema when strict is set', () => {
175
+ const schema = {
176
+ type: 'object',
177
+ properties: {
178
+ name: {
179
+ type: 'string',
180
+ },
181
+ age: {
182
+ type: 'number',
183
+ },
184
+ },
185
+ required: [],
186
+ advanced: [],
187
+ };
188
+ const expectedSchema = {
189
+ type: 'object',
190
+ properties: {
191
+ name: {
192
+ type: 'string',
193
+ },
194
+ age: {
195
+ type: 'number',
196
+ },
197
+ },
198
+ advanced: [],
199
+ additionalProperties: true,
200
+ };
201
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
202
+ expect(schema).toEqual(expectedSchema);
203
+ });
204
+ it('should correctly add additionalProperties=false to object schema', () => {
205
+ const schema = validSchema;
206
+ const expectedSchema = {
207
+ type: 'object',
208
+ properties: {
209
+ name: {
210
+ type: 'string',
211
+ },
212
+ age: {
213
+ type: 'number',
214
+ },
215
+ },
216
+ required: ['name', 'age'],
217
+ advanced: [],
218
+ additionalProperties: false,
219
+ };
220
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(true));
221
+ expect(schema).toEqual(expectedSchema);
222
+ });
223
+ it('should correctly add additionalProperties=true to complex object schema', () => {
224
+ const schema = {
225
+ type: 'object',
226
+ properties: {
227
+ name: {
228
+ type: 'array',
229
+ items: {
230
+ type: 'object',
231
+ properties: {
232
+ first: {
233
+ type: 'string',
234
+ },
235
+ last: {
236
+ type: 'string',
237
+ },
238
+ },
239
+ },
240
+ },
241
+ age: {
242
+ type: 'object',
243
+ properties: {
244
+ years: {
245
+ type: 'number',
246
+ },
247
+ months: {
248
+ type: 'number',
249
+ },
250
+ },
251
+ },
252
+ },
253
+ };
254
+ const expectedSchema = {
255
+ type: 'object',
256
+ properties: {
257
+ name: {
258
+ type: 'array',
259
+ items: {
260
+ type: 'object',
261
+ properties: {
262
+ first: {
263
+ type: 'string',
264
+ },
265
+ last: {
266
+ type: 'string',
267
+ },
268
+ },
269
+ additionalProperties: true,
270
+ },
271
+ },
272
+ age: {
273
+ type: 'object',
274
+ properties: {
275
+ years: {
276
+ type: 'number',
277
+ },
278
+ months: {
279
+ type: 'number',
280
+ },
281
+ },
282
+ additionalProperties: true,
283
+ },
284
+ },
285
+ additionalProperties: true,
286
+ };
287
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
288
+ expect(schema).toEqual(expectedSchema);
289
+ });
290
+ it('should skip mutation if there is no object type schema', () => {
291
+ const schema = {
292
+ type: 'string',
293
+ };
294
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(true));
295
+ expect(schema).toEqual(schema);
296
+ });
297
+ it('should correctly add additionalProperties=true to oneOf schemas', () => {
298
+ const schema = {
299
+ type: 'object',
300
+ oneOf: [
301
+ {
302
+ type: 'object',
303
+ properties: {
304
+ name: { type: 'string' },
305
+ age: { type: 'number' },
306
+ },
307
+ required: ['name', 'age'],
308
+ },
309
+ {
310
+ type: 'object',
311
+ properties: {
312
+ name: { type: 'string' },
313
+ email: { type: 'string' },
314
+ },
315
+ required: ['name', 'email'],
316
+ },
317
+ ],
318
+ };
319
+ const expectedSchema = {
320
+ type: 'object',
321
+ oneOf: [
322
+ {
323
+ type: 'object',
324
+ properties: {
325
+ name: { type: 'string' },
326
+ age: { type: 'number' },
327
+ },
328
+ required: ['name', 'age'],
329
+ additionalProperties: true,
330
+ },
331
+ {
332
+ type: 'object',
333
+ properties: {
334
+ name: { type: 'string' },
335
+ email: { type: 'string' },
336
+ },
337
+ required: ['name', 'email'],
338
+ additionalProperties: true,
339
+ },
340
+ ],
341
+ };
342
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
343
+ expect(schema).toEqual(expectedSchema);
344
+ });
345
+ it('should correctly add additionalProperties=true to anyOf schemas', () => {
346
+ const schema = {
347
+ type: 'object',
348
+ anyOf: [
349
+ {
350
+ type: 'object',
351
+ properties: {
352
+ name: { type: 'string' },
353
+ age: { type: 'number' },
354
+ },
355
+ required: ['name', 'age'],
356
+ },
357
+ {
358
+ type: 'object',
359
+ properties: {
360
+ name: { type: 'string' },
361
+ email: { type: 'string' },
362
+ },
363
+ required: ['name', 'email'],
364
+ },
365
+ ],
366
+ };
367
+ const expectedSchema = {
368
+ type: 'object',
369
+ anyOf: [
370
+ {
371
+ type: 'object',
372
+ properties: {
373
+ name: { type: 'string' },
374
+ age: { type: 'number' },
375
+ },
376
+ required: ['name', 'age'],
377
+ additionalProperties: true,
378
+ },
379
+ {
380
+ type: 'object',
381
+ properties: {
382
+ name: { type: 'string' },
383
+ email: { type: 'string' },
384
+ },
385
+ required: ['name', 'email'],
386
+ additionalProperties: true,
387
+ },
388
+ ],
389
+ };
390
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
391
+ expect(schema).toEqual(expectedSchema);
392
+ });
393
+ it('should correctly add additionalProperties=true to allOf schemas', () => {
394
+ const schema = {
395
+ type: 'object',
396
+ allOf: [
397
+ {
398
+ type: 'object',
399
+ properties: {
400
+ name: { type: 'string' },
401
+ age: { type: 'number' },
402
+ },
403
+ required: ['name', 'age'],
404
+ },
405
+ {
406
+ type: 'object',
407
+ properties: {
408
+ name: { type: 'string' },
409
+ email: { type: 'string' },
410
+ },
411
+ required: ['name', 'email'],
412
+ },
413
+ ],
414
+ };
415
+ const expectedSchema = {
416
+ type: 'object',
417
+ allOf: [
418
+ {
419
+ type: 'object',
420
+ properties: {
421
+ name: { type: 'string' },
422
+ age: { type: 'number' },
423
+ },
424
+ required: ['name', 'age'],
425
+ additionalProperties: true,
426
+ },
427
+ {
428
+ type: 'object',
429
+ properties: {
430
+ name: { type: 'string' },
431
+ email: { type: 'string' },
432
+ },
433
+ required: ['name', 'email'],
434
+ additionalProperties: true,
435
+ },
436
+ ],
437
+ };
438
+ (0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
439
+ expect(schema).toEqual(expectedSchema);
440
+ });
441
+ });
103
442
  });
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@trayio/commons",
3
- "version": "4.2.3",
3
+ "version": "4.4.0",
4
4
  "description": "Extensions to the standard/core libraries and basic features",
5
5
  "exports": {
6
6
  "./*": "./dist/*.js"
@@ -20,6 +20,7 @@
20
20
  "ajv": "^8.12.0",
21
21
  "ajv-draft-04": "^1.0.0",
22
22
  "ajv-formats": "^2.1.1",
23
+ "deep-copy-ts": "^0.5.4",
23
24
  "formidable": "3.5.1",
24
25
  "fp-ts": "2.12.3",
25
26
  "fs-extra": "11.1.0",