@trayio/commons 4.3.0 → 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.
|
@@ -4,7 +4,7 @@ import * as t from 'io-ts';
|
|
|
4
4
|
import { DynamicObject } from '../dynamictype/DynamicType';
|
|
5
5
|
declare const dynamicSchemaTypeDescriptor: t.BrandC<t.Type<DynamicObject, DynamicObject, unknown>, DynamicSchemaBrand>;
|
|
6
6
|
export type DynamicSchema = t.TypeOf<typeof dynamicSchemaTypeDescriptor>;
|
|
7
|
-
export declare const
|
|
7
|
+
export declare const recursivelyRemoveEmptyRequiredAndAddAdditionalProperties: (schema: DynamicObject, strict: O.Option<boolean>) => void;
|
|
8
8
|
export interface DynamicSchemaBrand {
|
|
9
9
|
readonly DynamicSchema: unique symbol;
|
|
10
10
|
}
|
|
@@ -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,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAG3B,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,eAAO,MAAM,
|
|
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,cAAc,CAAC;AAClC,OAAO,KAAK,CAAC,MAAM,OAAO,CAAC;AAG3B,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,eAAO,MAAM,wDAAwD,WAC5D,aAAa,UACb,EAAE,MAAM,CAAC,OAAO,CAAC,SA4CzB,CAAC;AAEF,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,aAAa,EAAE,OAAO,MAAM,CAAC;CACtC;AAED,MAAM,WAAW,sBAAsB;IACtC,iBAAiB,EAAE,CAClB,SAAS,EAAE,aAAa,KACpB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpC,QAAQ,EAAE,CACT,MAAM,EAAE,aAAa,GAAG,aAAa,EACrC,KAAK,EAAE,aAAa,EACpB,MAAM,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KACrB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;CAChC;AAED,eAAO,MAAM,aAAa,EAAE,sBA6B3B,CAAC"}
|
|
@@ -26,7 +26,7 @@ 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 = exports.
|
|
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"));
|
|
@@ -54,39 +54,43 @@ const isDynamicSchema = (o) => {
|
|
|
54
54
|
};
|
|
55
55
|
const dynamicSchemaTypeDescriptor = t.brand(DynamicType_1.dynamicObjectTypeDescriptor, (input) => isDynamicSchema(input), 'DynamicSchema');
|
|
56
56
|
const dynamicSchemaTypeCodec = TypeCodec_1.TypeCodec.fromDescriptor(dynamicSchemaTypeDescriptor);
|
|
57
|
-
const
|
|
57
|
+
const recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = (schema, strict) => {
|
|
58
|
+
const hasStrict = O.isSome(strict);
|
|
58
59
|
if (schema.oneOf || schema.anyOf || schema.allOf) {
|
|
59
60
|
const schemas = (schema.oneOf ||
|
|
60
61
|
schema.anyOf ||
|
|
61
62
|
schema.allOf);
|
|
62
63
|
schemas.forEach((subSchema) => {
|
|
63
|
-
(0, exports.
|
|
64
|
+
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(subSchema, strict);
|
|
64
65
|
});
|
|
65
66
|
}
|
|
66
67
|
if (schema.properties) {
|
|
67
|
-
|
|
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
|
+
}
|
|
68
76
|
const properties = schema.properties;
|
|
69
77
|
Object.keys(properties).forEach((key) => {
|
|
70
78
|
const property = properties[key];
|
|
71
79
|
if (property.type === 'object' || property.type === 'array') {
|
|
72
|
-
(0, exports.
|
|
80
|
+
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(property, strict);
|
|
73
81
|
}
|
|
74
82
|
});
|
|
75
83
|
}
|
|
76
84
|
if (schema.type === 'array') {
|
|
77
|
-
(0, exports.
|
|
85
|
+
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema.items, strict);
|
|
78
86
|
}
|
|
79
87
|
};
|
|
80
|
-
exports.
|
|
88
|
+
exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = recursivelyRemoveEmptyRequiredAndAddAdditionalProperties;
|
|
81
89
|
exports.DynamicSchema = {
|
|
82
90
|
fromDynamicObject: (schemaObj) => (0, function_1.pipe)(dynamicSchemaTypeCodec.decode(schemaObj), E.mapLeft((e) => new Error(`Failed to create DynamicSchema: ${e}`))),
|
|
83
91
|
validate: (schema, value, strict) => {
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
|
|
87
|
-
const hasAdditionalProperties = !strict.value;
|
|
88
|
-
(0, exports.recursivelyAddAdditionalProperties)(copiedSchema, hasAdditionalProperties);
|
|
89
|
-
}
|
|
92
|
+
const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
|
|
93
|
+
(0, exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(copiedSchema, strict);
|
|
90
94
|
const validateE = (0, Try_1.tryToEither)(() => ajv.compile(copiedSchema));
|
|
91
95
|
return (0, function_1.pipe)(validateE, E.chain((validate) => {
|
|
92
96
|
if (validate(value)) {
|
|
@@ -111,9 +111,80 @@ describe('DynamicSchema', () => {
|
|
|
111
111
|
}
|
|
112
112
|
});
|
|
113
113
|
});
|
|
114
|
-
describe('
|
|
115
|
-
it('should correctly add additionalProperties
|
|
116
|
-
const schema =
|
|
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
|
+
};
|
|
117
188
|
const expectedSchema = {
|
|
118
189
|
type: 'object',
|
|
119
190
|
properties: {
|
|
@@ -124,11 +195,10 @@ describe('DynamicSchema', () => {
|
|
|
124
195
|
type: 'number',
|
|
125
196
|
},
|
|
126
197
|
},
|
|
127
|
-
required: ['name', 'age'],
|
|
128
198
|
advanced: [],
|
|
129
199
|
additionalProperties: true,
|
|
130
200
|
};
|
|
131
|
-
(0, DynamicSchema_1.
|
|
201
|
+
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
|
|
132
202
|
expect(schema).toEqual(expectedSchema);
|
|
133
203
|
});
|
|
134
204
|
it('should correctly add additionalProperties=false to object schema', () => {
|
|
@@ -147,7 +217,7 @@ describe('DynamicSchema', () => {
|
|
|
147
217
|
advanced: [],
|
|
148
218
|
additionalProperties: false,
|
|
149
219
|
};
|
|
150
|
-
(0, DynamicSchema_1.
|
|
220
|
+
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(true));
|
|
151
221
|
expect(schema).toEqual(expectedSchema);
|
|
152
222
|
});
|
|
153
223
|
it('should correctly add additionalProperties=true to complex object schema', () => {
|
|
@@ -214,14 +284,14 @@ describe('DynamicSchema', () => {
|
|
|
214
284
|
},
|
|
215
285
|
additionalProperties: true,
|
|
216
286
|
};
|
|
217
|
-
(0, DynamicSchema_1.
|
|
287
|
+
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
|
|
218
288
|
expect(schema).toEqual(expectedSchema);
|
|
219
289
|
});
|
|
220
290
|
it('should skip mutation if there is no object type schema', () => {
|
|
221
291
|
const schema = {
|
|
222
292
|
type: 'string',
|
|
223
293
|
};
|
|
224
|
-
(0, DynamicSchema_1.
|
|
294
|
+
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(true));
|
|
225
295
|
expect(schema).toEqual(schema);
|
|
226
296
|
});
|
|
227
297
|
it('should correctly add additionalProperties=true to oneOf schemas', () => {
|
|
@@ -269,7 +339,7 @@ describe('DynamicSchema', () => {
|
|
|
269
339
|
},
|
|
270
340
|
],
|
|
271
341
|
};
|
|
272
|
-
(0, DynamicSchema_1.
|
|
342
|
+
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
|
|
273
343
|
expect(schema).toEqual(expectedSchema);
|
|
274
344
|
});
|
|
275
345
|
it('should correctly add additionalProperties=true to anyOf schemas', () => {
|
|
@@ -317,7 +387,7 @@ describe('DynamicSchema', () => {
|
|
|
317
387
|
},
|
|
318
388
|
],
|
|
319
389
|
};
|
|
320
|
-
(0, DynamicSchema_1.
|
|
390
|
+
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
|
|
321
391
|
expect(schema).toEqual(expectedSchema);
|
|
322
392
|
});
|
|
323
393
|
it('should correctly add additionalProperties=true to allOf schemas', () => {
|
|
@@ -365,7 +435,7 @@ describe('DynamicSchema', () => {
|
|
|
365
435
|
},
|
|
366
436
|
],
|
|
367
437
|
};
|
|
368
|
-
(0, DynamicSchema_1.
|
|
438
|
+
(0, DynamicSchema_1.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties)(schema, O.some(false));
|
|
369
439
|
expect(schema).toEqual(expectedSchema);
|
|
370
440
|
});
|
|
371
441
|
});
|