@trayio/commons 4.5.0 → 4.6.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,13 +4,12 @@ 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 recursivelyRemoveEmptyRequiredAndAddAdditionalProperties: (schema: DynamicObject, strict: O.Option<boolean>) => void;
|
|
8
7
|
export interface DynamicSchemaBrand {
|
|
9
8
|
readonly DynamicSchema: unique symbol;
|
|
10
9
|
}
|
|
11
10
|
export interface DynamicSchemaInterface {
|
|
12
|
-
fromDynamicObject: (
|
|
13
|
-
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict
|
|
11
|
+
fromDynamicObject: (schema: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, DynamicSchema>;
|
|
12
|
+
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, undefined>;
|
|
14
13
|
}
|
|
15
14
|
export declare const DynamicSchema: DynamicSchemaInterface;
|
|
16
15
|
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,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;
|
|
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;AA4CzE,MAAM,WAAW,kBAAkB;IAClC,QAAQ,CAAC,aAAa,EAAE,OAAO,MAAM,CAAC;CACtC;AAED,MAAM,WAAW,sBAAsB;IACtC,iBAAiB,EAAE,CAClB,MAAM,EAAE,aAAa,EACrB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KACtB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpC,QAAQ,EAAE,CACT,MAAM,EAAE,aAAa,GAAG,aAAa,EACrC,KAAK,EAAE,aAAa,EACpB,MAAM,CAAC,EAAE,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,KACtB,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,SAAS,CAAC,CAAC;CAChC;AAED,eAAO,MAAM,aAAa,EAAE,sBA8B3B,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 =
|
|
29
|
+
exports.DynamicSchema = 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,14 +54,15 @@ 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 preprocessSchema = (schema, strict) => {
|
|
58
58
|
const hasStrict = O.isSome(strict);
|
|
59
|
+
delete schema.$schema;
|
|
59
60
|
if (schema.oneOf || schema.anyOf || schema.allOf) {
|
|
60
61
|
const schemas = (schema.oneOf ||
|
|
61
62
|
schema.anyOf ||
|
|
62
63
|
schema.allOf);
|
|
63
64
|
schemas.forEach((subSchema) => {
|
|
64
|
-
(
|
|
65
|
+
preprocessSchema(subSchema, strict);
|
|
65
66
|
});
|
|
66
67
|
}
|
|
67
68
|
if (schema.properties) {
|
|
@@ -77,20 +78,23 @@ const recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = (schema, strict
|
|
|
77
78
|
Object.keys(properties).forEach((key) => {
|
|
78
79
|
const property = properties[key];
|
|
79
80
|
if (property.type === 'object' || property.type === 'array') {
|
|
80
|
-
(
|
|
81
|
+
preprocessSchema(property, strict);
|
|
81
82
|
}
|
|
82
83
|
});
|
|
83
84
|
}
|
|
84
85
|
if (schema.type === 'array') {
|
|
85
|
-
(
|
|
86
|
+
preprocessSchema(schema.items, strict);
|
|
86
87
|
}
|
|
87
88
|
};
|
|
88
|
-
exports.recursivelyRemoveEmptyRequiredAndAddAdditionalProperties = recursivelyRemoveEmptyRequiredAndAddAdditionalProperties;
|
|
89
89
|
exports.DynamicSchema = {
|
|
90
|
-
fromDynamicObject: (
|
|
91
|
-
validate: (schema, value, strict) => {
|
|
90
|
+
fromDynamicObject: (schema, strict = O.none) => {
|
|
92
91
|
const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
|
|
93
|
-
(
|
|
92
|
+
preprocessSchema(copiedSchema, strict);
|
|
93
|
+
return (0, function_1.pipe)(dynamicSchemaTypeCodec.decode(copiedSchema), E.mapLeft((e) => new Error(`Failed to create DynamicSchema: ${e}`)));
|
|
94
|
+
},
|
|
95
|
+
validate: (schema, value, strict = O.none) => {
|
|
96
|
+
const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
|
|
97
|
+
preprocessSchema(copiedSchema, strict);
|
|
94
98
|
const validateE = (0, Try_1.tryToEither)(() => ajv.compile(copiedSchema));
|
|
95
99
|
return (0, function_1.pipe)(validateE, E.chain((validate) => {
|
|
96
100
|
if (validate(value)) {
|
|
@@ -64,7 +64,6 @@ describe('DynamicSchema', () => {
|
|
|
64
64
|
describe('fromDynamicObject', () => {
|
|
65
65
|
it('should return a Right DynamicSchema when given a valid schema object', () => {
|
|
66
66
|
const result = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
67
|
-
console.log(JSON.stringify(result));
|
|
68
67
|
expect(E.isRight(result)).toBe(true);
|
|
69
68
|
if (E.isRight(result)) {
|
|
70
69
|
expect(result.right).toEqual(validSchema);
|
|
@@ -78,40 +77,35 @@ describe('DynamicSchema', () => {
|
|
|
78
77
|
expect(result.left.message).toContain('Failed to create DynamicSchema:');
|
|
79
78
|
}
|
|
80
79
|
});
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
|
|
109
|
-
expect(result.left).toBeInstanceOf(Error);
|
|
110
|
-
}
|
|
111
|
-
}
|
|
80
|
+
it('should remove empty $schema property', () => {
|
|
81
|
+
const schema = {
|
|
82
|
+
type: 'object',
|
|
83
|
+
properties: {
|
|
84
|
+
name: {
|
|
85
|
+
type: 'string',
|
|
86
|
+
},
|
|
87
|
+
age: {
|
|
88
|
+
type: 'number',
|
|
89
|
+
},
|
|
90
|
+
},
|
|
91
|
+
advanced: [],
|
|
92
|
+
$schema: 'http://json-schema.org/draft-07/schema#',
|
|
93
|
+
};
|
|
94
|
+
const expectedSchema = {
|
|
95
|
+
type: 'object',
|
|
96
|
+
properties: {
|
|
97
|
+
name: {
|
|
98
|
+
type: 'string',
|
|
99
|
+
},
|
|
100
|
+
age: {
|
|
101
|
+
type: 'number',
|
|
102
|
+
},
|
|
103
|
+
},
|
|
104
|
+
advanced: [],
|
|
105
|
+
};
|
|
106
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.none);
|
|
107
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
112
108
|
});
|
|
113
|
-
});
|
|
114
|
-
describe('recursivelyRemoveEmptyRequiredAndAddAdditionalProperties', () => {
|
|
115
109
|
it('should correctly remove empty required arrays and not add additionalProperties when strict is not set', () => {
|
|
116
110
|
const schema = {
|
|
117
111
|
type: 'object',
|
|
@@ -138,8 +132,8 @@ describe('DynamicSchema', () => {
|
|
|
138
132
|
},
|
|
139
133
|
advanced: [],
|
|
140
134
|
};
|
|
141
|
-
|
|
142
|
-
expect(
|
|
135
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.none);
|
|
136
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
143
137
|
});
|
|
144
138
|
it('should correctly keep populated required arrays and not add additionalProperties when strict is not set', () => {
|
|
145
139
|
const schema = {
|
|
@@ -168,8 +162,8 @@ describe('DynamicSchema', () => {
|
|
|
168
162
|
required: ['age', 'name'],
|
|
169
163
|
advanced: [],
|
|
170
164
|
};
|
|
171
|
-
|
|
172
|
-
expect(
|
|
165
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.none);
|
|
166
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
173
167
|
});
|
|
174
168
|
it('should correctly remove empty required and add additionalProperties=true to object schema when strict is set', () => {
|
|
175
169
|
const schema = {
|
|
@@ -198,8 +192,8 @@ describe('DynamicSchema', () => {
|
|
|
198
192
|
advanced: [],
|
|
199
193
|
additionalProperties: true,
|
|
200
194
|
};
|
|
201
|
-
|
|
202
|
-
expect(
|
|
195
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.some(false));
|
|
196
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
203
197
|
});
|
|
204
198
|
it('should correctly add additionalProperties=false to object schema', () => {
|
|
205
199
|
const schema = validSchema;
|
|
@@ -217,8 +211,8 @@ describe('DynamicSchema', () => {
|
|
|
217
211
|
advanced: [],
|
|
218
212
|
additionalProperties: false,
|
|
219
213
|
};
|
|
220
|
-
|
|
221
|
-
expect(
|
|
214
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.some(true));
|
|
215
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
222
216
|
});
|
|
223
217
|
it('should correctly add additionalProperties=true to complex object schema', () => {
|
|
224
218
|
const schema = {
|
|
@@ -284,15 +278,15 @@ describe('DynamicSchema', () => {
|
|
|
284
278
|
},
|
|
285
279
|
additionalProperties: true,
|
|
286
280
|
};
|
|
287
|
-
|
|
288
|
-
expect(
|
|
281
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.some(false));
|
|
282
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
289
283
|
});
|
|
290
284
|
it('should skip mutation if there is no object type schema', () => {
|
|
291
285
|
const schema = {
|
|
292
286
|
type: 'string',
|
|
293
287
|
};
|
|
294
|
-
|
|
295
|
-
expect(
|
|
288
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.some(true));
|
|
289
|
+
expect(actualSchemaE).toEqual(E.right(schema));
|
|
296
290
|
});
|
|
297
291
|
it('should correctly add additionalProperties=true to oneOf schemas', () => {
|
|
298
292
|
const schema = {
|
|
@@ -339,8 +333,8 @@ describe('DynamicSchema', () => {
|
|
|
339
333
|
},
|
|
340
334
|
],
|
|
341
335
|
};
|
|
342
|
-
|
|
343
|
-
expect(
|
|
336
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.some(false));
|
|
337
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
344
338
|
});
|
|
345
339
|
it('should correctly add additionalProperties=true to anyOf schemas', () => {
|
|
346
340
|
const schema = {
|
|
@@ -387,8 +381,8 @@ describe('DynamicSchema', () => {
|
|
|
387
381
|
},
|
|
388
382
|
],
|
|
389
383
|
};
|
|
390
|
-
|
|
391
|
-
expect(
|
|
384
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.some(false));
|
|
385
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
392
386
|
});
|
|
393
387
|
it('should correctly add additionalProperties=true to allOf schemas', () => {
|
|
394
388
|
const schema = {
|
|
@@ -435,8 +429,40 @@ describe('DynamicSchema', () => {
|
|
|
435
429
|
},
|
|
436
430
|
],
|
|
437
431
|
};
|
|
438
|
-
|
|
439
|
-
expect(
|
|
432
|
+
const actualSchemaE = DynamicSchema_1.DynamicSchema.fromDynamicObject(schema, O.some(false));
|
|
433
|
+
expect(actualSchemaE).toEqual(E.right(expectedSchema));
|
|
434
|
+
});
|
|
435
|
+
});
|
|
436
|
+
describe('validate', () => {
|
|
437
|
+
it('should return a Right undefined when the value matches the schema', () => {
|
|
438
|
+
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
439
|
+
if (E.isRight(schema)) {
|
|
440
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, O.none);
|
|
441
|
+
expect(E.isRight(result)).toBe(true);
|
|
442
|
+
if (E.isRight(result)) {
|
|
443
|
+
expect(result.right).toBeUndefined();
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
});
|
|
447
|
+
it('should return a Right undefined when strict is false and value matches the schema', () => {
|
|
448
|
+
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
449
|
+
if (E.isRight(schema)) {
|
|
450
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, value, O.some(false));
|
|
451
|
+
expect(E.isRight(result)).toBe(true);
|
|
452
|
+
if (E.isRight(result)) {
|
|
453
|
+
expect(result.right).toBeUndefined();
|
|
454
|
+
}
|
|
455
|
+
}
|
|
456
|
+
});
|
|
457
|
+
it('should return a Left Error when the value does not match the schema', () => {
|
|
458
|
+
const schema = DynamicSchema_1.DynamicSchema.fromDynamicObject(validSchema);
|
|
459
|
+
if (E.isRight(schema)) {
|
|
460
|
+
const result = DynamicSchema_1.DynamicSchema.validate(schema.right, invalidSchema, O.none);
|
|
461
|
+
expect(E.isLeft(result)).toBe(true);
|
|
462
|
+
if (E.isLeft(result)) {
|
|
463
|
+
expect(result.left).toBeInstanceOf(Error);
|
|
464
|
+
}
|
|
465
|
+
}
|
|
440
466
|
});
|
|
441
467
|
});
|
|
442
468
|
});
|