@croct/sdk 0.10.0 → 0.11.0-alpha.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.
- package/.src/activeRecord.ts +150 -0
- package/.src/base64Url.ts +18 -0
- package/.src/cache/cache.ts +15 -0
- package/.src/cache/fallbackCache.ts +29 -0
- package/.src/cache/inMemoryCache.ts +21 -0
- package/.src/cache/index.ts +4 -0
- package/.src/cache/localStorageCache.ts +85 -0
- package/.src/channel/beaconSocketChannel.ts +153 -0
- package/.src/channel/channel.ts +20 -0
- package/.src/channel/encodedChannel.ts +21 -0
- package/.src/channel/guaranteedChannel.ts +131 -0
- package/.src/channel/index.ts +8 -0
- package/.src/channel/queuedChannel.ts +112 -0
- package/.src/channel/retryChannel.ts +90 -0
- package/.src/channel/sandboxChannel.ts +43 -0
- package/.src/channel/socketChannel.ts +217 -0
- package/.src/cid/assigner.ts +3 -0
- package/.src/cid/cachedAssigner.ts +35 -0
- package/.src/cid/fixedAssigner.ts +13 -0
- package/.src/cid/index.ts +4 -0
- package/.src/cid/remoteAssigner.ts +47 -0
- package/.src/constants.ts +6 -0
- package/.src/container.ts +388 -0
- package/.src/contentFetcher.ts +226 -0
- package/.src/context.ts +137 -0
- package/.src/error.ts +31 -0
- package/.src/evaluator.ts +251 -0
- package/.src/eventManager.ts +53 -0
- package/.src/facade/contentFetcherFacade.ts +69 -0
- package/.src/facade/evaluatorFacade.ts +152 -0
- package/.src/facade/index.ts +7 -0
- package/.src/facade/sdkFacade.ts +291 -0
- package/.src/facade/sessionFacade.ts +14 -0
- package/.src/facade/sessionPatch.ts +32 -0
- package/.src/facade/trackerFacade.ts +98 -0
- package/.src/facade/userFacade.ts +26 -0
- package/.src/facade/userPatch.ts +32 -0
- package/.src/index.ts +4 -0
- package/.src/logging/consoleLogger.ts +37 -0
- package/.src/logging/index.ts +4 -0
- package/.src/logging/logger.ts +13 -0
- package/.src/logging/namespacedLogger.ts +32 -0
- package/.src/logging/nullLogger.ts +19 -0
- package/.src/namespacedStorage.ts +69 -0
- package/.src/patch.ts +64 -0
- package/.src/queue/capacityRestrictedQueue.ts +44 -0
- package/.src/queue/inMemoryQueue.ts +43 -0
- package/.src/queue/index.ts +5 -0
- package/.src/queue/monitoredQueue.ts +168 -0
- package/.src/queue/persistentQueue.ts +84 -0
- package/.src/queue/queue.ts +15 -0
- package/.src/retry/arbitraryPolicy.ts +21 -0
- package/.src/retry/backoffPolicy.ts +84 -0
- package/.src/retry/index.ts +5 -0
- package/.src/retry/maxAttemptsPolicy.ts +28 -0
- package/.src/retry/neverPolicy.ts +11 -0
- package/.src/retry/policy.ts +5 -0
- package/.src/schema/attributeSchema.ts +6 -0
- package/.src/schema/contentFetcherSchemas.ts +23 -0
- package/.src/schema/contentSchemas.ts +44 -0
- package/.src/schema/contextSchemas.ts +5 -0
- package/.src/schema/ecommerceSchemas.ts +179 -0
- package/.src/schema/evaluatorSchemas.ts +11 -0
- package/.src/schema/eventSchemas.ts +150 -0
- package/.src/schema/index.ts +11 -0
- package/.src/schema/loggerSchema.ts +12 -0
- package/.src/schema/operationSchemas.ts +102 -0
- package/.src/schema/sdkFacadeSchemas.ts +44 -0
- package/.src/schema/sdkSchemas.ts +49 -0
- package/.src/schema/tokenSchema.ts +42 -0
- package/.src/schema/userSchema.ts +184 -0
- package/.src/sdk.ts +174 -0
- package/.src/sdkEvents.ts +15 -0
- package/.src/sourceLocation.ts +85 -0
- package/.src/tab.ts +148 -0
- package/.src/token/cachedTokenStore.ts +34 -0
- package/.src/token/inMemoryTokenStore.ts +13 -0
- package/.src/token/index.ts +4 -0
- package/.src/token/replicatedTokenStore.ts +21 -0
- package/.src/token/token.ts +164 -0
- package/.src/tracker.ts +460 -0
- package/.src/trackingEvents.ts +456 -0
- package/.src/transformer.ts +7 -0
- package/.src/utilityTypes.ts +3 -0
- package/.src/uuid.ts +43 -0
- package/.src/validation/arrayType.ts +71 -0
- package/.src/validation/booleanType.ts +22 -0
- package/.src/validation/functionType.ts +22 -0
- package/.src/validation/index.ts +12 -0
- package/.src/validation/jsonType.ts +157 -0
- package/.src/validation/mixedSchema.ts +7 -0
- package/.src/validation/nullType.ts +22 -0
- package/.src/validation/numberType.ts +59 -0
- package/.src/validation/objectType.ts +138 -0
- package/.src/validation/schema.ts +21 -0
- package/.src/validation/stringType.ts +118 -0
- package/.src/validation/unionType.ts +53 -0
- package/.src/validation/violation.ts +23 -0
- package/activeRecord.js +33 -36
- package/base64Url.js +1 -0
- package/cache/cache.js +1 -0
- package/cache/fallbackCache.js +16 -32
- package/cache/inMemoryCache.js +10 -10
- package/cache/index.js +2 -1
- package/cache/localStorageCache.js +25 -25
- package/channel/beaconSocketChannel.d.ts +1 -1
- package/channel/beaconSocketChannel.js +50 -79
- package/channel/channel.d.ts +1 -1
- package/channel/channel.js +1 -0
- package/channel/encodedChannel.js +9 -10
- package/channel/guaranteedChannel.d.ts +4 -4
- package/channel/guaranteedChannel.js +42 -43
- package/channel/index.js +2 -1
- package/channel/queuedChannel.js +36 -64
- package/channel/retryChannel.d.ts +1 -1
- package/channel/retryChannel.js +45 -77
- package/channel/sandboxChannel.js +18 -18
- package/channel/socketChannel.d.ts +4 -4
- package/channel/socketChannel.js +78 -79
- package/cid/assigner.js +1 -0
- package/cid/cachedAssigner.js +16 -27
- package/cid/fixedAssigner.js +6 -6
- package/cid/index.js +2 -1
- package/cid/remoteAssigner.js +24 -36
- package/constants.d.ts +6 -5
- package/constants.js +7 -5
- package/container.d.ts +13 -6
- package/container.js +153 -168
- package/contentFetcher.d.ts +59 -0
- package/contentFetcher.js +130 -0
- package/context.d.ts +3 -3
- package/context.js +37 -38
- package/error.js +3 -2
- package/evaluator.d.ts +33 -24
- package/evaluator.js +127 -117
- package/eventManager.d.ts +1 -1
- package/eventManager.js +15 -15
- package/facade/contentFetcherFacade.d.ts +27 -0
- package/facade/contentFetcherFacade.js +41 -0
- package/facade/evaluatorFacade.d.ts +13 -3
- package/facade/evaluatorFacade.js +58 -72
- package/facade/index.js +1 -0
- package/facade/sdkFacade.d.ts +10 -3
- package/facade/sdkFacade.js +130 -141
- package/facade/sessionFacade.js +7 -7
- package/facade/sessionPatch.js +10 -13
- package/facade/trackerFacade.js +33 -38
- package/facade/userFacade.js +11 -11
- package/facade/userPatch.js +10 -13
- package/index.js +3 -2
- package/logging/consoleLogger.js +19 -35
- package/logging/index.js +2 -1
- package/logging/logger.js +1 -0
- package/logging/namespacedLogger.js +15 -15
- package/logging/nullLogger.js +11 -13
- package/namespacedStorage.js +31 -47
- package/package.json +13 -16
- package/patch.d.ts +1 -1
- package/patch.js +1 -0
- package/queue/capacityRestrictedQueue.js +18 -18
- package/queue/inMemoryQueue.js +23 -28
- package/queue/index.js +2 -1
- package/queue/monitoredQueue.d.ts +2 -2
- package/queue/monitoredQueue.js +40 -40
- package/queue/persistentQueue.js +34 -38
- package/queue/queue.js +1 -0
- package/retry/arbitraryPolicy.js +9 -10
- package/retry/backoffPolicy.d.ts +1 -1
- package/retry/backoffPolicy.js +12 -13
- package/retry/index.js +2 -1
- package/retry/maxAttemptsPolicy.js +8 -8
- package/retry/neverPolicy.js +7 -9
- package/retry/policy.js +1 -0
- package/schema/attributeSchema.js +2 -1
- package/schema/contentFetcherSchemas.d.ts +2 -0
- package/schema/contentFetcherSchemas.js +23 -0
- package/schema/contentSchemas.js +2 -1
- package/schema/contextSchemas.js +2 -1
- package/schema/ecommerceSchemas.js +2 -1
- package/schema/evaluatorSchemas.d.ts +2 -0
- package/schema/{evaluationSchemas.js → evaluatorSchemas.js} +4 -3
- package/schema/eventSchemas.js +6 -7
- package/schema/index.d.ts +2 -1
- package/schema/index.js +4 -2
- package/schema/loggerSchema.js +2 -1
- package/schema/operationSchemas.js +9 -8
- package/schema/sdkFacadeSchemas.js +10 -6
- package/schema/sdkSchemas.js +9 -5
- package/schema/tokenSchema.js +6 -4
- package/schema/userSchema.js +3 -2
- package/sdk.d.ts +9 -3
- package/sdk.js +82 -127
- package/sdkEvents.d.ts +3 -3
- package/sdkEvents.js +1 -0
- package/sourceLocation.d.ts +3 -3
- package/sourceLocation.js +14 -14
- package/tab.d.ts +5 -5
- package/tab.js +51 -80
- package/token/cachedTokenStore.js +10 -10
- package/token/inMemoryTokenStore.js +8 -8
- package/token/index.js +2 -1
- package/token/replicatedTokenStore.js +8 -8
- package/token/token.d.ts +9 -5
- package/token/token.js +64 -57
- package/tracker.d.ts +4 -4
- package/tracker.js +146 -122
- package/trackingEvents.d.ts +36 -36
- package/trackingEvents.js +13 -6
- package/transformer.js +2 -1
- package/utilityTypes.d.ts +2 -2
- package/utilityTypes.js +1 -0
- package/uuid.js +10 -7
- package/validation/arrayType.d.ts +2 -2
- package/validation/arrayType.js +30 -27
- package/validation/booleanType.js +12 -15
- package/validation/functionType.js +12 -15
- package/validation/index.js +2 -1
- package/validation/jsonType.d.ts +2 -2
- package/validation/jsonType.js +62 -80
- package/validation/mixedSchema.js +5 -7
- package/validation/nullType.js +12 -15
- package/validation/numberType.d.ts +1 -1
- package/validation/numberType.js +24 -22
- package/validation/objectType.d.ts +1 -1
- package/validation/objectType.js +62 -72
- package/validation/schema.js +7 -10
- package/validation/stringType.d.ts +1 -1
- package/validation/stringType.js +37 -47
- package/validation/unionType.js +28 -77
- package/validation/violation.js +2 -2
- package/schema/evaluationSchemas.d.ts +0 -2
package/validation/objectType.js
CHANGED
|
@@ -1,102 +1,92 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.ObjectType = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
function ObjectType(schema) {
|
|
10
|
-
if (schema === void 0) { schema = {}; }
|
|
4
|
+
const schema_1 = require("./schema");
|
|
5
|
+
const mixedSchema_1 = require("./mixedSchema");
|
|
6
|
+
const violation_1 = require("./violation");
|
|
7
|
+
class ObjectType {
|
|
8
|
+
constructor(schema = {}) {
|
|
11
9
|
var _a, _b, _c, _d, _e, _f;
|
|
12
|
-
this.definition =
|
|
10
|
+
this.definition = {
|
|
11
|
+
...schema,
|
|
12
|
+
properties: (_a = schema.properties) !== null && _a !== void 0 ? _a : {},
|
|
13
|
+
required: (_b = schema.required) !== null && _b !== void 0 ? _b : [],
|
|
14
|
+
additionalProperties: (_c = schema.additionalProperties) !== null && _c !== void 0 ? _c : false,
|
|
15
|
+
propertyNames: (_d = schema.propertyNames) !== null && _d !== void 0 ? _d : new mixedSchema_1.MixedSchema(),
|
|
16
|
+
minProperties: (_e = schema.minProperties) !== null && _e !== void 0 ? _e : -1,
|
|
17
|
+
maxProperties: (_f = schema.maxProperties) !== null && _f !== void 0 ? _f : -1,
|
|
18
|
+
};
|
|
13
19
|
}
|
|
14
|
-
|
|
20
|
+
getTypes() {
|
|
15
21
|
if (this.definition.type !== undefined) {
|
|
16
22
|
return [this.definition.type.name];
|
|
17
23
|
}
|
|
18
24
|
return ['object'];
|
|
19
|
-
}
|
|
20
|
-
|
|
25
|
+
}
|
|
26
|
+
isValidType(value) {
|
|
21
27
|
if (this.definition.type !== undefined) {
|
|
22
28
|
return value instanceof this.definition.type;
|
|
23
29
|
}
|
|
24
|
-
return Object.prototype
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
30
|
+
return Object.prototype
|
|
31
|
+
.toString
|
|
32
|
+
.call(value) === '[object Object]';
|
|
33
|
+
}
|
|
34
|
+
validate(value, path = []) {
|
|
29
35
|
if (!this.isValidType(value)) {
|
|
30
|
-
|
|
31
|
-
throw new schema_1.Violation(
|
|
32
|
-
+
|
|
36
|
+
const [type] = this.getTypes();
|
|
37
|
+
throw new schema_1.Violation(`Expected value of type ${type} at path '${(0, violation_1.formatPath)(path)}', `
|
|
38
|
+
+ `actual ${(0, violation_1.describe)(value)}.`, path, { type: type });
|
|
33
39
|
}
|
|
34
|
-
|
|
35
|
-
|
|
40
|
+
const entries = Object.entries(value);
|
|
41
|
+
const { minProperties, maxProperties } = this.definition;
|
|
36
42
|
if (minProperties >= 0 && minProperties > entries.length) {
|
|
37
|
-
throw new schema_1.Violation(
|
|
38
|
-
+
|
|
39
|
-
+
|
|
43
|
+
throw new schema_1.Violation(`Expected ${minProperties === maxProperties ? 'exactly' : 'at least'} `
|
|
44
|
+
+ `${minProperties} ${minProperties === 1 ? 'entry' : 'entries'} `
|
|
45
|
+
+ `at path '${(0, violation_1.formatPath)(path)}', actual ${entries.length}.`, path, { limit: minProperties });
|
|
40
46
|
}
|
|
41
47
|
if (maxProperties >= 0 && maxProperties < entries.length) {
|
|
42
|
-
throw new schema_1.Violation(
|
|
43
|
-
+
|
|
44
|
-
+
|
|
48
|
+
throw new schema_1.Violation(`Expected ${minProperties === maxProperties ? 'exactly' : 'at most'} `
|
|
49
|
+
+ `${maxProperties} ${maxProperties === 1 ? 'entry' : 'entries'} `
|
|
50
|
+
+ `at path '${(0, violation_1.formatPath)(path)}', actual ${entries.length}.`, path, { limit: maxProperties });
|
|
45
51
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
required: property,
|
|
53
|
-
});
|
|
54
|
-
}
|
|
52
|
+
const additionalProperties = { ...value };
|
|
53
|
+
for (const property of this.definition.required) {
|
|
54
|
+
if (!(property in value)) {
|
|
55
|
+
throw new schema_1.Violation(`Missing property '${(0, violation_1.formatPath)(path.concat([property]))}'.`, path, {
|
|
56
|
+
required: property,
|
|
57
|
+
});
|
|
55
58
|
}
|
|
56
59
|
}
|
|
57
|
-
|
|
58
|
-
|
|
59
|
-
|
|
60
|
-
|
|
60
|
+
for (const [entryName, entryValue] of entries) {
|
|
61
|
+
const propertyPath = path.concat([entryName]);
|
|
62
|
+
this.definition
|
|
63
|
+
.propertyNames
|
|
64
|
+
.validate(entryName, propertyPath);
|
|
65
|
+
const propertyRule = this.definition.properties[entryName];
|
|
66
|
+
if (propertyRule !== undefined) {
|
|
67
|
+
propertyRule.validate(entryValue, propertyPath);
|
|
68
|
+
delete additionalProperties[entryName];
|
|
69
|
+
continue;
|
|
61
70
|
}
|
|
62
|
-
|
|
63
|
-
|
|
64
|
-
|
|
65
|
-
|
|
66
|
-
var _g = tslib_1.__read(entries_1_1.value, 2), entryName = _g[0], entryValue = _g[1];
|
|
67
|
-
var propertyPath = path.concat([entryName]);
|
|
68
|
-
this.definition.propertyNames.validate(entryName, propertyPath);
|
|
69
|
-
var propertyRule = this.definition.properties[entryName];
|
|
70
|
-
if (propertyRule !== undefined) {
|
|
71
|
-
propertyRule.validate(entryValue, propertyPath);
|
|
72
|
-
delete additionalProperties[entryName];
|
|
73
|
-
continue;
|
|
74
|
-
}
|
|
75
|
-
if (this.definition.additionalProperties === false) {
|
|
76
|
-
throw new schema_1.Violation("Unknown property '".concat((0, violation_1.formatPath)(propertyPath), "'."), propertyPath, {
|
|
77
|
-
additionalProperty: entryName,
|
|
78
|
-
});
|
|
79
|
-
}
|
|
80
|
-
if (this.definition.additionalProperties !== true) {
|
|
81
|
-
this.definition.additionalProperties.validate(entryValue, propertyPath);
|
|
82
|
-
}
|
|
71
|
+
if (this.definition.additionalProperties === false) {
|
|
72
|
+
throw new schema_1.Violation(`Unknown property '${(0, violation_1.formatPath)(propertyPath)}'.`, propertyPath, {
|
|
73
|
+
additionalProperty: entryName,
|
|
74
|
+
});
|
|
83
75
|
}
|
|
84
|
-
|
|
85
|
-
|
|
86
|
-
|
|
87
|
-
|
|
88
|
-
if (entries_1_1 && !entries_1_1.done && (_b = entries_1.return)) _b.call(entries_1);
|
|
76
|
+
if (this.definition.additionalProperties !== true) {
|
|
77
|
+
this.definition
|
|
78
|
+
.additionalProperties
|
|
79
|
+
.validate(entryValue, propertyPath);
|
|
89
80
|
}
|
|
90
|
-
finally { if (e_2) throw e_2.error; }
|
|
91
81
|
}
|
|
92
|
-
|
|
82
|
+
const { subtypes } = this.definition;
|
|
93
83
|
if (subtypes !== undefined) {
|
|
94
|
-
|
|
84
|
+
const type = value[subtypes.discriminator];
|
|
95
85
|
if (type !== undefined && subtypes.schemas[type] !== undefined) {
|
|
96
86
|
subtypes.schemas[type].validate(additionalProperties, path);
|
|
97
87
|
}
|
|
98
88
|
}
|
|
99
|
-
}
|
|
100
|
-
|
|
101
|
-
}());
|
|
89
|
+
}
|
|
90
|
+
}
|
|
102
91
|
exports.ObjectType = ObjectType;
|
|
92
|
+
//# sourceMappingURL=objectType.js.map
|
package/validation/schema.js
CHANGED
|
@@ -1,15 +1,12 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.Violation = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
_this.path = path;
|
|
10
|
-
_this.params = params;
|
|
11
|
-
return _this;
|
|
4
|
+
class Violation extends Error {
|
|
5
|
+
constructor(message, path, params) {
|
|
6
|
+
super(message);
|
|
7
|
+
this.path = path;
|
|
8
|
+
this.params = params;
|
|
12
9
|
}
|
|
13
|
-
|
|
14
|
-
}(Error));
|
|
10
|
+
}
|
|
15
11
|
exports.Violation = Violation;
|
|
12
|
+
//# sourceMappingURL=schema.js.map
|
package/validation/stringType.js
CHANGED
|
@@ -1,10 +1,9 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.StringType = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
var FORMAT = {
|
|
4
|
+
const schema_1 = require("./schema");
|
|
5
|
+
const violation_1 = require("./violation");
|
|
6
|
+
const FORMAT = {
|
|
8
7
|
pointer: function pointer(value) {
|
|
9
8
|
return /^(\.|([a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])(\.[a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])*)$/.test(value);
|
|
10
9
|
},
|
|
@@ -19,71 +18,62 @@ var FORMAT = {
|
|
|
19
18
|
},
|
|
20
19
|
url: function url(value) {
|
|
21
20
|
try {
|
|
22
|
-
// eslint-disable-next-line no-new
|
|
21
|
+
// eslint-disable-next-line no-new -- Easier way to validate a URL
|
|
23
22
|
new URL(value);
|
|
24
23
|
}
|
|
25
|
-
catch
|
|
26
|
-
return false;
|
|
27
|
-
}
|
|
28
|
-
return true;
|
|
29
|
-
},
|
|
30
|
-
'uri-reference': function uriReference(value) {
|
|
31
|
-
try {
|
|
32
|
-
// This simplistic approach covers the most common cases
|
|
33
|
-
// without inflating the library with an RFC 3986-compliant parser.
|
|
34
|
-
// eslint-disable-next-line no-new
|
|
35
|
-
new URL(value, 'http://any.thing');
|
|
36
|
-
}
|
|
37
|
-
catch (_a) {
|
|
24
|
+
catch {
|
|
38
25
|
return false;
|
|
39
26
|
}
|
|
40
27
|
return true;
|
|
41
28
|
},
|
|
42
29
|
};
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
if (definition === void 0) { definition = {}; }
|
|
30
|
+
class StringType {
|
|
31
|
+
constructor(definition = {}) {
|
|
46
32
|
var _a, _b, _c;
|
|
47
|
-
this.definition =
|
|
33
|
+
this.definition = {
|
|
34
|
+
...definition,
|
|
35
|
+
minLength: (_a = definition.minLength) !== null && _a !== void 0 ? _a : -1,
|
|
36
|
+
maxLength: (_b = definition.maxLength) !== null && _b !== void 0 ? _b : -1,
|
|
37
|
+
enumeration: (_c = definition.enumeration) !== null && _c !== void 0 ? _c : [],
|
|
38
|
+
};
|
|
48
39
|
}
|
|
49
|
-
|
|
40
|
+
getTypes() {
|
|
50
41
|
return ['string'];
|
|
51
|
-
}
|
|
52
|
-
|
|
42
|
+
}
|
|
43
|
+
isValidType(value) {
|
|
53
44
|
return typeof value === 'string';
|
|
54
|
-
}
|
|
55
|
-
|
|
56
|
-
if (path === void 0) { path = []; }
|
|
45
|
+
}
|
|
46
|
+
validate(value, path = []) {
|
|
57
47
|
if (!this.isValidType(value)) {
|
|
58
|
-
throw new schema_1.Violation(
|
|
48
|
+
throw new schema_1.Violation(`Expected value of type string at path '${(0, violation_1.formatPath)(path)}', actual ${(0, violation_1.describe)(value)}.`, path, { type: 'string' });
|
|
59
49
|
}
|
|
60
|
-
|
|
50
|
+
const { minLength, maxLength } = this.definition;
|
|
61
51
|
if (minLength >= 0 && minLength > value.length) {
|
|
62
|
-
throw new schema_1.Violation(
|
|
63
|
-
+
|
|
64
|
-
+
|
|
52
|
+
throw new schema_1.Violation(`Expected ${minLength === maxLength ? 'exactly' : 'at least'} `
|
|
53
|
+
+ `${minLength} ${minLength === 1 ? 'character' : 'characters'} `
|
|
54
|
+
+ `at path '${(0, violation_1.formatPath)(path)}', actual ${value.length}.`, path, { limit: minLength });
|
|
65
55
|
}
|
|
66
56
|
if (maxLength >= 0 && maxLength < value.length) {
|
|
67
|
-
throw new schema_1.Violation(
|
|
68
|
-
+
|
|
69
|
-
+
|
|
57
|
+
throw new schema_1.Violation(`Expected ${minLength === maxLength ? 'exactly' : 'at most'} `
|
|
58
|
+
+ `${maxLength} ${maxLength === 1 ? 'character' : 'characters'} `
|
|
59
|
+
+ `at path '${(0, violation_1.formatPath)(path)}', actual ${value.length}.`, path, { limit: maxLength });
|
|
70
60
|
}
|
|
71
|
-
|
|
61
|
+
const { enumeration } = this.definition;
|
|
72
62
|
if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {
|
|
73
|
-
throw new schema_1.Violation(
|
|
74
|
-
+
|
|
63
|
+
throw new schema_1.Violation(`Unexpected value at path '${(0, violation_1.formatPath)(path)}', expecting `
|
|
64
|
+
+ `'${enumeration.length === 1
|
|
75
65
|
? enumeration[0]
|
|
76
|
-
:
|
|
77
|
-
+
|
|
66
|
+
: `${enumeration.slice(0, -1).join('\', \'')}' or '${enumeration.slice(-1)}`}', `
|
|
67
|
+
+ `found '${value}'.`, path, { enumeration: enumeration });
|
|
78
68
|
}
|
|
79
|
-
|
|
69
|
+
const { format, pattern } = this.definition;
|
|
80
70
|
if (format !== undefined && !FORMAT[format](value)) {
|
|
81
|
-
throw new schema_1.Violation(
|
|
71
|
+
throw new schema_1.Violation(`Invalid ${format} format at path '${(0, violation_1.formatPath)(path)}'.`, path, { format: format });
|
|
82
72
|
}
|
|
83
73
|
if (pattern !== undefined && !pattern.test(value)) {
|
|
84
|
-
throw new schema_1.Violation(
|
|
74
|
+
throw new schema_1.Violation(`Invalid format at path '${(0, violation_1.formatPath)(path)}'.`, path, { pattern: pattern });
|
|
85
75
|
}
|
|
86
|
-
}
|
|
87
|
-
|
|
88
|
-
}());
|
|
76
|
+
}
|
|
77
|
+
}
|
|
89
78
|
exports.StringType = StringType;
|
|
79
|
+
//# sourceMappingURL=stringType.js.map
|
package/validation/unionType.js
CHANGED
|
@@ -1,91 +1,42 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.UnionType = void 0;
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
var others = [];
|
|
10
|
-
for (var _i = 2; _i < arguments.length; _i++) {
|
|
11
|
-
others[_i - 2] = arguments[_i];
|
|
12
|
-
}
|
|
13
|
-
this.schemas = tslib_1.__spreadArray([first, second], tslib_1.__read(others), false);
|
|
4
|
+
const schema_1 = require("./schema");
|
|
5
|
+
const violation_1 = require("./violation");
|
|
6
|
+
class UnionType {
|
|
7
|
+
constructor(first, second, ...others) {
|
|
8
|
+
this.schemas = [first, second, ...others];
|
|
14
9
|
}
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
try {
|
|
22
|
-
for (var _e = (e_2 = void 0, tslib_1.__values(schema.getTypes())), _f = _e.next(); !_f.done; _f = _e.next()) {
|
|
23
|
-
var type = _f.value;
|
|
24
|
-
if (types.indexOf(type) < 0) {
|
|
25
|
-
types.push(type);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
28
|
-
}
|
|
29
|
-
catch (e_2_1) { e_2 = { error: e_2_1 }; }
|
|
30
|
-
finally {
|
|
31
|
-
try {
|
|
32
|
-
if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
|
|
33
|
-
}
|
|
34
|
-
finally { if (e_2) throw e_2.error; }
|
|
10
|
+
getTypes() {
|
|
11
|
+
const types = [];
|
|
12
|
+
for (const schema of this.schemas) {
|
|
13
|
+
for (const type of schema.getTypes()) {
|
|
14
|
+
if (types.indexOf(type) < 0) {
|
|
15
|
+
types.push(type);
|
|
35
16
|
}
|
|
36
17
|
}
|
|
37
18
|
}
|
|
38
|
-
catch (e_1_1) { e_1 = { error: e_1_1 }; }
|
|
39
|
-
finally {
|
|
40
|
-
try {
|
|
41
|
-
if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
|
|
42
|
-
}
|
|
43
|
-
finally { if (e_1) throw e_1.error; }
|
|
44
|
-
}
|
|
45
19
|
return types;
|
|
46
|
-
}
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
var schema = _c.value;
|
|
52
|
-
if (schema.isValidType(value)) {
|
|
53
|
-
return true;
|
|
54
|
-
}
|
|
55
|
-
}
|
|
56
|
-
}
|
|
57
|
-
catch (e_3_1) { e_3 = { error: e_3_1 }; }
|
|
58
|
-
finally {
|
|
59
|
-
try {
|
|
60
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
20
|
+
}
|
|
21
|
+
isValidType(value) {
|
|
22
|
+
for (const schema of this.schemas) {
|
|
23
|
+
if (schema.isValidType(value)) {
|
|
24
|
+
return true;
|
|
61
25
|
}
|
|
62
|
-
finally { if (e_3) throw e_3.error; }
|
|
63
26
|
}
|
|
64
27
|
return false;
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
|
|
68
|
-
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
var schema = _c.value;
|
|
72
|
-
if (schema.isValidType(value)) {
|
|
73
|
-
schema.validate(value, path);
|
|
74
|
-
return;
|
|
75
|
-
}
|
|
76
|
-
}
|
|
77
|
-
}
|
|
78
|
-
catch (e_4_1) { e_4 = { error: e_4_1 }; }
|
|
79
|
-
finally {
|
|
80
|
-
try {
|
|
81
|
-
if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
|
|
28
|
+
}
|
|
29
|
+
validate(value, path = []) {
|
|
30
|
+
for (const schema of this.schemas) {
|
|
31
|
+
if (schema.isValidType(value)) {
|
|
32
|
+
schema.validate(value, path);
|
|
33
|
+
return;
|
|
82
34
|
}
|
|
83
|
-
finally { if (e_4) throw e_4.error; }
|
|
84
35
|
}
|
|
85
|
-
|
|
86
|
-
throw new schema_1.Violation(
|
|
87
|
-
+
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
}());
|
|
36
|
+
const types = this.getTypes();
|
|
37
|
+
throw new schema_1.Violation(`Expected value of type ${types.slice(0, -1).join(', ')} or ${types[types.length - 1]} `
|
|
38
|
+
+ `at path '${(0, violation_1.formatPath)(path)}', actual ${(0, violation_1.describe)(value)}.`, path, { type: types.join('|') });
|
|
39
|
+
}
|
|
40
|
+
}
|
|
91
41
|
exports.UnionType = UnionType;
|
|
42
|
+
//# sourceMappingURL=unionType.js.map
|
package/validation/violation.js
CHANGED
|
@@ -12,13 +12,13 @@ function describe(value) {
|
|
|
12
12
|
return Number.isInteger(value) ? 'integer' : 'number';
|
|
13
13
|
}
|
|
14
14
|
if (typeof value === 'object') {
|
|
15
|
-
// eslint-disable-next-line @typescript-eslint/ban-types
|
|
16
15
|
return value.constructor.name;
|
|
17
16
|
}
|
|
18
17
|
return typeof value;
|
|
19
18
|
}
|
|
20
19
|
exports.describe = describe;
|
|
21
20
|
function formatPath(path) {
|
|
22
|
-
return
|
|
21
|
+
return `/${path.join('/')}`;
|
|
23
22
|
}
|
|
24
23
|
exports.formatPath = formatPath;
|
|
24
|
+
//# sourceMappingURL=violation.js.map
|