@croct/sdk 0.17.7 → 0.17.8
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/activeRecord.cjs.map +1 -0
- package/activeRecord.d.ts +8 -4
- package/activeRecord.js +115 -106
- package/activeRecord.js.map +1 -1
- package/apiKey.cjs.map +1 -0
- package/apiKey.d.ts +4 -2
- package/apiKey.js +152 -141
- package/apiKey.js.map +1 -1
- package/base64Url.cjs.map +1 -0
- package/base64Url.d.ts +4 -2
- package/base64Url.js +8 -15
- package/base64Url.js.map +1 -1
- package/cache/cache.cjs.map +1 -0
- package/cache/cache.d.ts +6 -4
- package/cache/cache.js +9 -10
- package/cache/cache.js.map +1 -1
- package/cache/cookieCache.cjs.map +1 -0
- package/cache/cookieCache.d.ts +6 -3
- package/cache/cookieCache.js +52 -54
- package/cache/cookieCache.js.map +1 -1
- package/cache/fallbackCache.cjs.map +1 -0
- package/cache/fallbackCache.d.ts +5 -2
- package/cache/fallbackCache.js +20 -21
- package/cache/fallbackCache.js.map +1 -1
- package/cache/inMemoryCache.cjs.map +1 -0
- package/cache/inMemoryCache.d.ts +5 -2
- package/cache/inMemoryCache.js +15 -17
- package/cache/inMemoryCache.js.map +1 -1
- package/cache/index.cjs.map +1 -0
- package/cache/index.d.ts +4 -4
- package/cache/index.js +9 -11
- package/cache/index.js.map +1 -1
- package/cache/localStorageCache.cjs.map +1 -0
- package/cache/localStorageCache.d.ts +5 -2
- package/cache/localStorageCache.js +55 -63
- package/cache/localStorageCache.js.map +1 -1
- package/channel/channel.cjs.map +1 -0
- package/channel/channel.d.ts +8 -6
- package/channel/channel.js +21 -22
- package/channel/channel.js.map +1 -1
- package/channel/encodedChannel.cjs.map +1 -0
- package/channel/encodedChannel.d.ts +6 -3
- package/channel/encodedChannel.js +13 -14
- package/channel/encodedChannel.js.map +1 -1
- package/channel/guaranteedChannel.cjs.map +1 -0
- package/channel/guaranteedChannel.d.ts +9 -7
- package/channel/guaranteedChannel.js +74 -73
- package/channel/guaranteedChannel.js.map +1 -1
- package/channel/httpBeaconChannel.cjs.map +1 -0
- package/channel/httpBeaconChannel.d.ts +9 -6
- package/channel/httpBeaconChannel.js +82 -83
- package/channel/httpBeaconChannel.js.map +1 -1
- package/channel/index.cjs.map +1 -0
- package/channel/index.d.ts +12 -7
- package/channel/index.js +15 -17
- package/channel/index.js.map +1 -1
- package/channel/queuedChannel.cjs.map +1 -0
- package/channel/queuedChannel.d.ts +7 -4
- package/channel/queuedChannel.js +87 -97
- package/channel/queuedChannel.js.map +1 -1
- package/channel/retryChannel.cjs.map +1 -0
- package/channel/retryChannel.d.ts +7 -5
- package/channel/retryChannel.js +58 -57
- package/channel/retryChannel.js.map +1 -1
- package/channel/sandboxChannel.cjs.map +1 -0
- package/channel/sandboxChannel.d.ts +5 -2
- package/channel/sandboxChannel.js +34 -35
- package/channel/sandboxChannel.js.map +1 -1
- package/cid/assigner.cjs.map +1 -0
- package/cid/assigner.d.ts +3 -1
- package/cid/assigner.js +0 -2
- package/cid/assigner.js.map +1 -1
- package/cid/cachedAssigner.cjs.map +1 -0
- package/cid/cachedAssigner.d.ts +7 -5
- package/cid/cachedAssigner.js +37 -43
- package/cid/cachedAssigner.js.map +1 -1
- package/cid/fixedAssigner.cjs.map +1 -0
- package/cid/fixedAssigner.d.ts +5 -2
- package/cid/fixedAssigner.js +9 -10
- package/cid/fixedAssigner.js.map +1 -1
- package/cid/index.cjs.map +1 -0
- package/cid/index.d.ts +6 -4
- package/cid/index.js +9 -11
- package/cid/index.js.map +1 -1
- package/cid/remoteAssigner.cjs.map +1 -0
- package/cid/remoteAssigner.d.ts +6 -3
- package/cid/remoteAssigner.js +36 -37
- package/cid/remoteAssigner.js.map +1 -1
- package/constants.cjs.map +1 -0
- package/constants.d.ts +6 -4
- package/constants.js +10 -7
- package/constants.js.map +1 -1
- package/container.cjs.map +1 -0
- package/container.d.ts +28 -15
- package/container.js +269 -252
- package/container.js.map +1 -1
- package/contentFetcher.cjs.map +1 -0
- package/contentFetcher.d.ts +17 -14
- package/contentFetcher.js +156 -163
- package/contentFetcher.js.map +1 -1
- package/context.cjs.map +1 -0
- package/context.d.ts +13 -9
- package/context.js +80 -81
- package/context.js.map +1 -1
- package/error.cjs.map +1 -0
- package/error.d.ts +4 -2
- package/error.js +21 -21
- package/error.js.map +1 -1
- package/evaluator.cjs.map +1 -0
- package/evaluator.d.ts +18 -16
- package/evaluator.js +179 -179
- package/evaluator.js.map +1 -1
- package/eventManager.cjs.map +1 -0
- package/eventManager.d.ts +8 -6
- package/eventManager.js +24 -26
- package/eventManager.js.map +1 -1
- package/eventSubjectProcessor.cjs.map +1 -0
- package/eventSubjectProcessor.d.ts +16 -3
- package/eventSubjectProcessor.js +53 -59
- package/eventSubjectProcessor.js.map +1 -1
- package/facade/contentFetcherFacade.cjs.map +1 -0
- package/facade/contentFetcherFacade.d.ts +16 -7
- package/facade/contentFetcherFacade.js +32 -35
- package/facade/contentFetcherFacade.js.map +1 -1
- package/facade/evaluatorFacade.cjs.map +1 -0
- package/facade/evaluatorFacade.d.ts +17 -10
- package/facade/evaluatorFacade.js +56 -60
- package/facade/evaluatorFacade.js.map +1 -1
- package/facade/index.cjs.map +1 -0
- package/facade/index.d.ts +29 -7
- package/facade/index.js +19 -19
- package/facade/index.js.map +1 -1
- package/facade/sdkFacade.cjs.map +1 -0
- package/facade/sdkFacade.d.ts +33 -15
- package/facade/sdkFacade.js +201 -214
- package/facade/sdkFacade.js.map +1 -1
- package/facade/sessionFacade.cjs.map +1 -0
- package/facade/sessionFacade.d.ts +18 -3
- package/facade/sessionFacade.js +10 -11
- package/facade/sessionFacade.js.map +1 -1
- package/facade/sessionPatch.cjs.map +1 -0
- package/facade/sessionPatch.d.ts +17 -4
- package/facade/sessionPatch.js +22 -24
- package/facade/sessionPatch.js.map +1 -1
- package/facade/trackerFacade.cjs.map +1 -0
- package/facade/trackerFacade.d.ts +17 -4
- package/facade/trackerFacade.js +67 -57
- package/facade/trackerFacade.js.map +1 -1
- package/facade/userFacade.cjs.map +1 -0
- package/facade/userFacade.d.ts +21 -4
- package/facade/userFacade.js +17 -18
- package/facade/userFacade.js.map +1 -1
- package/facade/userPatch.cjs.map +1 -0
- package/facade/userPatch.d.ts +17 -4
- package/facade/userPatch.js +22 -24
- package/facade/userPatch.js.map +1 -1
- package/help.cjs.map +1 -0
- package/help.d.ts +3 -1
- package/help.js +19 -23
- package/help.js.map +1 -1
- package/index.cjs.map +1 -0
- package/index.d.ts +25 -3
- package/index.js +7 -7
- package/index.js.map +1 -1
- package/logging/consoleLogger.cjs.map +1 -0
- package/logging/consoleLogger.d.ts +5 -2
- package/logging/consoleLogger.js +23 -24
- package/logging/consoleLogger.js.map +1 -1
- package/logging/filteredLogger.cjs.map +1 -0
- package/logging/filteredLogger.d.ts +8 -5
- package/logging/filteredLogger.js +36 -37
- package/logging/filteredLogger.js.map +1 -1
- package/logging/index.cjs.map +1 -0
- package/logging/index.d.ts +5 -5
- package/logging/index.js +11 -13
- package/logging/index.js.map +1 -1
- package/logging/logger.cjs.map +1 -0
- package/logging/logger.d.ts +4 -2
- package/logging/logger.js +0 -2
- package/logging/logger.js.map +1 -1
- package/logging/namespacedLogger.cjs.map +1 -0
- package/logging/namespacedLogger.d.ts +5 -2
- package/logging/namespacedLogger.js +22 -23
- package/logging/namespacedLogger.js.map +1 -1
- package/logging/nullLogger.cjs.map +1 -0
- package/logging/nullLogger.d.ts +5 -2
- package/logging/nullLogger.js +11 -16
- package/logging/nullLogger.js.map +1 -1
- package/namespacedStorage.cjs.map +1 -0
- package/namespacedStorage.d.ts +3 -1
- package/namespacedStorage.js +51 -52
- package/namespacedStorage.js.map +1 -1
- package/package.json +60 -6
- package/patch.cjs.map +1 -0
- package/patch.d.ts +6 -4
- package/patch.js +0 -2
- package/patch.js.map +1 -1
- package/queue/capacityRestrictedQueue.cjs.map +1 -0
- package/queue/capacityRestrictedQueue.d.ts +5 -2
- package/queue/capacityRestrictedQueue.js +31 -32
- package/queue/capacityRestrictedQueue.js.map +1 -1
- package/queue/inMemoryQueue.cjs.map +1 -0
- package/queue/inMemoryQueue.d.ts +5 -2
- package/queue/inMemoryQueue.js +32 -34
- package/queue/inMemoryQueue.js.map +1 -1
- package/queue/index.cjs.map +1 -0
- package/queue/index.d.ts +6 -5
- package/queue/index.js +11 -13
- package/queue/index.js.map +1 -1
- package/queue/monitoredQueue.cjs.map +1 -0
- package/queue/monitoredQueue.d.ts +8 -5
- package/queue/monitoredQueue.js +104 -110
- package/queue/monitoredQueue.js.map +1 -1
- package/queue/persistentQueue.cjs.map +1 -0
- package/queue/persistentQueue.d.ts +5 -2
- package/queue/persistentQueue.js +52 -54
- package/queue/persistentQueue.js.map +1 -1
- package/queue/queue.cjs.map +1 -0
- package/queue/queue.d.ts +3 -1
- package/queue/queue.js +0 -2
- package/queue/queue.js.map +1 -1
- package/retry/arbitraryPolicy.cjs.map +1 -0
- package/retry/arbitraryPolicy.d.ts +5 -2
- package/retry/arbitraryPolicy.js +14 -15
- package/retry/arbitraryPolicy.js.map +1 -1
- package/retry/backoffPolicy.cjs.map +1 -0
- package/retry/backoffPolicy.d.ts +5 -3
- package/retry/backoffPolicy.js +52 -50
- package/retry/backoffPolicy.js.map +1 -1
- package/retry/index.cjs.map +1 -0
- package/retry/index.d.ts +5 -5
- package/retry/index.js +11 -13
- package/retry/index.js.map +1 -1
- package/retry/maxAttemptsPolicy.cjs.map +1 -0
- package/retry/maxAttemptsPolicy.d.ts +5 -2
- package/retry/maxAttemptsPolicy.js +17 -18
- package/retry/maxAttemptsPolicy.js.map +1 -1
- package/retry/neverPolicy.cjs.map +1 -0
- package/retry/neverPolicy.d.ts +5 -2
- package/retry/neverPolicy.js +9 -10
- package/retry/neverPolicy.js.map +1 -1
- package/retry/policy.cjs.map +1 -0
- package/retry/policy.d.ts +3 -1
- package/retry/policy.js +0 -2
- package/retry/policy.js.map +1 -1
- package/schema/attributeSchema.cjs.map +1 -0
- package/schema/attributeSchema.d.ts +6 -2
- package/schema/attributeSchema.js +7 -7
- package/schema/attributeSchema.js.map +1 -1
- package/schema/contentFetcherSchemas.cjs.map +1 -0
- package/schema/contentFetcherSchemas.d.ts +6 -2
- package/schema/contentFetcherSchemas.js +24 -21
- package/schema/contentFetcherSchemas.js.map +1 -1
- package/schema/contentSchemas.cjs.map +1 -0
- package/schema/contentSchemas.d.ts +6 -2
- package/schema/contentSchemas.js +45 -45
- package/schema/contentSchemas.js.map +1 -1
- package/schema/contextSchemas.cjs.map +1 -0
- package/schema/contextSchemas.d.ts +6 -2
- package/schema/contextSchemas.js +6 -6
- package/schema/contextSchemas.js.map +1 -1
- package/schema/ecommerceSchemas.cjs.map +1 -0
- package/schema/ecommerceSchemas.d.ts +10 -6
- package/schema/ecommerceSchemas.js +176 -172
- package/schema/ecommerceSchemas.js.map +1 -1
- package/schema/evaluatorSchemas.cjs.map +1 -0
- package/schema/evaluatorSchemas.d.ts +6 -2
- package/schema/evaluatorSchemas.js +46 -30
- package/schema/evaluatorSchemas.js.map +1 -1
- package/schema/eventSchemas.cjs.map +1 -0
- package/schema/eventSchemas.d.ts +16 -12
- package/schema/eventSchemas.js +125 -102
- package/schema/eventSchemas.js.map +1 -1
- package/schema/index.cjs.map +1 -0
- package/schema/index.d.ts +14 -11
- package/schema/index.js +11 -14
- package/schema/index.js.map +1 -1
- package/schema/loggerSchema.cjs.map +1 -0
- package/schema/loggerSchema.d.ts +6 -2
- package/schema/loggerSchema.js +13 -13
- package/schema/loggerSchema.js.map +1 -1
- package/schema/operationSchemas.cjs.map +1 -0
- package/schema/operationSchemas.d.ts +14 -10
- package/schema/operationSchemas.js +85 -69
- package/schema/operationSchemas.js.map +1 -1
- package/schema/sdkFacadeSchemas.cjs.map +1 -0
- package/schema/sdkFacadeSchemas.d.ts +6 -2
- package/schema/sdkFacadeSchemas.js +65 -51
- package/schema/sdkFacadeSchemas.js.map +1 -1
- package/schema/sdkSchemas.cjs.map +1 -0
- package/schema/sdkSchemas.d.ts +8 -4
- package/schema/sdkSchemas.js +80 -78
- package/schema/sdkSchemas.js.map +1 -1
- package/schema/tokenSchema.cjs.map +1 -0
- package/schema/tokenSchema.d.ts +6 -2
- package/schema/tokenSchema.js +41 -38
- package/schema/tokenSchema.js.map +1 -1
- package/schema/userSchema.cjs.map +1 -0
- package/schema/userSchema.d.ts +6 -2
- package/schema/userSchema.js +183 -134
- package/schema/userSchema.js.map +1 -1
- package/sdk.cjs.map +1 -0
- package/sdk.d.ts +28 -14
- package/sdk.js +106 -108
- package/sdk.js.map +1 -1
- package/sdkEvents.cjs.map +1 -0
- package/sdkEvents.d.ts +10 -5
- package/sdkEvents.js +0 -2
- package/sdkEvents.js.map +1 -1
- package/sourceLocation.cjs.map +1 -0
- package/sourceLocation.d.ts +7 -5
- package/sourceLocation.js +56 -57
- package/sourceLocation.js.map +1 -1
- package/tab.cjs.map +1 -0
- package/tab.d.ts +9 -7
- package/tab.js +93 -90
- package/tab.js.map +1 -1
- package/token/cachedTokenStore.cjs.map +1 -0
- package/token/cachedTokenStore.d.ts +8 -3
- package/token/cachedTokenStore.js +22 -24
- package/token/cachedTokenStore.js.map +1 -1
- package/token/inMemoryTokenStore.cjs.map +1 -0
- package/token/inMemoryTokenStore.d.ts +7 -2
- package/token/inMemoryTokenStore.js +12 -13
- package/token/inMemoryTokenStore.js.map +1 -1
- package/token/index.cjs.map +1 -0
- package/token/index.d.ts +7 -4
- package/token/index.js +9 -11
- package/token/index.js.map +1 -1
- package/token/replicatedTokenStore.cjs.map +1 -0
- package/token/replicatedTokenStore.d.ts +7 -2
- package/token/replicatedTokenStore.js +14 -15
- package/token/replicatedTokenStore.js.map +1 -1
- package/token/token.cjs.map +1 -0
- package/token/token.d.ts +11 -8
- package/token/token.js +212 -195
- package/token/token.js.map +1 -1
- package/tracker.cjs.map +1 -0
- package/tracker.d.ts +20 -13
- package/tracker.js +307 -307
- package/tracker.js.map +1 -1
- package/trackingEvents.cjs.map +1 -0
- package/trackingEvents.d.ts +65 -62
- package/trackingEvents.js +57 -56
- package/trackingEvents.js.map +1 -1
- package/transformer.cjs.map +1 -0
- package/transformer.d.ts +4 -2
- package/transformer.js +5 -6
- package/transformer.js.map +1 -1
- package/utilityTypes.cjs.map +1 -0
- package/utilityTypes.d.ts +4 -2
- package/utilityTypes.js +0 -2
- package/utilityTypes.js.map +1 -1
- package/uuid.cjs.map +1 -0
- package/uuid.d.ts +3 -1
- package/uuid.js +27 -32
- package/uuid.js.map +1 -1
- package/validation/arrayType.cjs.map +1 -0
- package/validation/arrayType.d.ts +5 -3
- package/validation/arrayType.js +45 -41
- package/validation/arrayType.js.map +1 -1
- package/validation/booleanType.cjs.map +1 -0
- package/validation/booleanType.d.ts +5 -2
- package/validation/booleanType.js +19 -16
- package/validation/booleanType.js.map +1 -1
- package/validation/functionType.cjs.map +1 -0
- package/validation/functionType.d.ts +5 -2
- package/validation/functionType.js +19 -16
- package/validation/functionType.js.map +1 -1
- package/validation/index.cjs.map +1 -0
- package/validation/index.d.ts +12 -12
- package/validation/index.js +27 -29
- package/validation/index.js.map +1 -1
- package/validation/jsonType.cjs.map +1 -0
- package/validation/jsonType.d.ts +8 -6
- package/validation/jsonType.js +89 -82
- package/validation/jsonType.js.map +1 -1
- package/validation/mixedSchema.cjs.map +1 -0
- package/validation/mixedSchema.d.ts +5 -2
- package/validation/mixedSchema.js +5 -7
- package/validation/mixedSchema.js.map +1 -1
- package/validation/nullType.cjs.map +1 -0
- package/validation/nullType.d.ts +5 -2
- package/validation/nullType.js +19 -16
- package/validation/nullType.js.map +1 -1
- package/validation/numberType.cjs.map +1 -0
- package/validation/numberType.d.ts +5 -3
- package/validation/numberType.js +40 -32
- package/validation/numberType.js.map +1 -1
- package/validation/objectType.cjs.map +1 -0
- package/validation/objectType.d.ts +5 -3
- package/validation/objectType.js +84 -85
- package/validation/objectType.js.map +1 -1
- package/validation/schema.cjs.map +1 -0
- package/validation/schema.d.ts +5 -3
- package/validation/schema.js +8 -9
- package/validation/schema.js.map +1 -1
- package/validation/stringType.cjs.map +1 -0
- package/validation/stringType.d.ts +5 -3
- package/validation/stringType.js +74 -70
- package/validation/stringType.js.map +1 -1
- package/validation/unionType.cjs.map +1 -0
- package/validation/unionType.d.ts +5 -2
- package/validation/unionType.js +37 -35
- package/validation/unionType.js.map +1 -1
- package/validation/violation.cjs.map +1 -0
- package/validation/violation.d.ts +4 -2
- package/validation/violation.js +18 -18
- package/validation/violation.js.map +1 -1
package/validation/objectType.js
CHANGED
|
@@ -1,92 +1,91 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
const mixedSchema_1 = require("./mixedSchema");
|
|
6
|
-
const violation_1 = require("./violation");
|
|
1
|
+
import { Violation } from "./schema";
|
|
2
|
+
import { MixedSchema } from "./mixedSchema";
|
|
3
|
+
import { describe, formatPath } from "./violation";
|
|
7
4
|
class ObjectType {
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
5
|
+
constructor(schema = {}) {
|
|
6
|
+
this.definition = {
|
|
7
|
+
...schema,
|
|
8
|
+
properties: schema.properties ?? {},
|
|
9
|
+
required: schema.required ?? [],
|
|
10
|
+
additionalProperties: schema.additionalProperties ?? false,
|
|
11
|
+
propertyNames: schema.propertyNames ?? new MixedSchema(),
|
|
12
|
+
minProperties: schema.minProperties ?? -1,
|
|
13
|
+
maxProperties: schema.maxProperties ?? -1
|
|
14
|
+
};
|
|
15
|
+
}
|
|
16
|
+
getTypes() {
|
|
17
|
+
if (this.definition.type !== void 0) {
|
|
18
|
+
return [this.definition.type.name];
|
|
19
19
|
}
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
20
|
+
return ["object"];
|
|
21
|
+
}
|
|
22
|
+
isValidType(value) {
|
|
23
|
+
if (this.definition.type !== void 0) {
|
|
24
|
+
return value instanceof this.definition.type;
|
|
25
25
|
}
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
26
|
+
return Object.prototype.toString.call(value) === "[object Object]";
|
|
27
|
+
}
|
|
28
|
+
validate(value, path = []) {
|
|
29
|
+
if (!this.isValidType(value)) {
|
|
30
|
+
const [type] = this.getTypes();
|
|
31
|
+
throw new Violation(
|
|
32
|
+
`Expected value of type ${type} at path '${formatPath(path)}', actual ${describe(value)}.`,
|
|
33
|
+
path,
|
|
34
|
+
{ type }
|
|
35
|
+
);
|
|
33
36
|
}
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
if (minProperties >= 0 && minProperties > entries.length) {
|
|
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 });
|
|
46
|
-
}
|
|
47
|
-
if (maxProperties >= 0 && maxProperties < entries.length) {
|
|
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 });
|
|
51
|
-
}
|
|
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
|
-
});
|
|
58
|
-
}
|
|
59
|
-
}
|
|
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;
|
|
70
|
-
}
|
|
71
|
-
if (this.definition.additionalProperties === false) {
|
|
72
|
-
throw new schema_1.Violation(`Unknown property '${(0, violation_1.formatPath)(propertyPath)}'.`, propertyPath, {
|
|
73
|
-
additionalProperty: entryName,
|
|
74
|
-
});
|
|
75
|
-
}
|
|
76
|
-
if (this.definition.additionalProperties !== true) {
|
|
77
|
-
this.definition
|
|
78
|
-
.additionalProperties
|
|
79
|
-
.validate(entryValue, propertyPath);
|
|
80
|
-
}
|
|
81
|
-
}
|
|
82
|
-
const { subtypes } = this.definition;
|
|
83
|
-
if (subtypes !== undefined) {
|
|
84
|
-
const type = value[subtypes.discriminator];
|
|
85
|
-
if (type !== undefined && subtypes.schemas[type] !== undefined) {
|
|
86
|
-
subtypes.schemas[type].validate(additionalProperties, path);
|
|
87
|
-
}
|
|
88
|
-
}
|
|
37
|
+
const entries = Object.entries(value);
|
|
38
|
+
const { minProperties, maxProperties } = this.definition;
|
|
39
|
+
if (minProperties >= 0 && minProperties > entries.length) {
|
|
40
|
+
throw new Violation(
|
|
41
|
+
`Expected ${minProperties === maxProperties ? "exactly" : "at least"} ${minProperties} ${minProperties === 1 ? "entry" : "entries"} at path '${formatPath(path)}', actual ${entries.length}.`,
|
|
42
|
+
path,
|
|
43
|
+
{ limit: minProperties }
|
|
44
|
+
);
|
|
89
45
|
}
|
|
46
|
+
if (maxProperties >= 0 && maxProperties < entries.length) {
|
|
47
|
+
throw new Violation(
|
|
48
|
+
`Expected ${minProperties === maxProperties ? "exactly" : "at most"} ${maxProperties} ${maxProperties === 1 ? "entry" : "entries"} at path '${formatPath(path)}', actual ${entries.length}.`,
|
|
49
|
+
path,
|
|
50
|
+
{ limit: maxProperties }
|
|
51
|
+
);
|
|
52
|
+
}
|
|
53
|
+
const additionalProperties = { ...value };
|
|
54
|
+
for (const property of this.definition.required) {
|
|
55
|
+
if (!(property in value)) {
|
|
56
|
+
throw new Violation(`Missing property '${formatPath(path.concat([property]))}'.`, path, {
|
|
57
|
+
required: property
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const [entryName, entryValue] of entries) {
|
|
62
|
+
const propertyPath = path.concat([entryName]);
|
|
63
|
+
this.definition.propertyNames.validate(entryName, propertyPath);
|
|
64
|
+
const propertyRule = this.definition.properties[entryName];
|
|
65
|
+
if (propertyRule !== void 0) {
|
|
66
|
+
propertyRule.validate(entryValue, propertyPath);
|
|
67
|
+
delete additionalProperties[entryName];
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
if (this.definition.additionalProperties === false) {
|
|
71
|
+
throw new Violation(`Unknown property '${formatPath(propertyPath)}'.`, propertyPath, {
|
|
72
|
+
additionalProperty: entryName
|
|
73
|
+
});
|
|
74
|
+
}
|
|
75
|
+
if (this.definition.additionalProperties !== true) {
|
|
76
|
+
this.definition.additionalProperties.validate(entryValue, propertyPath);
|
|
77
|
+
}
|
|
78
|
+
}
|
|
79
|
+
const { subtypes } = this.definition;
|
|
80
|
+
if (subtypes !== void 0) {
|
|
81
|
+
const type = value[subtypes.discriminator];
|
|
82
|
+
if (type !== void 0 && subtypes.schemas[type] !== void 0) {
|
|
83
|
+
subtypes.schemas[type].validate(additionalProperties, path);
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
90
87
|
}
|
|
91
|
-
|
|
88
|
+
export {
|
|
89
|
+
ObjectType
|
|
90
|
+
};
|
|
92
91
|
//# sourceMappingURL=objectType.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/validation/objectType.ts"],"sourcesContent":["import {Schema, TypeSchema, Violation} from './schema';\nimport {MixedSchema} from './mixedSchema';\nimport {describe, formatPath} from './violation';\n\ntype ObjectDefinition = {\n type?: {new (...args: any): any},\n properties: {[key: string]: Schema},\n additionalProperties: boolean | Schema,\n subtypes?: {\n discriminator: string,\n schemas: {[key: string]: ObjectType},\n },\n propertyNames: Schema,\n required: string[],\n minProperties: number,\n maxProperties: number,\n};\n\nexport class ObjectType implements TypeSchema {\n private readonly definition: ObjectDefinition;\n\n public constructor(schema: Partial<ObjectDefinition> = {}) {\n this.definition = {\n ...schema,\n properties: schema.properties ?? {},\n required: schema.required ?? [],\n additionalProperties: schema.additionalProperties ?? false,\n propertyNames: schema.propertyNames ?? new MixedSchema(),\n minProperties: schema.minProperties ?? -1,\n maxProperties: schema.maxProperties ?? -1,\n };\n }\n\n public getTypes(): string[] {\n if (this.definition.type !== undefined) {\n return [this.definition.type.name];\n }\n\n return ['object'];\n }\n\n public isValidType(value: unknown): value is object {\n if (this.definition.type !== undefined) {\n return value instanceof this.definition.type;\n }\n\n return Object.prototype\n .toString\n .call(value) === '[object Object]';\n }\n\n public validate(value: unknown, path: string[] = []): void {\n if (!this.isValidType(value)) {\n const [type] = this.getTypes();\n\n throw new Violation(\n `Expected value of type ${type} at path '${formatPath(path)}', `\n + `actual ${describe(value)}.`,\n path,\n {type: type},\n );\n }\n\n const entries = Object.entries(value);\n\n const {minProperties, maxProperties} = this.definition;\n\n if (minProperties >= 0 && minProperties > entries.length) {\n throw new Violation(\n `Expected ${minProperties === maxProperties ? 'exactly' : 'at least'} `\n + `${minProperties} ${minProperties === 1 ? 'entry' : 'entries'} `\n + `at path '${formatPath(path)}', actual ${entries.length}.`,\n path,\n {limit: minProperties},\n );\n }\n\n if (maxProperties >= 0 && maxProperties < entries.length) {\n throw new Violation(\n `Expected ${minProperties === maxProperties ? 'exactly' : 'at most'} `\n + `${maxProperties} ${maxProperties === 1 ? 'entry' : 'entries'} `\n + `at path '${formatPath(path)}', actual ${entries.length}.`,\n path,\n {limit: maxProperties},\n );\n }\n\n const additionalProperties: {[key: string]: any} = {...value};\n\n for (const property of this.definition.required) {\n if (!(property in value)) {\n throw new Violation(`Missing property '${formatPath(path.concat([property]))}'.`, path, {\n required: property,\n });\n }\n }\n\n for (const [entryName, entryValue] of entries) {\n const propertyPath = path.concat([entryName]);\n\n this.definition\n .propertyNames\n .validate(entryName, propertyPath);\n\n const propertyRule = this.definition.properties[entryName];\n\n if (propertyRule !== undefined) {\n propertyRule.validate(entryValue, propertyPath);\n\n delete additionalProperties[entryName];\n\n continue;\n }\n\n if (this.definition.additionalProperties === false) {\n throw new Violation(`Unknown property '${formatPath(propertyPath)}'.`, propertyPath, {\n additionalProperty: entryName,\n });\n }\n\n if (this.definition.additionalProperties !== true) {\n this.definition\n .additionalProperties\n .validate(entryValue, propertyPath);\n }\n }\n\n const {subtypes} = this.definition;\n\n if (subtypes !== undefined) {\n const type = (value as any)[subtypes.discriminator];\n\n if (type !== undefined && subtypes.schemas[type] !== undefined) {\n subtypes.schemas[type].validate(additionalProperties, path);\n }\n }\n }\n}\n"],"mappings":"AAAA,SAA4B,iBAAgB;AAC5C,SAAQ,mBAAkB;AAC1B,SAAQ,UAAU,kBAAiB;AAgB5B,MAAM,WAAiC;AAAA,EAGnC,YAAY,SAAoC,CAAC,GAAG;AACvD,SAAK,aAAa;AAAA,MACd,GAAG;AAAA,MACH,YAAY,OAAO,cAAc,CAAC;AAAA,MAClC,UAAU,OAAO,YAAY,CAAC;AAAA,MAC9B,sBAAsB,OAAO,wBAAwB;AAAA,MACrD,eAAe,OAAO,iBAAiB,IAAI,YAAY;AAAA,MACvD,eAAe,OAAO,iBAAiB;AAAA,MACvC,eAAe,OAAO,iBAAiB;AAAA,IAC3C;AAAA,EACJ;AAAA,EAEO,WAAqB;AACxB,QAAI,KAAK,WAAW,SAAS,QAAW;AACpC,aAAO,CAAC,KAAK,WAAW,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;AACtB,cAAM,IAAI,UAAU,qBAAqB,WAAW,KAAK,OAAO,CAAC,QAAQ,CAAC,CAAC,CAAC,MAAM,MAAM;AAAA,UACpF,UAAU;AAAA,QACd,CAAC;AAAA,MACL;AAAA,IACJ;AAEA,eAAW,CAAC,WAAW,UAAU,KAAK,SAAS;AAC3C,YAAM,eAAe,KAAK,OAAO,CAAC,SAAS,CAAC;AAE5C,WAAK,WACA,cACA,SAAS,WAAW,YAAY;AAErC,YAAM,eAAe,KAAK,WAAW,WAAW,SAAS;AAEzD,UAAI,iBAAiB,QAAW;AAC5B,qBAAa,SAAS,YAAY,YAAY;AAE9C,eAAO,qBAAqB,SAAS;AAErC;AAAA,MACJ;AAEA,UAAI,KAAK,WAAW,yBAAyB,OAAO;AAChD,cAAM,IAAI,UAAU,qBAAqB,WAAW,YAAY,CAAC,MAAM,cAAc;AAAA,UACjF,oBAAoB;AAAA,QACxB,CAAC;AAAA,MACL;AAEA,UAAI,KAAK,WAAW,yBAAyB,MAAM;AAC/C,aAAK,WACA,qBACA,SAAS,YAAY,YAAY;AAAA,MAC1C;AAAA,IACJ;AAEA,UAAM,EAAC,SAAQ,IAAI,KAAK;AAExB,QAAI,aAAa,QAAW;AACxB,YAAM,OAAQ,MAAc,SAAS,aAAa;AAElD,UAAI,SAAS,UAAa,SAAS,QAAQ,IAAI,MAAM,QAAW;AAC5D,iBAAS,QAAQ,IAAI,EAAE,SAAS,sBAAsB,IAAI;AAAA,MAC9D;AAAA,IACJ;AAAA,EACJ;AACJ;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/validation/schema.ts"],"sourcesContent":["export interface Schema {\n validate(value: unknown, path?: string[]): void;\n}\n\nexport interface TypeSchema extends Schema {\n getTypes(): string[];\n\n isValidType(value: unknown): boolean;\n}\n\nexport class Violation extends Error {\n public readonly path: string[];\n\n public readonly params: {[key: string]: unknown};\n\n public constructor(message: string, path: string[], params: {[p: string]: unknown}) {\n super(message);\n this.path = path;\n this.params = params;\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAUO,MAAM,kBAAkB,MAAM;AAAA,EAK1B,YAAY,SAAiB,MAAgB,QAAgC;AAChF,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAClB;AACJ;","names":[]}
|
package/validation/schema.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
interface Schema {
|
|
2
2
|
validate(value: unknown, path?: string[]): void;
|
|
3
3
|
}
|
|
4
|
-
|
|
4
|
+
interface TypeSchema extends Schema {
|
|
5
5
|
getTypes(): string[];
|
|
6
6
|
isValidType(value: unknown): boolean;
|
|
7
7
|
}
|
|
8
|
-
|
|
8
|
+
declare class Violation extends Error {
|
|
9
9
|
readonly path: string[];
|
|
10
10
|
readonly params: {
|
|
11
11
|
[key: string]: unknown;
|
|
@@ -14,3 +14,5 @@ export declare class Violation extends Error {
|
|
|
14
14
|
[p: string]: unknown;
|
|
15
15
|
});
|
|
16
16
|
}
|
|
17
|
+
|
|
18
|
+
export { type Schema, type TypeSchema, Violation };
|
package/validation/schema.js
CHANGED
|
@@ -1,12 +1,11 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.Violation = void 0;
|
|
4
1
|
class Violation extends Error {
|
|
5
|
-
|
|
6
|
-
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
2
|
+
constructor(message, path, params) {
|
|
3
|
+
super(message);
|
|
4
|
+
this.path = path;
|
|
5
|
+
this.params = params;
|
|
6
|
+
}
|
|
10
7
|
}
|
|
11
|
-
|
|
8
|
+
export {
|
|
9
|
+
Violation
|
|
10
|
+
};
|
|
12
11
|
//# sourceMappingURL=schema.js.map
|
package/validation/schema.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/validation/schema.ts"],"sourcesContent":["export interface Schema {\n validate(value: unknown, path?: string[]): void;\n}\n\nexport interface TypeSchema extends Schema {\n getTypes(): string[];\n\n isValidType(value: unknown): boolean;\n}\n\nexport class Violation extends Error {\n public readonly path: string[];\n\n public readonly params: {[key: string]: unknown};\n\n public constructor(message: string, path: string[], params: {[p: string]: unknown}) {\n super(message);\n this.path = path;\n this.params = params;\n }\n}\n"],"mappings":"AAUO,MAAM,kBAAkB,MAAM;AAAA,EAK1B,YAAY,SAAiB,MAAgB,QAAgC;AAChF,UAAM,OAAO;AACb,SAAK,OAAO;AACZ,SAAK,SAAS;AAAA,EAClB;AACJ;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/validation/stringType.ts"],"sourcesContent":["import {TypeSchema, Violation} from './schema';\nimport {describe, formatPath} from './violation';\n\ninterface Format {\n (value: string): boolean;\n}\n\nconst FORMAT: {[key: string]: Format} = {\n pointer: function pointer(value: string): boolean {\n return /^(\\.|([a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])(\\.[a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])*)$/.test(value);\n },\n identifier: function identifier(value: string): boolean {\n return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);\n },\n uuid: function uuid(value: string): boolean {\n return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);\n },\n date: function date(value: string): boolean {\n return /^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)$/.test(value);\n },\n url: function url(value: string): boolean {\n try {\n // eslint-disable-next-line no-new -- Easier way to validate a URL\n new URL(value);\n } catch {\n return false;\n }\n\n return true;\n },\n};\n\ntype StringDefinition = {\n minLength: number,\n maxLength: number,\n enumeration: string[],\n pattern?: RegExp,\n format?: keyof typeof FORMAT,\n};\n\nexport class StringType implements TypeSchema {\n private readonly definition: StringDefinition;\n\n public constructor(definition: Partial<StringDefinition> = {}) {\n this.definition = {\n ...definition,\n minLength: definition.minLength ?? -1,\n maxLength: definition.maxLength ?? -1,\n enumeration: definition.enumeration ?? [],\n };\n }\n\n public getTypes(): string[] {\n return ['string'];\n }\n\n public isValidType(value: unknown): value is string {\n return typeof value === 'string';\n }\n\n public validate(value: unknown, path: string[] = []): void {\n if (!this.isValidType(value)) {\n throw new Violation(\n `Expected value of type string at path '${formatPath(path)}', actual ${describe(value)}.`,\n path,\n {type: 'string'},\n );\n }\n\n const {minLength, maxLength} = this.definition;\n\n if (minLength >= 0 && minLength > value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at least'} `\n + `${minLength} ${minLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: minLength},\n );\n }\n\n if (maxLength >= 0 && maxLength < value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at most'} `\n + `${maxLength} ${maxLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: maxLength},\n );\n }\n\n const {enumeration} = this.definition;\n\n if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {\n throw new Violation(\n `Unexpected value at path '${formatPath(path)}', expecting `\n + `'${\n enumeration.length === 1\n ? enumeration[0]\n : `${enumeration.slice(0, -1).join('\\', \\'')}' or '${enumeration.slice(-1)}`\n }', `\n + `found '${value}'.`,\n path,\n {enumeration: enumeration},\n );\n }\n\n const {format, pattern} = this.definition;\n\n if (format !== undefined && !FORMAT[format](value)) {\n throw new Violation(`Invalid ${format} format at path '${formatPath(path)}'.`, path, {format: format});\n }\n\n if (pattern !== undefined && !pattern.test(value)) {\n throw new Violation(`Invalid format at path '${formatPath(path)}'.`, path, {pattern: pattern});\n }\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoC;AACpC,uBAAmC;AAMnC,MAAM,SAAkC;AAAA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
|
3
4
|
(value: string): boolean;
|
|
4
5
|
}
|
|
@@ -12,11 +13,12 @@ type StringDefinition = {
|
|
|
12
13
|
pattern?: RegExp;
|
|
13
14
|
format?: keyof typeof FORMAT;
|
|
14
15
|
};
|
|
15
|
-
|
|
16
|
+
declare class StringType implements TypeSchema {
|
|
16
17
|
private readonly definition;
|
|
17
18
|
constructor(definition?: Partial<StringDefinition>);
|
|
18
19
|
getTypes(): string[];
|
|
19
20
|
isValidType(value: unknown): value is string;
|
|
20
21
|
validate(value: unknown, path?: string[]): void;
|
|
21
22
|
}
|
|
22
|
-
|
|
23
|
+
|
|
24
|
+
export { StringType };
|
package/validation/stringType.js
CHANGED
|
@@ -1,79 +1,83 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.StringType = void 0;
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
const violation_1 = require("./violation");
|
|
1
|
+
import { Violation } from "./schema";
|
|
2
|
+
import { describe, formatPath } from "./violation";
|
|
6
3
|
const FORMAT = {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
return true;
|
|
28
|
-
},
|
|
4
|
+
pointer: function pointer(value) {
|
|
5
|
+
return /^(\.|([a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])(\.[a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])*)$/.test(value);
|
|
6
|
+
},
|
|
7
|
+
identifier: function identifier(value) {
|
|
8
|
+
return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);
|
|
9
|
+
},
|
|
10
|
+
uuid: function uuid(value) {
|
|
11
|
+
return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);
|
|
12
|
+
},
|
|
13
|
+
date: function date(value) {
|
|
14
|
+
return /^(\d\d\d\d)-(\d\d)-(\d\d)$/.test(value);
|
|
15
|
+
},
|
|
16
|
+
url: function url(value) {
|
|
17
|
+
try {
|
|
18
|
+
new URL(value);
|
|
19
|
+
} catch {
|
|
20
|
+
return false;
|
|
21
|
+
}
|
|
22
|
+
return true;
|
|
23
|
+
}
|
|
29
24
|
};
|
|
30
25
|
class StringType {
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
26
|
+
constructor(definition = {}) {
|
|
27
|
+
this.definition = {
|
|
28
|
+
...definition,
|
|
29
|
+
minLength: definition.minLength ?? -1,
|
|
30
|
+
maxLength: definition.maxLength ?? -1,
|
|
31
|
+
enumeration: definition.enumeration ?? []
|
|
32
|
+
};
|
|
33
|
+
}
|
|
34
|
+
getTypes() {
|
|
35
|
+
return ["string"];
|
|
36
|
+
}
|
|
37
|
+
isValidType(value) {
|
|
38
|
+
return typeof value === "string";
|
|
39
|
+
}
|
|
40
|
+
validate(value, path = []) {
|
|
41
|
+
if (!this.isValidType(value)) {
|
|
42
|
+
throw new Violation(
|
|
43
|
+
`Expected value of type string at path '${formatPath(path)}', actual ${describe(value)}.`,
|
|
44
|
+
path,
|
|
45
|
+
{ type: "string" }
|
|
46
|
+
);
|
|
47
|
+
}
|
|
48
|
+
const { minLength, maxLength } = this.definition;
|
|
49
|
+
if (minLength >= 0 && minLength > value.length) {
|
|
50
|
+
throw new Violation(
|
|
51
|
+
`Expected ${minLength === maxLength ? "exactly" : "at least"} ${minLength} ${minLength === 1 ? "character" : "characters"} at path '${formatPath(path)}', actual ${value.length}.`,
|
|
52
|
+
path,
|
|
53
|
+
{ limit: minLength }
|
|
54
|
+
);
|
|
39
55
|
}
|
|
40
|
-
|
|
41
|
-
|
|
56
|
+
if (maxLength >= 0 && maxLength < value.length) {
|
|
57
|
+
throw new Violation(
|
|
58
|
+
`Expected ${minLength === maxLength ? "exactly" : "at most"} ${maxLength} ${maxLength === 1 ? "character" : "characters"} at path '${formatPath(path)}', actual ${value.length}.`,
|
|
59
|
+
path,
|
|
60
|
+
{ limit: maxLength }
|
|
61
|
+
);
|
|
42
62
|
}
|
|
43
|
-
|
|
44
|
-
|
|
63
|
+
const { enumeration } = this.definition;
|
|
64
|
+
if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {
|
|
65
|
+
throw new Violation(
|
|
66
|
+
`Unexpected value at path '${formatPath(path)}', expecting '${enumeration.length === 1 ? enumeration[0] : `${enumeration.slice(0, -1).join("', '")}' or '${enumeration.slice(-1)}`}', found '${value}'.`,
|
|
67
|
+
path,
|
|
68
|
+
{ enumeration }
|
|
69
|
+
);
|
|
45
70
|
}
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
const { minLength, maxLength } = this.definition;
|
|
51
|
-
if (minLength >= 0 && minLength > value.length) {
|
|
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 });
|
|
55
|
-
}
|
|
56
|
-
if (maxLength >= 0 && maxLength < value.length) {
|
|
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 });
|
|
60
|
-
}
|
|
61
|
-
const { enumeration } = this.definition;
|
|
62
|
-
if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {
|
|
63
|
-
throw new schema_1.Violation(`Unexpected value at path '${(0, violation_1.formatPath)(path)}', expecting `
|
|
64
|
-
+ `'${enumeration.length === 1
|
|
65
|
-
? enumeration[0]
|
|
66
|
-
: `${enumeration.slice(0, -1).join('\', \'')}' or '${enumeration.slice(-1)}`}', `
|
|
67
|
-
+ `found '${value}'.`, path, { enumeration: enumeration });
|
|
68
|
-
}
|
|
69
|
-
const { format, pattern } = this.definition;
|
|
70
|
-
if (format !== undefined && !FORMAT[format](value)) {
|
|
71
|
-
throw new schema_1.Violation(`Invalid ${format} format at path '${(0, violation_1.formatPath)(path)}'.`, path, { format: format });
|
|
72
|
-
}
|
|
73
|
-
if (pattern !== undefined && !pattern.test(value)) {
|
|
74
|
-
throw new schema_1.Violation(`Invalid format at path '${(0, violation_1.formatPath)(path)}'.`, path, { pattern: pattern });
|
|
75
|
-
}
|
|
71
|
+
const { format, pattern } = this.definition;
|
|
72
|
+
if (format !== void 0 && !FORMAT[format](value)) {
|
|
73
|
+
throw new Violation(`Invalid ${format} format at path '${formatPath(path)}'.`, path, { format });
|
|
76
74
|
}
|
|
75
|
+
if (pattern !== void 0 && !pattern.test(value)) {
|
|
76
|
+
throw new Violation(`Invalid format at path '${formatPath(path)}'.`, path, { pattern });
|
|
77
|
+
}
|
|
78
|
+
}
|
|
77
79
|
}
|
|
78
|
-
|
|
80
|
+
export {
|
|
81
|
+
StringType
|
|
82
|
+
};
|
|
79
83
|
//# sourceMappingURL=stringType.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"
|
|
1
|
+
{"version":3,"sources":["../src/validation/stringType.ts"],"sourcesContent":["import {TypeSchema, Violation} from './schema';\nimport {describe, formatPath} from './violation';\n\ninterface Format {\n (value: string): boolean;\n}\n\nconst FORMAT: {[key: string]: Format} = {\n pointer: function pointer(value: string): boolean {\n return /^(\\.|([a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])(\\.[a-zA-Z_][a-zA-Z0-9_]*|\\[[0-9]+])*)$/.test(value);\n },\n identifier: function identifier(value: string): boolean {\n return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);\n },\n uuid: function uuid(value: string): boolean {\n return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);\n },\n date: function date(value: string): boolean {\n return /^(\\d\\d\\d\\d)-(\\d\\d)-(\\d\\d)$/.test(value);\n },\n url: function url(value: string): boolean {\n try {\n // eslint-disable-next-line no-new -- Easier way to validate a URL\n new URL(value);\n } catch {\n return false;\n }\n\n return true;\n },\n};\n\ntype StringDefinition = {\n minLength: number,\n maxLength: number,\n enumeration: string[],\n pattern?: RegExp,\n format?: keyof typeof FORMAT,\n};\n\nexport class StringType implements TypeSchema {\n private readonly definition: StringDefinition;\n\n public constructor(definition: Partial<StringDefinition> = {}) {\n this.definition = {\n ...definition,\n minLength: definition.minLength ?? -1,\n maxLength: definition.maxLength ?? -1,\n enumeration: definition.enumeration ?? [],\n };\n }\n\n public getTypes(): string[] {\n return ['string'];\n }\n\n public isValidType(value: unknown): value is string {\n return typeof value === 'string';\n }\n\n public validate(value: unknown, path: string[] = []): void {\n if (!this.isValidType(value)) {\n throw new Violation(\n `Expected value of type string at path '${formatPath(path)}', actual ${describe(value)}.`,\n path,\n {type: 'string'},\n );\n }\n\n const {minLength, maxLength} = this.definition;\n\n if (minLength >= 0 && minLength > value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at least'} `\n + `${minLength} ${minLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: minLength},\n );\n }\n\n if (maxLength >= 0 && maxLength < value.length) {\n throw new Violation(\n `Expected ${minLength === maxLength ? 'exactly' : 'at most'} `\n + `${maxLength} ${maxLength === 1 ? 'character' : 'characters'} `\n + `at path '${formatPath(path)}', actual ${value.length}.`,\n path,\n {limit: maxLength},\n );\n }\n\n const {enumeration} = this.definition;\n\n if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {\n throw new Violation(\n `Unexpected value at path '${formatPath(path)}', expecting `\n + `'${\n enumeration.length === 1\n ? enumeration[0]\n : `${enumeration.slice(0, -1).join('\\', \\'')}' or '${enumeration.slice(-1)}`\n }', `\n + `found '${value}'.`,\n path,\n {enumeration: enumeration},\n );\n }\n\n const {format, pattern} = this.definition;\n\n if (format !== undefined && !FORMAT[format](value)) {\n throw new Violation(`Invalid ${format} format at path '${formatPath(path)}'.`, path, {format: format});\n }\n\n if (pattern !== undefined && !pattern.test(value)) {\n throw new Violation(`Invalid format at path '${formatPath(path)}'.`, path, {pattern: pattern});\n }\n }\n}\n"],"mappings":"AAAA,SAAoB,iBAAgB;AACpC,SAAQ,UAAU,kBAAiB;AAMnC,MAAM,SAAkC;AAAA,EACpC,SAAS,SAAS,QAAQ,OAAwB;AAC9C,WAAO,iFAAiF,KAAK,KAAK;AAAA,EACtG;AAAA,EACA,YAAY,SAAS,WAAW,OAAwB;AACpD,WAAO,2BAA2B,KAAK,KAAK;AAAA,EAChD;AAAA,EACA,MAAM,SAAS,KAAK,OAAwB;AACxC,WAAO,gDAAgD,KAAK,KAAK;AAAA,EACrE;AAAA,EACA,MAAM,SAAS,KAAK,OAAwB;AACxC,WAAO,6BAA6B,KAAK,KAAK;AAAA,EAClD;AAAA,EACA,KAAK,SAAS,IAAI,OAAwB;AACtC,QAAI;AAEA,UAAI,IAAI,KAAK;AAAA,IACjB,QAAQ;AACJ,aAAO;AAAA,IACX;AAEA,WAAO;AAAA,EACX;AACJ;AAUO,MAAM,WAAiC;AAAA,EAGnC,YAAY,aAAwC,CAAC,GAAG;AAC3D,SAAK,aAAa;AAAA,MACd,GAAG;AAAA,MACH,WAAW,WAAW,aAAa;AAAA,MACnC,WAAW,WAAW,aAAa;AAAA,MACnC,aAAa,WAAW,eAAe,CAAC;AAAA,IAC5C;AAAA,EACJ;AAAA,EAEO,WAAqB;AACxB,WAAO,CAAC,QAAQ;AAAA,EACpB;AAAA,EAEO,YAAY,OAAiC;AAChD,WAAO,OAAO,UAAU;AAAA,EAC5B;AAAA,EAEO,SA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G;AAChD,YAAM,IAAI,UAAU,WAAW,MAAM,oBAAoB,WAAW,IAAI,CAAC,MAAM,MAAM,EAAC,OAAc,CAAC;AAAA,IACzG;AAEA,QAAI,YAAY,UAAa,CAAC,QAAQ,KAAK,KAAK,GAAG;AAC/C,YAAM,IAAI,UAAU,2BAA2B,WAAW,IAAI,CAAC,MAAM,MAAM,EAAC,QAAgB,CAAC;AAAA,IACjG;AAAA,EACJ;AACJ;","names":[]}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"sources":["../../src/validation/unionType.ts"],"sourcesContent":["import {TypeSchema, Violation} from './schema';\nimport {describe, formatPath} from './violation';\n\nexport class UnionType implements TypeSchema {\n private readonly schemas: TypeSchema[];\n\n public constructor(first: TypeSchema, second: TypeSchema, ...others: TypeSchema[]) {\n this.schemas = [first, second, ...others];\n }\n\n public getTypes(): string[] {\n const types = [];\n\n for (const schema of this.schemas) {\n for (const type of schema.getTypes()) {\n if (types.indexOf(type) < 0) {\n types.push(type);\n }\n }\n }\n\n return types;\n }\n\n public isValidType(value: unknown): boolean {\n for (const schema of this.schemas) {\n if (schema.isValidType(value)) {\n return true;\n }\n }\n\n return false;\n }\n\n public validate(value: unknown, path: string[] = []): void {\n for (const schema of this.schemas) {\n if (schema.isValidType(value)) {\n schema.validate(value, path);\n\n return;\n }\n }\n\n const types = this.getTypes();\n\n throw new Violation(\n `Expected value of type ${types.slice(0, -1).join(', ')} or ${types[types.length - 1]} `\n + `at path '${formatPath(path)}', actual ${describe(value)}.`,\n path,\n {type: types.join('|')},\n );\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA,oBAAoC;AACpC,uBAAmC;AAE5B,MAAM,UAAgC;AAAA,EAGlC,YAAY,OAAmB,WAAuB,QAAsB;AAC/E,SAAK,UAAU,CAAC,OAAO,QAAQ,GAAG,MAAM;AAAA,EAC5C;AAAA,EAEO,WAAqB;AACxB,UAAM,QAAQ,CAAC;AAEf,eAAW,UAAU,KAAK,SAAS;AAC/B,iBAAW,QAAQ,OAAO,SAAS,GAAG;AAClC,YAAI,MAAM,QAAQ,IAAI,IAAI,GAAG;AACzB,gBAAM,KAAK,IAAI;AAAA,QACnB;AAAA,MACJ;AAAA,IACJ;AAEA,WAAO;AAAA,EACX;AAAA,EAEO,YAAY,OAAyB;AACxC,eAAW,UAAU,KAAK,SAAS;AAC/B,UAAI,OAAO,YAAY,KAAK,GAAG;AAC3B,eAAO;AAAA,MACX;AAAA,IACJ;AAEA,WAAO;AAAA,EACX;AAAA,EAEO,SAAS,OAAgB,OAAiB,CAAC,GAAS;AACvD,eAAW,UAAU,KAAK,SAAS;AAC/B,UAAI,OAAO,YAAY,KAAK,GAAG;AAC3B,eAAO,SAAS,OAAO,IAAI;AAE3B;AAAA,MACJ;AAAA,IACJ;AAEA,UAAM,QAAQ,KAAK,SAAS;AAE5B,UAAM,IAAI;AAAA,MACN,0BAA0B,MAAM,MAAM,GAAG,EAAE,EAAE,KAAK,IAAI,CAAC,OAAO,MAAM,MAAM,SAAS,CAAC,CAAC,iBACvE,6BAAW,IAAI,CAAC,iBAAa,2BAAS,KAAK,CAAC;AAAA,MAC1D;AAAA,MACA,EAAC,MAAM,MAAM,KAAK,GAAG,EAAC;AAAA,IAC1B;AAAA,EACJ;AACJ;","names":[]}
|
|
@@ -1,8 +1,11 @@
|
|
|
1
|
-
import { TypeSchema } from './schema';
|
|
2
|
-
|
|
1
|
+
import { TypeSchema } from './schema.js';
|
|
2
|
+
|
|
3
|
+
declare class UnionType implements TypeSchema {
|
|
3
4
|
private readonly schemas;
|
|
4
5
|
constructor(first: TypeSchema, second: TypeSchema, ...others: TypeSchema[]);
|
|
5
6
|
getTypes(): string[];
|
|
6
7
|
isValidType(value: unknown): boolean;
|
|
7
8
|
validate(value: unknown, path?: string[]): void;
|
|
8
9
|
}
|
|
10
|
+
|
|
11
|
+
export { UnionType };
|
package/validation/unionType.js
CHANGED
|
@@ -1,42 +1,44 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
exports.UnionType = void 0;
|
|
4
|
-
const schema_1 = require("./schema");
|
|
5
|
-
const violation_1 = require("./violation");
|
|
1
|
+
import { Violation } from "./schema";
|
|
2
|
+
import { describe, formatPath } from "./violation";
|
|
6
3
|
class UnionType {
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
}
|
|
17
|
-
}
|
|
4
|
+
constructor(first, second, ...others) {
|
|
5
|
+
this.schemas = [first, second, ...others];
|
|
6
|
+
}
|
|
7
|
+
getTypes() {
|
|
8
|
+
const types = [];
|
|
9
|
+
for (const schema of this.schemas) {
|
|
10
|
+
for (const type of schema.getTypes()) {
|
|
11
|
+
if (types.indexOf(type) < 0) {
|
|
12
|
+
types.push(type);
|
|
18
13
|
}
|
|
19
|
-
|
|
14
|
+
}
|
|
20
15
|
}
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
16
|
+
return types;
|
|
17
|
+
}
|
|
18
|
+
isValidType(value) {
|
|
19
|
+
for (const schema of this.schemas) {
|
|
20
|
+
if (schema.isValidType(value)) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
28
23
|
}
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
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('|') });
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
validate(value, path = []) {
|
|
27
|
+
for (const schema of this.schemas) {
|
|
28
|
+
if (schema.isValidType(value)) {
|
|
29
|
+
schema.validate(value, path);
|
|
30
|
+
return;
|
|
31
|
+
}
|
|
39
32
|
}
|
|
33
|
+
const types = this.getTypes();
|
|
34
|
+
throw new Violation(
|
|
35
|
+
`Expected value of type ${types.slice(0, -1).join(", ")} or ${types[types.length - 1]} at path '${formatPath(path)}', actual ${describe(value)}.`,
|
|
36
|
+
path,
|
|
37
|
+
{ type: types.join("|") }
|
|
38
|
+
);
|
|
39
|
+
}
|
|
40
40
|
}
|
|
41
|
-
|
|
41
|
+
export {
|
|
42
|
+
UnionType
|
|
43
|
+
};
|
|
42
44
|
//# sourceMappingURL=unionType.js.map
|