@croct/sdk 0.17.9 → 0.17.11

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.
Files changed (209) hide show
  1. package/activeRecord.cjs +135 -0
  2. package/apiKey.cjs +178 -0
  3. package/base64Url.cjs +41 -0
  4. package/cache/cache.cjs +34 -0
  5. package/cache/cookieCache.cjs +85 -0
  6. package/cache/fallbackCache.cjs +47 -0
  7. package/cache/inMemoryCache.cjs +41 -0
  8. package/cache/index.cjs +37 -0
  9. package/cache/localStorageCache.cjs +81 -0
  10. package/channel/channel.cjs +48 -0
  11. package/channel/encodedChannel.cjs +39 -0
  12. package/channel/guaranteedChannel.cjs +105 -0
  13. package/channel/httpBeaconChannel.cjs +111 -0
  14. package/channel/index.cjs +46 -0
  15. package/channel/queuedChannel.cjs +122 -0
  16. package/channel/retryChannel.cjs +87 -0
  17. package/channel/sandboxChannel.cjs +63 -0
  18. package/cid/assigner.cjs +16 -0
  19. package/cid/cachedAssigner.cjs +66 -0
  20. package/cid/fixedAssigner.cjs +35 -0
  21. package/cid/index.cjs +37 -0
  22. package/cid/remoteAssigner.cjs +65 -0
  23. package/constants.cjs +37 -0
  24. package/constants.cjs.map +1 -1
  25. package/constants.d.ts +2 -2
  26. package/constants.js +1 -1
  27. package/constants.js.map +1 -1
  28. package/container.cjs +305 -0
  29. package/contentFetcher.cjs +193 -0
  30. package/context.cjs +114 -0
  31. package/error.cjs +52 -0
  32. package/evaluator.cjs +219 -0
  33. package/eventManager.cjs +53 -0
  34. package/eventSubjectProcessor.cjs +84 -0
  35. package/facade/contentFetcherFacade.cjs +61 -0
  36. package/facade/evaluatorFacade.cjs +94 -0
  37. package/facade/index.cjs +52 -0
  38. package/facade/sdkFacade.cjs +229 -0
  39. package/facade/sessionFacade.cjs +36 -0
  40. package/facade/sessionPatch.cjs +48 -0
  41. package/facade/trackerFacade.cjs +87 -0
  42. package/facade/userFacade.cjs +43 -0
  43. package/facade/userPatch.cjs +48 -0
  44. package/help.cjs +45 -0
  45. package/index.cjs +33 -0
  46. package/logging/consoleLogger.cjs +50 -0
  47. package/logging/filteredLogger.cjs +62 -0
  48. package/logging/index.cjs +40 -0
  49. package/logging/logger.cjs +16 -0
  50. package/logging/namespacedLogger.cjs +48 -0
  51. package/logging/nullLogger.cjs +37 -0
  52. package/namespacedStorage.cjs +77 -0
  53. package/package.json +7 -6
  54. package/patch.cjs +16 -0
  55. package/queue/capacityRestrictedQueue.cjs +57 -0
  56. package/queue/inMemoryQueue.cjs +58 -0
  57. package/queue/index.cjs +40 -0
  58. package/queue/monitoredQueue.cjs +141 -0
  59. package/queue/persistentQueue.cjs +78 -0
  60. package/queue/queue.cjs +16 -0
  61. package/retry/arbitraryPolicy.cjs +41 -0
  62. package/retry/backoffPolicy.cjs +81 -0
  63. package/retry/index.cjs +40 -0
  64. package/retry/maxAttemptsPolicy.cjs +45 -0
  65. package/retry/neverPolicy.cjs +35 -0
  66. package/retry/policy.cjs +16 -0
  67. package/schema/attributeSchema.cjs +32 -0
  68. package/schema/contentFetcherSchemas.cjs +49 -0
  69. package/schema/contentSchemas.cjs +70 -0
  70. package/schema/contextSchemas.cjs +31 -0
  71. package/schema/ecommerceSchemas.cjs +209 -0
  72. package/schema/evaluatorSchemas.cjs +64 -0
  73. package/schema/eventSchemas.cjs +162 -0
  74. package/schema/index.cjs +42 -0
  75. package/schema/loggerSchema.cjs +38 -0
  76. package/schema/operationSchemas.cjs +122 -0
  77. package/schema/sdkFacadeSchemas.cjs +82 -0
  78. package/schema/sdkSchemas.cjs +110 -0
  79. package/schema/tokenSchema.cjs +68 -0
  80. package/schema/userSchema.cjs +202 -0
  81. package/sdk.cjs +134 -0
  82. package/sdkEvents.cjs +16 -0
  83. package/sourceLocation.cjs +92 -0
  84. package/tab.cjs +122 -0
  85. package/token/cachedTokenStore.cjs +51 -0
  86. package/token/inMemoryTokenStore.cjs +38 -0
  87. package/token/index.cjs +37 -0
  88. package/token/replicatedTokenStore.cjs +40 -0
  89. package/token/token.cjs +239 -0
  90. package/tracker.cjs +358 -0
  91. package/trackingEvents.cjs +94 -0
  92. package/transformer.cjs +30 -0
  93. package/utilityTypes.cjs +16 -0
  94. package/uuid.cjs +55 -0
  95. package/validation/arrayType.cjs +75 -0
  96. package/validation/booleanType.cjs +46 -0
  97. package/validation/functionType.cjs +46 -0
  98. package/validation/index.cjs +66 -0
  99. package/validation/jsonType.cjs +142 -0
  100. package/validation/mixedSchema.cjs +31 -0
  101. package/validation/nullType.cjs +46 -0
  102. package/validation/numberType.cjs +69 -0
  103. package/validation/objectType.cjs +114 -0
  104. package/validation/schema.cjs +34 -0
  105. package/validation/stringType.cjs +106 -0
  106. package/validation/unionType.cjs +67 -0
  107. package/validation/violation.cjs +47 -0
  108. package/src/activeRecord.ts +0 -150
  109. package/src/apiKey.ts +0 -208
  110. package/src/base64Url.ts +0 -22
  111. package/src/cache/cache.ts +0 -22
  112. package/src/cache/cookieCache.ts +0 -88
  113. package/src/cache/fallbackCache.ts +0 -29
  114. package/src/cache/inMemoryCache.ts +0 -21
  115. package/src/cache/index.ts +0 -4
  116. package/src/cache/localStorageCache.ts +0 -85
  117. package/src/channel/channel.ts +0 -52
  118. package/src/channel/encodedChannel.ts +0 -21
  119. package/src/channel/guaranteedChannel.ts +0 -131
  120. package/src/channel/httpBeaconChannel.ts +0 -128
  121. package/src/channel/index.ts +0 -7
  122. package/src/channel/queuedChannel.ts +0 -143
  123. package/src/channel/retryChannel.ts +0 -94
  124. package/src/channel/sandboxChannel.ts +0 -47
  125. package/src/cid/assigner.ts +0 -3
  126. package/src/cid/cachedAssigner.ts +0 -68
  127. package/src/cid/fixedAssigner.ts +0 -13
  128. package/src/cid/index.ts +0 -4
  129. package/src/cid/remoteAssigner.ts +0 -57
  130. package/src/constants.ts +0 -4
  131. package/src/container.ts +0 -410
  132. package/src/contentFetcher.ts +0 -290
  133. package/src/context.ts +0 -139
  134. package/src/error.ts +0 -31
  135. package/src/evaluator.ts +0 -314
  136. package/src/eventManager.ts +0 -53
  137. package/src/eventSubjectProcessor.ts +0 -85
  138. package/src/facade/contentFetcherFacade.ts +0 -69
  139. package/src/facade/evaluatorFacade.ts +0 -111
  140. package/src/facade/index.ts +0 -7
  141. package/src/facade/sdkFacade.ts +0 -310
  142. package/src/facade/sessionFacade.ts +0 -14
  143. package/src/facade/sessionPatch.ts +0 -32
  144. package/src/facade/trackerFacade.ts +0 -98
  145. package/src/facade/userFacade.ts +0 -26
  146. package/src/facade/userPatch.ts +0 -32
  147. package/src/help.ts +0 -24
  148. package/src/index.ts +0 -4
  149. package/src/logging/consoleLogger.ts +0 -38
  150. package/src/logging/filteredLogger.ts +0 -57
  151. package/src/logging/index.ts +0 -5
  152. package/src/logging/logger.ts +0 -13
  153. package/src/logging/namespacedLogger.ts +0 -32
  154. package/src/logging/nullLogger.ts +0 -19
  155. package/src/namespacedStorage.ts +0 -69
  156. package/src/patch.ts +0 -64
  157. package/src/queue/capacityRestrictedQueue.ts +0 -44
  158. package/src/queue/inMemoryQueue.ts +0 -43
  159. package/src/queue/index.ts +0 -5
  160. package/src/queue/monitoredQueue.ts +0 -168
  161. package/src/queue/persistentQueue.ts +0 -73
  162. package/src/queue/queue.ts +0 -15
  163. package/src/retry/arbitraryPolicy.ts +0 -21
  164. package/src/retry/backoffPolicy.ts +0 -84
  165. package/src/retry/index.ts +0 -5
  166. package/src/retry/maxAttemptsPolicy.ts +0 -28
  167. package/src/retry/neverPolicy.ts +0 -11
  168. package/src/retry/policy.ts +0 -5
  169. package/src/schema/attributeSchema.ts +0 -6
  170. package/src/schema/contentFetcherSchemas.ts +0 -23
  171. package/src/schema/contentSchemas.ts +0 -44
  172. package/src/schema/contextSchemas.ts +0 -5
  173. package/src/schema/ecommerceSchemas.ts +0 -179
  174. package/src/schema/evaluatorSchemas.ts +0 -52
  175. package/src/schema/eventSchemas.ts +0 -134
  176. package/src/schema/index.ts +0 -11
  177. package/src/schema/loggerSchema.ts +0 -12
  178. package/src/schema/operationSchemas.ts +0 -102
  179. package/src/schema/sdkFacadeSchemas.ts +0 -64
  180. package/src/schema/sdkSchemas.ts +0 -82
  181. package/src/schema/tokenSchema.ts +0 -42
  182. package/src/schema/userSchema.ts +0 -184
  183. package/src/sdk.ts +0 -183
  184. package/src/sdkEvents.ts +0 -15
  185. package/src/sourceLocation.ts +0 -85
  186. package/src/tab.ts +0 -148
  187. package/src/token/cachedTokenStore.ts +0 -34
  188. package/src/token/inMemoryTokenStore.ts +0 -13
  189. package/src/token/index.ts +0 -4
  190. package/src/token/replicatedTokenStore.ts +0 -21
  191. package/src/token/token.ts +0 -301
  192. package/src/tracker.ts +0 -504
  193. package/src/trackingEvents.ts +0 -452
  194. package/src/transformer.ts +0 -7
  195. package/src/utilityTypes.ts +0 -3
  196. package/src/uuid.ts +0 -43
  197. package/src/validation/arrayType.ts +0 -71
  198. package/src/validation/booleanType.ts +0 -22
  199. package/src/validation/functionType.ts +0 -22
  200. package/src/validation/index.ts +0 -12
  201. package/src/validation/jsonType.ts +0 -156
  202. package/src/validation/mixedSchema.ts +0 -7
  203. package/src/validation/nullType.ts +0 -22
  204. package/src/validation/numberType.ts +0 -59
  205. package/src/validation/objectType.ts +0 -138
  206. package/src/validation/schema.ts +0 -21
  207. package/src/validation/stringType.ts +0 -118
  208. package/src/validation/unionType.ts +0 -53
  209. package/src/validation/violation.ts +0 -23
