@trayio/commons 4.36.1 → 4.38.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.
- package/dist/dynamicschema/DynamicSchema.d.ts +2 -0
- package/dist/dynamicschema/DynamicSchema.d.ts.map +1 -1
- package/dist/dynamicschema/DynamicSchema.js +43 -1
- package/dist/dynamicschema/DynamicSchema.unit.test.js +46 -0
- package/dist/error/Error.d.ts.map +1 -1
- package/dist/error/Error.js +4 -1
- package/dist/task/Task.d.ts +14 -5
- package/dist/task/Task.d.ts.map +1 -1
- package/dist/task/Task.js +43 -4
- package/package.json +1 -1
|
@@ -10,6 +10,8 @@ export interface DynamicSchemaBrand {
|
|
|
10
10
|
export interface DynamicSchemaInterface {
|
|
11
11
|
fromDynamicObject: (schema: DynamicObject, strict?: O.Option<boolean>) => E.Either<Error, DynamicSchema>;
|
|
12
12
|
validate: (schema: DynamicSchema | DynamicObject, value: DynamicObject, strict?: O.Option<boolean>, coerce?: boolean) => E.Either<Error, DynamicObject>;
|
|
13
|
+
fromValue: (value: DynamicObject, schemaUrl: O.Option<string>) => DynamicObject;
|
|
14
|
+
isValidJsonSchema: (schema: DynamicObject) => boolean;
|
|
13
15
|
}
|
|
14
16
|
export declare const DynamicSchema: DynamicSchemaInterface;
|
|
15
17
|
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;
|
|
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;AA+BpC,QAAA,MAAM,2BAA2B,6EAKhC,CAAC;AAEF,MAAM,MAAM,aAAa,GAAG,CAAC,CAAC,MAAM,CAAC,OAAO,2BAA2B,CAAC,CAAC;AA0FzE,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,EAC1B,MAAM,CAAC,EAAE,OAAO,KACZ,CAAC,CAAC,MAAM,CAAC,KAAK,EAAE,aAAa,CAAC,CAAC;IACpC,SAAS,EAAE,CACV,KAAK,EAAE,aAAa,EACpB,SAAS,EAAE,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,KACvB,aAAa,CAAC;IACnB,iBAAiB,EAAE,CAAC,MAAM,EAAE,aAAa,KAAK,OAAO,CAAC;CACtD;AAED,eAAO,MAAM,aAAa,EAAE,sBAgD3B,CAAC"}
|
|
@@ -32,11 +32,13 @@ const ajv_formats_1 = __importDefault(require("ajv-formats"));
|
|
|
32
32
|
const E = __importStar(require("fp-ts/Either"));
|
|
33
33
|
const O = __importStar(require("fp-ts/Option"));
|
|
34
34
|
const t = __importStar(require("io-ts"));
|
|
35
|
-
const function_1 = require("fp-ts/function");
|
|
35
|
+
const function_1 = require("fp-ts/lib/function");
|
|
36
36
|
const deep_copy_ts_1 = require("deep-copy-ts");
|
|
37
37
|
const DynamicType_1 = require("../dynamictype/DynamicType");
|
|
38
38
|
const TypeCodec_1 = require("../codec/TypeCodec");
|
|
39
39
|
const Try_1 = require("../try/Try");
|
|
40
|
+
const draft04SchemaUrl = 'http://json-schema.org/draft-04/schema';
|
|
41
|
+
const schemaProperty = '$schema';
|
|
40
42
|
const ajv = new ajv_draft_04_1.default({ strict: false, removeAdditional: 'all' });
|
|
41
43
|
/*
|
|
42
44
|
We define additional formats for the JSON schema
|
|
@@ -92,6 +94,44 @@ const preprocessSchema = (schema, strict) => {
|
|
|
92
94
|
preprocessSchema(schema.items, strict);
|
|
93
95
|
}
|
|
94
96
|
};
|
|
97
|
+
const createSchema = (value) => {
|
|
98
|
+
if (Array.isArray(value) && value.length > 0) {
|
|
99
|
+
return {
|
|
100
|
+
type: 'array',
|
|
101
|
+
items: createSchema(value[0]),
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
if (typeof value === 'object' && value !== null) {
|
|
105
|
+
const properties = {};
|
|
106
|
+
for (const [key, val] of Object.entries(value)) {
|
|
107
|
+
properties[key] = createSchema(val);
|
|
108
|
+
}
|
|
109
|
+
return {
|
|
110
|
+
type: 'object',
|
|
111
|
+
properties,
|
|
112
|
+
required: Object.keys(properties),
|
|
113
|
+
};
|
|
114
|
+
}
|
|
115
|
+
if (value === null) {
|
|
116
|
+
return { type: 'null' };
|
|
117
|
+
}
|
|
118
|
+
if (typeof value === 'boolean') {
|
|
119
|
+
return { type: 'boolean' };
|
|
120
|
+
}
|
|
121
|
+
if (typeof value === 'number') {
|
|
122
|
+
return { type: 'number' };
|
|
123
|
+
}
|
|
124
|
+
if (typeof value === 'string') {
|
|
125
|
+
return { type: 'string' };
|
|
126
|
+
}
|
|
127
|
+
return {};
|
|
128
|
+
};
|
|
129
|
+
const createSchemaWithSchemaUrl = (schema, schemaUrl) => {
|
|
130
|
+
const hasSchemaUrl = O.isSome(schemaUrl);
|
|
131
|
+
const schemaObj = createSchema(schema);
|
|
132
|
+
schemaObj[schemaProperty] = hasSchemaUrl ? schemaUrl.value : draft04SchemaUrl;
|
|
133
|
+
return schemaObj;
|
|
134
|
+
};
|
|
95
135
|
exports.DynamicSchema = {
|
|
96
136
|
fromDynamicObject: (schema, strict = O.none) => {
|
|
97
137
|
const copiedSchema = (0, deep_copy_ts_1.deepCopy)(schema);
|
|
@@ -118,4 +158,6 @@ exports.DynamicSchema = {
|
|
|
118
158
|
return E.left(new Error(errorText));
|
|
119
159
|
}));
|
|
120
160
|
},
|
|
161
|
+
fromValue: (value, schemaUrl = O.none) => createSchemaWithSchemaUrl(value, schemaUrl),
|
|
162
|
+
isValidJsonSchema: (schema) => isDynamicSchema(schema),
|
|
121
163
|
};
|
|
@@ -30,6 +30,7 @@ describe('DynamicSchema', () => {
|
|
|
30
30
|
let validSchema;
|
|
31
31
|
let invalidSchema;
|
|
32
32
|
let value;
|
|
33
|
+
let validSchemaWithoutExtraFields;
|
|
33
34
|
let valueWithExtraFields;
|
|
34
35
|
let valueWithCoerceableTypes;
|
|
35
36
|
let invalidValue;
|
|
@@ -67,6 +68,40 @@ describe('DynamicSchema', () => {
|
|
|
67
68
|
required: ['name', 'age', 'races'],
|
|
68
69
|
advanced: [], // not part of json schema but the validator should not be strict with extra elements
|
|
69
70
|
};
|
|
71
|
+
validSchemaWithoutExtraFields = {
|
|
72
|
+
type: 'object',
|
|
73
|
+
properties: {
|
|
74
|
+
name: {
|
|
75
|
+
type: 'string',
|
|
76
|
+
},
|
|
77
|
+
age: {
|
|
78
|
+
type: 'number',
|
|
79
|
+
},
|
|
80
|
+
races: {
|
|
81
|
+
type: 'array',
|
|
82
|
+
items: {
|
|
83
|
+
type: 'object',
|
|
84
|
+
properties: {
|
|
85
|
+
name: {
|
|
86
|
+
type: 'string',
|
|
87
|
+
},
|
|
88
|
+
car: {
|
|
89
|
+
type: 'object',
|
|
90
|
+
properties: {
|
|
91
|
+
model: {
|
|
92
|
+
type: 'string',
|
|
93
|
+
},
|
|
94
|
+
},
|
|
95
|
+
required: ['model'],
|
|
96
|
+
},
|
|
97
|
+
},
|
|
98
|
+
required: ['name', 'car'],
|
|
99
|
+
},
|
|
100
|
+
},
|
|
101
|
+
},
|
|
102
|
+
required: ['name', 'age', 'races'],
|
|
103
|
+
$schema: 'http://json-schema.org/draft-04/schema',
|
|
104
|
+
};
|
|
70
105
|
invalidSchema = {
|
|
71
106
|
type: 'invalidType',
|
|
72
107
|
properties: {
|
|
@@ -574,4 +609,15 @@ describe('DynamicSchema', () => {
|
|
|
574
609
|
}
|
|
575
610
|
});
|
|
576
611
|
});
|
|
612
|
+
describe('fromValue', () => {
|
|
613
|
+
it('should return a Right with the schema when given a valid value object', () => {
|
|
614
|
+
const result = DynamicSchema_1.DynamicSchema.fromValue(value, O.none);
|
|
615
|
+
expect(result).toEqual(validSchemaWithoutExtraFields);
|
|
616
|
+
});
|
|
617
|
+
it('should return a Right with the schema when given a valid value object and a schema url', () => {
|
|
618
|
+
const schemaUrl = 'http://json-schema.org/draft-04/schema#';
|
|
619
|
+
const result = DynamicSchema_1.DynamicSchema.fromValue(value, O.some(schemaUrl));
|
|
620
|
+
expect(result.$schema).toEqual(schemaUrl);
|
|
621
|
+
});
|
|
622
|
+
});
|
|
577
623
|
});
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/error/Error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,
|
|
1
|
+
{"version":3,"file":"Error.d.ts","sourceRoot":"","sources":["../../src/error/Error.ts"],"names":[],"mappings":"AAAA,eAAO,MAAM,SAAS,EAAE,CAAC,KAAK,EAAE,GAAG,KAAK,KAWvC,CAAC"}
|
package/dist/error/Error.js
CHANGED
|
@@ -8,6 +8,9 @@ const castError = (error) => {
|
|
|
8
8
|
if (error instanceof Error) {
|
|
9
9
|
return error;
|
|
10
10
|
}
|
|
11
|
-
|
|
11
|
+
if (typeof error === 'object') {
|
|
12
|
+
return new Error(`Internal error: ${JSON.stringify(error)}`);
|
|
13
|
+
}
|
|
14
|
+
return new Error('Unknown internal error');
|
|
12
15
|
};
|
|
13
16
|
exports.castError = castError;
|
package/dist/task/Task.d.ts
CHANGED
|
@@ -1,7 +1,16 @@
|
|
|
1
|
-
import
|
|
1
|
+
import * as TE from 'fp-ts/TaskEither';
|
|
2
|
+
import * as TSK from 'fp-ts/Task';
|
|
2
3
|
import { Either } from 'fp-ts/lib/Either';
|
|
3
|
-
export
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
export declare const
|
|
4
|
+
export interface TaskExtensionsInterface {
|
|
5
|
+
recover: <T>(task: TSK.Task<T>, onError: (error: Error) => T) => TSK.Task<T>;
|
|
6
|
+
}
|
|
7
|
+
export declare const TaskExtensions: TaskExtensionsInterface;
|
|
8
|
+
export interface TaskEitherExtensionsInterface {
|
|
9
|
+
fromPromiseWithSimpleError: <T>(block: () => Promise<T>) => TE.TaskEither<Error, T>;
|
|
10
|
+
}
|
|
11
|
+
export declare const TaskEitherExtensions: TaskEitherExtensionsInterface;
|
|
12
|
+
export declare const createTaskEitherFromPromise: <E, T>(block: () => Promise<T>, errorHandling: (error: Error) => E) => TE.TaskEither<E, T>;
|
|
13
|
+
export declare const createTaskEither: <E, T>(block: () => Promise<Either<E, T>>) => TE.TaskEither<E, T>;
|
|
14
|
+
export declare const createTaskEitherFromEither: <E, T>(either: Either<E, T>) => TE.TaskEither<E, T>;
|
|
15
|
+
export declare const createTaskEitherFromPromiseWithSimpleError: <T>(block: () => Promise<T>) => TE.TaskEither<Error, T>;
|
|
7
16
|
//# sourceMappingURL=Task.d.ts.map
|
package/dist/task/Task.d.ts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"Task.d.ts","sourceRoot":"","sources":["../../src/task/Task.ts"],"names":[],"mappings":"A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AC,EAAE,CAAC,CAAC,KAChB,EAAE,CAAC,UAAU,CAAC,CAAC,EAAE,CAAC,CACyB,CAAC;AAGjD,eAAO,MAAM,0CAA0C,EAAE,CAAC,CAAC,EAC1D,KAAK,EAAE,MAAM,OAAO,CAAC,CAAC,CAAC,KACnB,EAAE,CAAC,UAAU,CAAC,KAAK,EAAE,CAAC,CACqB,CAAC"}
|
package/dist/task/Task.js
CHANGED
|
@@ -1,14 +1,53 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || function (mod) {
|
|
19
|
+
if (mod && mod.__esModule) return mod;
|
|
20
|
+
var result = {};
|
|
21
|
+
if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
|
|
22
|
+
__setModuleDefault(result, mod);
|
|
23
|
+
return result;
|
|
24
|
+
};
|
|
2
25
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.createTaskEitherFromPromiseWithSimpleError = exports.createTaskEitherFromEither = exports.createTaskEither = exports.createTaskEitherFromPromise = void 0;
|
|
4
|
-
const
|
|
26
|
+
exports.createTaskEitherFromPromiseWithSimpleError = exports.createTaskEitherFromEither = exports.createTaskEither = exports.createTaskEitherFromPromise = exports.TaskEitherExtensions = exports.TaskExtensions = void 0;
|
|
27
|
+
const TE = __importStar(require("fp-ts/TaskEither"));
|
|
5
28
|
const Error_1 = require("../error/Error");
|
|
6
|
-
|
|
29
|
+
exports.TaskExtensions = {
|
|
30
|
+
recover: (task, onError) => async () => {
|
|
31
|
+
try {
|
|
32
|
+
return await task();
|
|
33
|
+
}
|
|
34
|
+
catch (error) {
|
|
35
|
+
return onError((0, Error_1.castError)(error));
|
|
36
|
+
}
|
|
37
|
+
},
|
|
38
|
+
};
|
|
39
|
+
exports.TaskEitherExtensions = {
|
|
40
|
+
fromPromiseWithSimpleError: (block) => TE.tryCatch(block, (error) => (0, Error_1.castError)(error)),
|
|
41
|
+
};
|
|
42
|
+
// TODO: Remove, use TE.tryCatch
|
|
43
|
+
const createTaskEitherFromPromise = (block, errorHandling) => TE.tryCatch(block, (error) => errorHandling((0, Error_1.castError)(error)));
|
|
7
44
|
exports.createTaskEitherFromPromise = createTaskEitherFromPromise;
|
|
45
|
+
// TODO: Remove, use TE.tryCatch
|
|
8
46
|
const createTaskEither = (block) => block;
|
|
9
47
|
exports.createTaskEither = createTaskEither;
|
|
10
48
|
// TODO: Remove, we can use TE.fromEither (double check that the other functions are necessary)
|
|
11
49
|
const createTaskEitherFromEither = (either) => (0, exports.createTaskEither)(() => Promise.resolve(either));
|
|
12
50
|
exports.createTaskEitherFromEither = createTaskEitherFromEither;
|
|
13
|
-
|
|
51
|
+
// TODO: Remove, use the method on the interface
|
|
52
|
+
const createTaskEitherFromPromiseWithSimpleError = (block) => TE.tryCatch(block, (error) => (0, Error_1.castError)(error));
|
|
14
53
|
exports.createTaskEitherFromPromiseWithSimpleError = createTaskEitherFromPromiseWithSimpleError;
|