@@ -1,156 +0,0 @@
1
- import {JsonArray, JsonObject, JsonPrimitive, JsonValue} from '@croct/json';
2
- import {Schema, TypeSchema, Violation} from './schema';
3
- import {describe, formatPath} from './violation';
4
-
5
- function isJsonPrimitive(value: unknown): value is JsonPrimitive {
6
- return value === null || typeof value === 'string' || typeof value === 'boolean' || typeof value === 'number';
7
- }
8
-
9
- function isPlainObject(value: unknown): value is object {
10
- return Object.prototype
11
- .toString
12
- .call(value) === '[object Object]';
13
- }
14
-
15
- function isJsonArray(value: unknown): value is JsonArray {
16
- return Array.isArray(value) && value.every(isJsonValue);
17
- }
18
-
19
- function isJsonObject(value: unknown): value is JsonObject {
20
- return isPlainObject(value) && Object.values(value).every(isJsonValue);
21
- }
22
-
23
- function isJsonValue(value: unknown): value is JsonValue {
24
- return isJsonPrimitive(value) || isJsonArray(value) || isJsonObject(value);
25
- }
26
-
27
- type JsonObjectDefinition = {
28
- properties?: Schema,
29
- propertyNames?: Schema,
30
- };
31
-
32
- export class JsonObjectType implements TypeSchema {
33
- private readonly definition: JsonObjectDefinition;
34
-
35
- public constructor(definition: JsonObjectDefinition = {}) {
36
- this.definition = definition;
37
- }
38
-
39
- public getTypes(): string[] {
40
- return ['object'];
41
- }
42
-
43
- public isValidType(value: unknown): boolean {
44
- return isPlainObject(value);
45
- }
46
-
47
- public validate(value: unknown, path: string[] = []): void {
48
- if (!isJsonObject(value)) {
49
- throw new Violation(
50
- `Expected a JSON object at path '${formatPath(path)}', actual ${describe(value)}.`,
51
- path,
52
- {type: 'object'},
53
- );
54
- }
55
-
56
- if (this.definition.properties === undefined && this.definition.propertyNames === undefined) {
57
- return;
58
- }
59
-
60
- for (const [entryName, entryValue] of Object.entries(value)) {
61
- const propertyPath = path.concat([entryName]);
62
-
63
- if (this.definition.propertyNames !== undefined) {
64
- this.definition
65
- .propertyNames
66
- .validate(entryName, propertyPath);
67
- }
68
-
69
- if (this.definition.properties !== undefined) {
70
- this.definition
71
- .properties
72
- .validate(entryValue, path.concat([entryName]));
73
- }
74
- }
75
- }
76
- }
77
-
78
- type JsonArrayDefinition = {
79
- items?: Schema,
80
- };
81
-
82
- export class JsonArrayType implements TypeSchema {
83
- private readonly definition: JsonArrayDefinition;
84
-
85
- public constructor(definition: JsonArrayDefinition = {}) {
86
- this.definition = definition;
87
- }
88
-
89
- public getTypes(): string[] {
90
- return ['array'];
91
- }
92
-
93
- public isValidType(value: unknown): boolean {
94
- return Array.isArray(value);
95
- }
96
-
97
- public validate(value: unknown, path: string[] = []): void {
98
- if (!isJsonArray(value)) {
99
- throw new Violation(
100
- `Expected a JSON array at path '${formatPath(path)}', actual ${describe(value)}.`,
101
- path,
102
- {type: 'array'},
103
- );
104
- }
105
-
106
- if (this.definition.items === undefined) {
107
- return;
108
- }
109
-
110
- for (let index = 0; index < value.length; index++) {
111
- this.definition
112
- .items
113
- .validate(value[index], path.concat([index.toString()]));
114
- }
115
- }
116
- }
117
-
118
- export class JsonPrimitiveType implements TypeSchema {
119
- public getTypes(): string[] {
120
- return ['null', 'number', 'string', 'boolean'];
121
- }
122
-
123
- public isValidType(value: unknown): boolean {
124
- return isJsonPrimitive(value);
125
- }
126
-
127
- public validate(value: unknown, path: string[] = []): void {
128
- if (!this.isValidType(value)) {
129
- throw new Violation(
130
- `Expected a JSON primitive at path '${formatPath(path)}', actual ${describe(value)}.`,
131
- path,
132
- {type: this.getTypes().join('|')},
133
- );
134
- }
135
- }
136
- }
137
-
138
- export class JsonType implements TypeSchema {
139
- public getTypes(): string[] {
140
- return ['null', 'number', 'string', 'boolean', 'array', 'object'];
141
- }
142
-
143
- public isValidType(value: unknown): boolean {
144
- return isJsonPrimitive(value) || Array.isArray(value) || isPlainObject(value);
145
- }
146
-
147
- public validate(value: unknown, path: string[] = []): void {
148
- if (!isJsonValue(value)) {
149
- throw new Violation(
150
- `Expected a JSON value at path '${formatPath(path)}', actual ${describe(value)}.`,
151
- path,
152
- {type: this.getTypes().join('|')},
153
- );
154
- }
155
- }
156
- }
@@ -1,7 +0,0 @@
1
- import {Schema} from './schema';
2
-
3
- export class MixedSchema implements Schema {
4
- public validate(): void {
5
- // always valid
6
- }
7
- }
@@ -1,22 +0,0 @@
1
- import {TypeSchema, Violation} from './schema';
2
- import {describe, formatPath} from './violation';
3
-
4
- export class NullType implements TypeSchema {
5
- public getTypes(): string[] {
6
- return ['null'];
7
- }
8
-
9
- public isValidType(value: unknown): value is null {
10
- return value === null;
11
- }
12
-
13
- public validate(value: unknown, path: string[] = []): void {
14
- if (!this.isValidType(value)) {
15
- throw new Violation(
16
- `Expected value of type null at path '${formatPath(path)}', actual ${describe(value)}.`,
17
- path,
18
- {type: 'null'},
19
- );
20
- }
21
- }
22
- }
@@ -1,59 +0,0 @@
1
- import {TypeSchema, Violation} from './schema';
2
- import {describe, formatPath} from './violation';
3
-
4
- type NumberDefinition = {
5
- integer: boolean,
6
- minimum: number,
7
- maximum: number,
8
- };
9
-
10
- export class NumberType implements TypeSchema {
11
- private readonly definition: NumberDefinition;
12
-
13
- public constructor(definition: Partial<NumberDefinition> = {}) {
14
- this.definition = {
15
- ...definition,
16
- integer: definition.integer ?? false,
17
- minimum: definition.minimum ?? Number.NEGATIVE_INFINITY,
18
- maximum: definition.maximum ?? Number.POSITIVE_INFINITY,
19
- };
20
- }
21
-
22
- public getTypes(): string[] {
23
- return [this.definition.integer ? 'integer' : 'number'];
24
- }
25
-
26
- public isValidType(value: unknown): value is number {
27
- return typeof value === 'number' && (!this.definition.integer || Number.isInteger(value));
28
- }
29
-
30
- public validate(value: unknown, path: string[] = []): void {
31
- if (!this.isValidType(value)) {
32
- const type = this.getTypes()[0];
33
-
34
- throw new Violation(
35
- `Expected value of type ${type} at path '${formatPath(path)}', actual ${describe(value)}.`,
36
- path,
37
- {type: type},
38
- );
39
- }
40
-
41
- if (value < this.definition.minimum) {
42
- throw new Violation(
43
- `Expected a value greater than or equal to ${this.definition.minimum} `
44
- + `at path '${formatPath(path)}', actual ${value}.`,
45
- path,
46
- {limit: this.definition.minimum},
47
- );
48
- }
49
-
50
- if (value > this.definition.maximum) {
51
- throw new Violation(
52
- `Expected a value less than or equal to ${this.definition.maximum} `
53
- + `at path '${formatPath(path)}', actual ${value}.`,
54
- path,
55
- {limit: this.definition.maximum},
56
- );
57
- }
58
- }
59
- }
@@ -1,138 +0,0 @@
1
- import {Schema, TypeSchema, Violation} from './schema';
2
- import {MixedSchema} from './mixedSchema';
3
- import {describe, formatPath} from './violation';
4
-
5
- type ObjectDefinition = {
6
- type?: {new (...args: any): any},
7
- properties: {[key: string]: Schema},
8
- additionalProperties: boolean | Schema,
9
- subtypes?: {
10
- discriminator: string,
11
- schemas: {[key: string]: ObjectType},
12
- },
13
- propertyNames: Schema,
14
- required: string[],
15
- minProperties: number,
16
- maxProperties: number,
17
- };
18
-
19
- export class ObjectType implements TypeSchema {
20
- private readonly definition: ObjectDefinition;
21
-
22
- public constructor(schema: Partial<ObjectDefinition> = {}) {
23
- this.definition = {
24
- ...schema,
25
- properties: schema.properties ?? {},
26
- required: schema.required ?? [],
27
- additionalProperties: schema.additionalProperties ?? false,
28
- propertyNames: schema.propertyNames ?? new MixedSchema(),
29
- minProperties: schema.minProperties ?? -1,
30
- maxProperties: schema.maxProperties ?? -1,
31
- };
32
- }
33
-
34
- public getTypes(): string[] {
35
- if (this.definition.type !== undefined) {
36
- return [this.definition.type.name];
37
- }
38
-
39
- return ['object'];
40
- }
41
-
42
- public isValidType(value: unknown): value is object {
43
- if (this.definition.type !== undefined) {
44
- return value instanceof this.definition.type;
45
- }
46
-
47
- return Object.prototype
48
- .toString
49
- .call(value) === '[object Object]';
50
- }
51
-
52
- public validate(value: unknown, path: string[] = []): void {
53
- if (!this.isValidType(value)) {
54
- const [type] = this.getTypes();
55
-
56
- throw new Violation(
57
- `Expected value of type ${type} at path '${formatPath(path)}', `
58
- + `actual ${describe(value)}.`,
59
- path,
60
- {type: type},
61
- );
62
- }
63
-
64
- const entries = Object.entries(value);
65
-
66
- const {minProperties, maxProperties} = this.definition;
67
-
68
- if (minProperties >= 0 && minProperties > entries.length) {
69
- throw new Violation(
70
- `Expected ${minProperties === maxProperties ? 'exactly' : 'at least'} `
71
- + `${minProperties} ${minProperties === 1 ? 'entry' : 'entries'} `
72
- + `at path '${formatPath(path)}', actual ${entries.length}.`,
73
- path,
74
- {limit: minProperties},
75
- );
76
- }
77
-
78
- if (maxProperties >= 0 && maxProperties < entries.length) {
79
- throw new Violation(
80
- `Expected ${minProperties === maxProperties ? 'exactly' : 'at most'} `
81
- + `${maxProperties} ${maxProperties === 1 ? 'entry' : 'entries'} `
82
- + `at path '${formatPath(path)}', actual ${entries.length}.`,
83
- path,
84
- {limit: maxProperties},
85
- );
86
- }
87
-
88
- const additionalProperties: {[key: string]: any} = {...value};
89
-
90
- for (const property of this.definition.required) {
91
- if (!(property in value)) {
92
- throw new Violation(`Missing property '${formatPath(path.concat([property]))}'.`, path, {
93
- required: property,
94
- });
95
- }
96
- }
97
-
98
- for (const [entryName, entryValue] of entries) {
99
- const propertyPath = path.concat([entryName]);
100
-
101
- this.definition
102
- .propertyNames
103
- .validate(entryName, propertyPath);
104
-
105
- const propertyRule = this.definition.properties[entryName];
106
-
107
- if (propertyRule !== undefined) {
108
- propertyRule.validate(entryValue, propertyPath);
109
-
110
- delete additionalProperties[entryName];
111
-
112
- continue;
113
- }
114
-
115
- if (this.definition.additionalProperties === false) {
116
- throw new Violation(`Unknown property '${formatPath(propertyPath)}'.`, propertyPath, {
117
- additionalProperty: entryName,
118
- });
119
- }
120
-
121
- if (this.definition.additionalProperties !== true) {
122
- this.definition
123
- .additionalProperties
124
- .validate(entryValue, propertyPath);
125
- }
126
- }
127
-
128
- const {subtypes} = this.definition;
129
-
130
- if (subtypes !== undefined) {
131
- const type = (value as any)[subtypes.discriminator];
132
-
133
- if (type !== undefined && subtypes.schemas[type] !== undefined) {
134
- subtypes.schemas[type].validate(additionalProperties, path);
135
- }
136
- }
137
- }
138
- }
@@ -1,21 +0,0 @@
1
- export interface Schema {
2
- validate(value: unknown, path?: string[]): void;
3
- }
4
-
5
- export interface TypeSchema extends Schema {
6
- getTypes(): string[];
7
-
8
- isValidType(value: unknown): boolean;
9
- }
10
-
11
- export class Violation extends Error {
12
- public readonly path: string[];
13
-
14
- public readonly params: {[key: string]: unknown};
15
-
16
- public constructor(message: string, path: string[], params: {[p: string]: unknown}) {
17
- super(message);
18
- this.path = path;
19
- this.params = params;
20
- }
21
- }
@@ -1,118 +0,0 @@
1
- import {TypeSchema, Violation} from './schema';
2
- import {describe, formatPath} from './violation';
3
-
4
- interface Format {
5
- (value: string): boolean;
6
- }
7
-
8
- const FORMAT: {[key: string]: Format} = {
9
- pointer: function pointer(value: string): boolean {
10
- return /^(\.|([a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])(\.[a-zA-Z_][a-zA-Z0-9_]*|\[[0-9]+])*)$/.test(value);
11
- },
12
- identifier: function identifier(value: string): boolean {
13
- return /^[a-zA-Z_][a-zA-Z0-9_]*$/.test(value);
14
- },
15
- uuid: function uuid(value: string): boolean {
16
- return /^[0-9a-f]{8}-(?:[0-9a-f]{4}-){3}[0-9a-f]{12}$/.test(value);
17
- },
18
- date: function date(value: string): boolean {
19
- return /^(\d\d\d\d)-(\d\d)-(\d\d)$/.test(value);
20
- },
21
- url: function url(value: string): boolean {
22
- try {
23
- // eslint-disable-next-line no-new -- Easier way to validate a URL
24
- new URL(value);
25
- } catch {
26
- return false;
27
- }
28
-
29
- return true;
30
- },
31
- };
32
-
33
- type StringDefinition = {
34
- minLength: number,
35
- maxLength: number,
36
- enumeration: string[],
37
- pattern?: RegExp,
38
- format?: keyof typeof FORMAT,
39
- };
40
-
41
- export class StringType implements TypeSchema {
42
- private readonly definition: StringDefinition;
43
-
44
- public constructor(definition: Partial<StringDefinition> = {}) {
45
- this.definition = {
46
- ...definition,
47
- minLength: definition.minLength ?? -1,
48
- maxLength: definition.maxLength ?? -1,
49
- enumeration: definition.enumeration ?? [],
50
- };
51
- }
52
-
53
- public getTypes(): string[] {
54
- return ['string'];
55
- }
56
-
57
- public isValidType(value: unknown): value is string {
58
- return typeof value === 'string';
59
- }
60
-
61
- public validate(value: unknown, path: string[] = []): void {
62
- if (!this.isValidType(value)) {
63
- throw new Violation(
64
- `Expected value of type string at path '${formatPath(path)}', actual ${describe(value)}.`,
65
- path,
66
- {type: 'string'},
67
- );
68
- }
69
-
70
- const {minLength, maxLength} = this.definition;
71
-
72
- if (minLength >= 0 && minLength > value.length) {
73
- throw new Violation(
74
- `Expected ${minLength === maxLength ? 'exactly' : 'at least'} `
75
- + `${minLength} ${minLength === 1 ? 'character' : 'characters'} `
76
- + `at path '${formatPath(path)}', actual ${value.length}.`,
77
- path,
78
- {limit: minLength},
79
- );
80
- }
81
-
82
- if (maxLength >= 0 && maxLength < value.length) {
83
- throw new Violation(
84
- `Expected ${minLength === maxLength ? 'exactly' : 'at most'} `
85
- + `${maxLength} ${maxLength === 1 ? 'character' : 'characters'} `
86
- + `at path '${formatPath(path)}', actual ${value.length}.`,
87
- path,
88
- {limit: maxLength},
89
- );
90
- }
91
-
92
- const {enumeration} = this.definition;
93
-
94
- if (enumeration.length > 0 && enumeration.indexOf(value) < 0) {
95
- throw new Violation(
96
- `Unexpected value at path '${formatPath(path)}', expecting `
97
- + `'${
98
- enumeration.length === 1
99
- ? enumeration[0]
100
- : `${enumeration.slice(0, -1).join('\', \'')}' or '${enumeration.slice(-1)}`
101
- }', `
102
- + `found '${value}'.`,
103
- path,
104
- {enumeration: enumeration},
105
- );
106
- }
107
-
108
- const {format, pattern} = this.definition;
109
-
110
- if (format !== undefined && !FORMAT[format](value)) {
111
- throw new Violation(`Invalid ${format} format at path '${formatPath(path)}'.`, path, {format: format});
112
- }
113
-
114
- if (pattern !== undefined && !pattern.test(value)) {
115
- throw new Violation(`Invalid format at path '${formatPath(path)}'.`, path, {pattern: pattern});
116
- }
117
- }
118
- }
@@ -1,53 +0,0 @@
1
- import {TypeSchema, Violation} from './schema';
2
- import {describe, formatPath} from './violation';
3
-
4
- export class UnionType implements TypeSchema {
5
- private readonly schemas: TypeSchema[];
6
-
7
- public constructor(first: TypeSchema, second: TypeSchema, ...others: TypeSchema[]) {
8
- this.schemas = [first, second, ...others];
9
- }
10
-
11
- public getTypes(): string[] {
12
- const types = [];
13
-
14
- for (const schema of this.schemas) {
15
- for (const type of schema.getTypes()) {
16
- if (types.indexOf(type) < 0) {
17
- types.push(type);
18
- }
19
- }
20
- }
21
-
22
- return types;
23
- }
24
-
25
- public isValidType(value: unknown): boolean {
26
- for (const schema of this.schemas) {
27
- if (schema.isValidType(value)) {
28
- return true;
29
- }
30
- }
31
-
32
- return false;
33
- }
34
-
35
- public validate(value: unknown, path: string[] = []): void {
36
- for (const schema of this.schemas) {
37
- if (schema.isValidType(value)) {
38
- schema.validate(value, path);
39
-
40
- return;
41
- }
42
- }
43
-
44
- const types = this.getTypes();
45
-
46
- throw new Violation(
47
- `Expected value of type ${types.slice(0, -1).join(', ')} or ${types[types.length - 1]} `
48
- + `at path '${formatPath(path)}', actual ${describe(value)}.`,
49
- path,
50
- {type: types.join('|')},
51
- );
52
- }
53
- }
@@ -1,23 +0,0 @@
1
- export function describe(value: unknown): string {
2
- if (value === null) {
3
- return 'null';
4
- }
5
-
6
- if (Array.isArray(value)) {
7
- return 'array';
8
- }
9
-
10
- if (typeof value === 'number') {
11
- return Number.isInteger(value) ? 'integer' : 'number';
12
- }
13
-
14
- if (typeof value === 'object') {
15
- return (value as object).constructor.name;
16
- }
17
-
18
- return typeof value;
19
- }
20
-
21
- export function formatPath(path: string[]): string {
22
- return `/${path.join('/')}`;
23
- }