wrangler 0.0.0-e88a54e → 0.0.0-e8a2a1d9

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 (173) hide show
  1. package/Cloudflare_CA.pem +18 -0
  2. package/README.md +47 -16
  3. package/bin/wrangler.js +123 -32
  4. package/kv-asset-handler.js +1 -0
  5. package/package.json +155 -86
  6. package/templates/__tests__/pages-dev-util.test.ts +128 -0
  7. package/templates/__tests__/tsconfig-sanity.ts +12 -0
  8. package/templates/__tests__/tsconfig.json +8 -0
  9. package/templates/checked-fetch.js +30 -0
  10. package/templates/facade.d.ts +17 -0
  11. package/templates/gitignore +172 -0
  12. package/templates/init-tests/test-jest-new-worker.js +23 -0
  13. package/templates/init-tests/test-vitest-new-worker.js +24 -0
  14. package/templates/init-tests/test-vitest-new-worker.ts +25 -0
  15. package/templates/middleware/common.ts +67 -0
  16. package/templates/middleware/loader-modules.ts +137 -0
  17. package/templates/middleware/loader-sw.ts +228 -0
  18. package/templates/middleware/middleware-d1-beta.d.ts +3 -0
  19. package/templates/middleware/middleware-miniflare3-json-error.ts +33 -0
  20. package/templates/middleware/middleware-multiworker-dev.d.ts +4 -0
  21. package/templates/middleware/middleware-multiworker-dev.ts +68 -0
  22. package/templates/middleware/middleware-pretty-error.ts +40 -0
  23. package/templates/middleware/middleware-scheduled.ts +15 -0
  24. package/templates/middleware/middleware-serve-static-assets.d.ts +6 -0
  25. package/templates/middleware/middleware-serve-static-assets.ts +56 -0
  26. package/templates/modules-watch-stub.js +4 -0
  27. package/templates/new-worker-scheduled.js +17 -0
  28. package/templates/new-worker-scheduled.ts +32 -0
  29. package/templates/new-worker.js +3 -3
  30. package/templates/new-worker.ts +22 -4
  31. package/templates/no-op-worker.js +10 -0
  32. package/templates/pages-dev-pipeline.ts +31 -0
  33. package/templates/pages-dev-util.ts +55 -0
  34. package/templates/pages-shim.ts +9 -0
  35. package/templates/pages-template-plugin.ts +190 -0
  36. package/templates/pages-template-worker.ts +198 -0
  37. package/templates/startDevWorker/InspectorProxyWorker.ts +652 -0
  38. package/templates/startDevWorker/ProxyWorker.ts +252 -0
  39. package/templates/tsconfig-sanity.ts +11 -0
  40. package/templates/tsconfig.init.json +105 -0
  41. package/templates/tsconfig.json +6 -103
  42. package/wrangler-dist/InspectorProxyWorker.js +460 -0
  43. package/wrangler-dist/InspectorProxyWorker.js.map +6 -0
  44. package/wrangler-dist/ProxyWorker.js +186 -0
  45. package/wrangler-dist/ProxyWorker.js.map +6 -0
  46. package/wrangler-dist/cli.d.ts +23507 -0
  47. package/wrangler-dist/cli.js +152534 -132977
  48. package/wrangler-dist/wasm-sync.wasm +0 -0
  49. package/import_meta_url.js +0 -3
  50. package/miniflare-config-stubs/.env.empty +0 -0
  51. package/miniflare-config-stubs/package.empty.json +0 -1
  52. package/miniflare-config-stubs/wrangler.empty.toml +0 -0
  53. package/miniflare-dist/index.mjs +0 -79
  54. package/miniflare-dist/index.mjs.map +0 -7
  55. package/pages/functions/buildWorker.ts +0 -62
  56. package/pages/functions/filepath-routing.test.ts +0 -223
  57. package/pages/functions/filepath-routing.ts +0 -181
  58. package/pages/functions/identifiers.ts +0 -78
  59. package/pages/functions/routes.ts +0 -151
  60. package/pages/functions/template-worker.ts +0 -130
  61. package/src/__tests__/configuration.test.ts +0 -2796
  62. package/src/__tests__/dev.test.tsx +0 -496
  63. package/src/__tests__/dev2.test.tsx +0 -154
  64. package/src/__tests__/guess-worker-format.test.ts +0 -58
  65. package/src/__tests__/helpers/clipboardy-mock.js +0 -4
  66. package/src/__tests__/helpers/cmd-shim.d.ts +0 -11
  67. package/src/__tests__/helpers/faye-websocket.d.ts +0 -6
  68. package/src/__tests__/helpers/mock-account-id.ts +0 -48
  69. package/src/__tests__/helpers/mock-bin.ts +0 -36
  70. package/src/__tests__/helpers/mock-cfetch.ts +0 -207
  71. package/src/__tests__/helpers/mock-console.ts +0 -70
  72. package/src/__tests__/helpers/mock-dialogs.ts +0 -65
  73. package/src/__tests__/helpers/mock-kv.ts +0 -33
  74. package/src/__tests__/helpers/mock-stdin.ts +0 -103
  75. package/src/__tests__/helpers/mock-web-socket.ts +0 -29
  76. package/src/__tests__/helpers/run-in-tmp.ts +0 -32
  77. package/src/__tests__/helpers/run-wrangler.ts +0 -16
  78. package/src/__tests__/helpers/write-wrangler-toml.ts +0 -17
  79. package/src/__tests__/https-options.test.ts +0 -127
  80. package/src/__tests__/index.test.ts +0 -991
  81. package/src/__tests__/jest.setup.ts +0 -69
  82. package/src/__tests__/kv.test.ts +0 -1382
  83. package/src/__tests__/logout.test.ts +0 -52
  84. package/src/__tests__/package-manager.test.ts +0 -206
  85. package/src/__tests__/pages.test.ts +0 -44
  86. package/src/__tests__/parse.test.ts +0 -322
  87. package/src/__tests__/publish.test.ts +0 -2704
  88. package/src/__tests__/r2.test.ts +0 -198
  89. package/src/__tests__/route.test.ts +0 -45
  90. package/src/__tests__/secret.test.ts +0 -425
  91. package/src/__tests__/sentry.test.ts +0 -146
  92. package/src/__tests__/tail.test.ts +0 -564
  93. package/src/__tests__/whoami.test.tsx +0 -100
  94. package/src/bundle.ts +0 -142
  95. package/src/cfetch/index.ts +0 -104
  96. package/src/cfetch/internal.ts +0 -139
  97. package/src/cli.ts +0 -19
  98. package/src/config/README.md +0 -107
  99. package/src/config/config.ts +0 -218
  100. package/src/config/diagnostics.ts +0 -80
  101. package/src/config/environment.ts +0 -366
  102. package/src/config/index.ts +0 -62
  103. package/src/config/validation-helpers.ts +0 -486
  104. package/src/config/validation.ts +0 -1090
  105. package/src/create-worker-preview.ts +0 -166
  106. package/src/create-worker-upload-form.ts +0 -217
  107. package/src/dev/dev.tsx +0 -364
  108. package/src/dev/local.tsx +0 -232
  109. package/src/dev/remote.tsx +0 -203
  110. package/src/dev/use-esbuild.ts +0 -100
  111. package/src/dialogs.tsx +0 -77
  112. package/src/entry.ts +0 -208
  113. package/src/environment-variables.ts +0 -33
  114. package/src/https-options.ts +0 -122
  115. package/src/index.tsx +0 -2373
  116. package/src/inspect.ts +0 -617
  117. package/src/kv.ts +0 -319
  118. package/src/miniflare-cli/README.md +0 -30
  119. package/src/miniflare-cli/enum-keys.ts +0 -17
  120. package/src/miniflare-cli/index.ts +0 -37
  121. package/src/module-collection.ts +0 -258
  122. package/src/open-in-browser.ts +0 -25
  123. package/src/package-manager.ts +0 -126
  124. package/src/pages.tsx +0 -1047
  125. package/src/parse.ts +0 -183
  126. package/src/paths.ts +0 -26
  127. package/src/proxy.ts +0 -440
  128. package/src/publish.ts +0 -347
  129. package/src/r2.ts +0 -50
  130. package/src/reporting.ts +0 -131
  131. package/src/selfsigned.d.ts +0 -29
  132. package/src/sites.tsx +0 -280
  133. package/src/tail/filters.ts +0 -279
  134. package/src/tail/index.ts +0 -299
  135. package/src/tail/printing.ts +0 -62
  136. package/src/user.tsx +0 -1118
  137. package/src/whoami.tsx +0 -69
  138. package/src/worker.ts +0 -163
  139. package/templates/static-asset-facade.js +0 -47
  140. package/vendor/@cloudflare/kv-asset-handler/CHANGELOG.md +0 -332
  141. package/vendor/@cloudflare/kv-asset-handler/LICENSE_APACHE +0 -176
  142. package/vendor/@cloudflare/kv-asset-handler/LICENSE_MIT +0 -25
  143. package/vendor/@cloudflare/kv-asset-handler/README.md +0 -245
  144. package/vendor/@cloudflare/kv-asset-handler/dist/index.d.ts +0 -32
  145. package/vendor/@cloudflare/kv-asset-handler/dist/index.js +0 -354
  146. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.d.ts +0 -13
  147. package/vendor/@cloudflare/kv-asset-handler/dist/mocks.js +0 -148
  148. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.d.ts +0 -1
  149. package/vendor/@cloudflare/kv-asset-handler/dist/test/getAssetFromKV.js +0 -436
  150. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.d.ts +0 -1
  151. package/vendor/@cloudflare/kv-asset-handler/dist/test/mapRequestToAsset.js +0 -40
  152. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.d.ts +0 -1
  153. package/vendor/@cloudflare/kv-asset-handler/dist/test/serveSinglePageApp.js +0 -42
  154. package/vendor/@cloudflare/kv-asset-handler/dist/types.d.ts +0 -26
  155. package/vendor/@cloudflare/kv-asset-handler/dist/types.js +0 -31
  156. package/vendor/@cloudflare/kv-asset-handler/package.json +0 -52
  157. package/vendor/@cloudflare/kv-asset-handler/src/index.ts +0 -296
  158. package/vendor/@cloudflare/kv-asset-handler/src/mocks.ts +0 -136
  159. package/vendor/@cloudflare/kv-asset-handler/src/test/getAssetFromKV.ts +0 -464
  160. package/vendor/@cloudflare/kv-asset-handler/src/test/mapRequestToAsset.ts +0 -33
  161. package/vendor/@cloudflare/kv-asset-handler/src/test/serveSinglePageApp.ts +0 -42
  162. package/vendor/@cloudflare/kv-asset-handler/src/types.ts +0 -39
  163. package/vendor/wrangler-mime/CHANGELOG.md +0 -289
  164. package/vendor/wrangler-mime/LICENSE +0 -21
  165. package/vendor/wrangler-mime/Mime.js +0 -97
  166. package/vendor/wrangler-mime/README.md +0 -187
  167. package/vendor/wrangler-mime/cli.js +0 -46
  168. package/vendor/wrangler-mime/index.js +0 -4
  169. package/vendor/wrangler-mime/lite.js +0 -4
  170. package/vendor/wrangler-mime/package.json +0 -52
  171. package/vendor/wrangler-mime/types/other.js +0 -1
  172. package/vendor/wrangler-mime/types/standard.js +0 -1
  173. package/wrangler-dist/cli.js.map +0 -7
@@ -1,62 +0,0 @@
1
- import { findUpSync } from "find-up";
2
- import { parseTOML, readFileSync } from "../parse";
3
- import { normalizeAndValidateConfig } from "./validation";
4
- import type { Config, RawConfig } from "./config";
5
-
6
- export type {
7
- Config,
8
- RawConfig,
9
- ConfigFields,
10
- DevConfig,
11
- RawDevConfig,
12
- } from "./config";
13
- export type {
14
- Environment,
15
- RawEnvironment,
16
- ConfigModuleRuleType,
17
- } from "./environment";
18
-
19
- /**
20
- * Get the Wrangler configuration; read it from the give `configPath` if available.
21
- */
22
- export function readConfig(
23
- configPath: string | undefined,
24
- envName: string | undefined
25
- ): Config {
26
- let rawConfig: RawConfig = {};
27
- if (!configPath) {
28
- configPath = findWranglerToml();
29
- }
30
-
31
- // Load the configuration from disk if available
32
- if (configPath) {
33
- rawConfig = parseTOML(readFileSync(configPath), configPath);
34
- }
35
-
36
- // Process the top-level configuration.
37
- const { config, diagnostics } = normalizeAndValidateConfig(
38
- rawConfig,
39
- configPath,
40
- envName
41
- );
42
-
43
- if (diagnostics.hasWarnings()) {
44
- console.warn(diagnostics.renderWarnings());
45
- }
46
- if (diagnostics.hasErrors()) {
47
- throw new Error(diagnostics.renderErrors());
48
- }
49
-
50
- return config;
51
- }
52
-
53
- /**
54
- * Find the wrangler.toml file by searching up the file-system
55
- * from the current working directory.
56
- */
57
- export function findWranglerToml(
58
- referencePath: string = process.cwd()
59
- ): string | undefined {
60
- const configPath = findUpSync("wrangler.toml", { cwd: referencePath });
61
- return configPath;
62
- }
@@ -1,486 +0,0 @@
1
- import type { RawConfig } from "./config";
2
- import type { Diagnostics } from "./diagnostics";
3
- import type { Environment, RawEnvironment } from "./environment";
4
-
5
- /**
6
- * Mark a field as deprecated.
7
- *
8
- * This function will add a diagnostics warning if the deprecated field is found in the `rawEnv` (or an error if it's also a breaking deprecation)
9
- * The `fieldPath` is a dot separated property path, e.g. `"build.upload.format"`.
10
- */
11
- export function deprecated<T extends object>(
12
- diagnostics: Diagnostics,
13
- config: T,
14
- fieldPath: DeepKeyOf<T>,
15
- message: string,
16
- remove: boolean,
17
- breaking = false
18
- ): void {
19
- const result = unwindPropertyPath(config, fieldPath);
20
- if (result !== undefined && result.field in result.container) {
21
- (breaking ? diagnostics.errors : diagnostics.warnings).push(
22
- `DEPRECATION: "${fieldPath}":\n${message}`
23
- );
24
- if (remove) {
25
- delete (result.container as Record<string, unknown>)[result.field];
26
- }
27
- }
28
- }
29
-
30
- /**
31
- * Mark a field as experimental.
32
- *
33
- * This function will add a diagnostics warning if the experimental field is found in the `rawEnv`.
34
- * The `fieldPath` is a dot separated property path, e.g. `"build.upload.format"`.
35
- */
36
- export function experimental<T extends object>(
37
- diagnostics: Diagnostics,
38
- config: T,
39
- fieldPath: DeepKeyOf<T>
40
- ): void {
41
- const result = unwindPropertyPath(config, fieldPath);
42
- if (result !== undefined && result.field in result.container) {
43
- diagnostics.warnings.push(
44
- `"${fieldPath}" fields are experimental and may change or break at any time.`
45
- );
46
- }
47
- }
48
-
49
- /**
50
- * Get an inheritable environment field, after computing and validating its value.
51
- *
52
- * If the field is not defined in the given environment, then fallback to the value from the top-level config,
53
- * and then the `defaultValue`.
54
- */
55
- export function inheritable<K extends keyof Environment>(
56
- diagnostics: Diagnostics,
57
- topLevelEnv: Environment | undefined,
58
- rawEnv: RawEnvironment,
59
- field: K,
60
- validate: ValidatorFn,
61
- defaultValue: Environment[K]
62
- ): Environment[K] {
63
- validate(diagnostics, field, rawEnv[field], topLevelEnv);
64
- return (
65
- (rawEnv[field] as Environment[K]) ?? topLevelEnv?.[field] ?? defaultValue
66
- );
67
- }
68
-
69
- /**
70
- * Get an inheritable environment field, but only if we are in legacy environments
71
- */
72
- export function inheritableInLegacyEnvironments<K extends keyof Environment>(
73
- diagnostics: Diagnostics,
74
- isLegacyEnv: boolean | undefined,
75
- topLevelEnv: Environment | undefined,
76
- rawEnv: RawEnvironment,
77
- field: K,
78
- validate: ValidatorFn,
79
- defaultValue: Environment[K]
80
- ): Environment[K] {
81
- return topLevelEnv === undefined || isLegacyEnv === true
82
- ? inheritable(
83
- diagnostics,
84
- topLevelEnv,
85
- rawEnv,
86
- field,
87
- validate,
88
- defaultValue
89
- )
90
- : notAllowedInNamedServiceEnvironment(
91
- diagnostics,
92
- topLevelEnv,
93
- rawEnv,
94
- field
95
- );
96
- }
97
-
98
- /**
99
- * Log an error if this named environment is trying to override the value in the top-level
100
- * environment, which is not allow for this field.
101
- */
102
- function notAllowedInNamedServiceEnvironment<K extends keyof Environment>(
103
- diagnostics: Diagnostics,
104
- topLevelEnv: Environment,
105
- rawEnv: RawEnvironment,
106
- field: K
107
- ): Environment[K] {
108
- if (field in rawEnv) {
109
- diagnostics.errors.push(
110
- `The "${field}" field is not allowed in named service environments.\n` +
111
- `Please remove the field from this environment.`
112
- );
113
- }
114
- return topLevelEnv[field];
115
- }
116
-
117
- /**
118
- * Get a not inheritable environment field, after computing and validating its value.
119
- *
120
- * If the field is not defined in the given environment but it is defined in the top-level config,
121
- * then log a warning and return the `defaultValue`.
122
- */
123
- export function notInheritable<K extends keyof Environment>(
124
- diagnostics: Diagnostics,
125
- topLevelEnv: Environment | undefined,
126
- rawConfig: RawConfig | undefined,
127
- rawEnv: RawEnvironment,
128
- envName: string,
129
- field: K,
130
- validate: ValidatorFn,
131
- defaultValue: Environment[K]
132
- ): Environment[K] {
133
- if (rawEnv[field] !== undefined) {
134
- validate(diagnostics, field, rawEnv[field], topLevelEnv);
135
- } else {
136
- if (rawConfig?.[field] !== undefined) {
137
- diagnostics.warnings.push(
138
- `"${field}" exists at the top level, but not on "env.${envName}".\n` +
139
- `This is not what you probably want, since "${field}" is not inherited by environments.\n` +
140
- `Please add "${field}" to "env.${envName}".`
141
- );
142
- }
143
- }
144
- return (rawEnv[field] as Environment[K]) ?? defaultValue;
145
- }
146
-
147
- // Idea taken from https://stackoverflow.com/a/66661477
148
- type DeepKeyOf<T> = (
149
- T extends object
150
- ? {
151
- [K in Exclude<keyof T, symbol>]: `${K}${DotPrefix<DeepKeyOf<T[K]>>}`;
152
- }[Exclude<keyof T, symbol>]
153
- : ""
154
- ) extends infer D
155
- ? Extract<D, string>
156
- : never;
157
-
158
- type DotPrefix<T extends string> = T extends "" ? "" : `.${T}`;
159
-
160
- /**
161
- * Return a container object and field name for the last property in a given property path.
162
- *
163
- * For example, given a path of `"build.upload.format"`) and a starting `root` object
164
- * this will return:
165
- *
166
- * ```
167
- * { container: root.build.upload, field: "format" }
168
- * ```
169
- */
170
- function unwindPropertyPath<T extends object>(
171
- root: T,
172
- path: DeepKeyOf<T>
173
- ): { container: object; field: string } | undefined {
174
- let container: object = root;
175
- const parts = (path as string).split(".");
176
- for (let i = 0; i < parts.length - 1; i++) {
177
- if (!hasProperty(container, parts[i])) {
178
- return;
179
- }
180
- container = container[parts[i]];
181
- }
182
- return { container, field: parts[parts.length - 1] };
183
- }
184
-
185
- /**
186
- * The type of a function that can be used to validate a configuration field.
187
- */
188
- export type ValidatorFn = (
189
- diagnostics: Diagnostics,
190
- field: string,
191
- value: unknown,
192
- topLevelEnv: Environment | undefined
193
- ) => boolean;
194
-
195
- /**
196
- * Validate that the field is a string.
197
- */
198
- export const isString: ValidatorFn = (diagnostics, field, value) => {
199
- if (value !== undefined && typeof value !== "string") {
200
- diagnostics.errors.push(
201
- `Expected "${field}" to be of type string but got ${JSON.stringify(
202
- value
203
- )}.`
204
- );
205
- return false;
206
- }
207
- return true;
208
- };
209
-
210
- /**
211
- * Validate that the field is an array of strings.
212
- */
213
- export const isStringArray: ValidatorFn = (diagnostics, field, value) => {
214
- if (
215
- value !== undefined &&
216
- (!Array.isArray(value) || value.some((item) => typeof item !== "string"))
217
- ) {
218
- diagnostics.errors.push(
219
- `Expected "${field}" to be of type string array but got ${JSON.stringify(
220
- value
221
- )}.`
222
- );
223
- return false;
224
- }
225
- return true;
226
- };
227
-
228
- /**
229
- * Validate that the field is an object containing the given properties.
230
- */
231
- export const isObjectWith =
232
- (...properties: string[]): ValidatorFn =>
233
- (diagnostics, field, value) => {
234
- if (
235
- value !== undefined &&
236
- (typeof value !== "object" ||
237
- value === null ||
238
- !properties.every((prop) => prop in value))
239
- ) {
240
- diagnostics.errors.push(
241
- `Expected "${field}" to be of type object, containing properties ${properties}, but got ${JSON.stringify(
242
- value
243
- )}.`
244
- );
245
- return false;
246
- }
247
- return true;
248
- };
249
-
250
- /**
251
- * Validate that the field value is one of the given choices.
252
- */
253
- export const isOneOf =
254
- (...choices: unknown[]): ValidatorFn =>
255
- (diagnostics, field, value) => {
256
- if (value !== undefined && !choices.some((choice) => value === choice)) {
257
- diagnostics.errors.push(
258
- `Expected "${field}" field to be one of ${JSON.stringify(
259
- choices
260
- )} but got ${JSON.stringify(value)}.`
261
- );
262
- return false;
263
- }
264
- return true;
265
- };
266
-
267
- /**
268
- * Aggregate multiple validator functions
269
- */
270
- export const all = (...validations: ValidatorFn[]): ValidatorFn => {
271
- return (diagnostics, field, value, config) => {
272
- let passedValidations = true;
273
-
274
- for (const validate of validations) {
275
- if (!validate(diagnostics, field, value, config)) {
276
- passedValidations = false;
277
- }
278
- }
279
-
280
- return passedValidations;
281
- };
282
- };
283
-
284
- /**
285
- * Check that the field is mutually exclusive with a list of other fields.
286
- *
287
- * @param container the container of the fields to check against.
288
- * @param fields the names of the fields to check against.
289
- */
290
- export const isMutuallyExclusiveWith = <T extends RawEnvironment | RawConfig>(
291
- container: T,
292
- ...fields: (keyof T)[]
293
- ): ValidatorFn => {
294
- return (diagnostics, field, value) => {
295
- if (value === undefined) {
296
- return true;
297
- }
298
-
299
- for (const exclusiveWith of fields) {
300
- if (container[exclusiveWith] !== undefined) {
301
- diagnostics.errors.push(
302
- `Expected exactly one of the following fields ${JSON.stringify([
303
- field,
304
- ...fields,
305
- ])}.`
306
- );
307
- return false;
308
- }
309
- }
310
-
311
- return true;
312
- };
313
- };
314
-
315
- /**
316
- * Validate that the field is a boolean.
317
- */
318
- export const isBoolean: ValidatorFn = (diagnostics, field, value) => {
319
- if (value !== undefined && typeof value !== "boolean") {
320
- diagnostics.errors.push(
321
- `Expected "${field}" to be of type boolean but got ${JSON.stringify(
322
- value
323
- )}.`
324
- );
325
- return false;
326
- }
327
- return true;
328
- };
329
-
330
- /**
331
- * Validate that the required field exists and has the expected type.
332
- */
333
- export const validateRequiredProperty = (
334
- diagnostics: Diagnostics,
335
- container: string,
336
- key: string,
337
- value: unknown,
338
- type: string,
339
- choices?: unknown[]
340
- ): boolean => {
341
- if (container) {
342
- container += ".";
343
- }
344
- if (value === undefined) {
345
- diagnostics.errors.push(`"${container}${key}" is a required field.`);
346
- return false;
347
- } else if (typeof value !== type) {
348
- diagnostics.errors.push(
349
- `Expected "${container}${key}" to be of type ${type} but got ${JSON.stringify(
350
- value
351
- )}.`
352
- );
353
- return false;
354
- } else if (choices) {
355
- if (
356
- !isOneOf(...choices)(diagnostics, `${container}${key}`, value, undefined)
357
- ) {
358
- return false;
359
- }
360
- }
361
- return true;
362
- };
363
-
364
- /**
365
- * Validate that, if the optional field exists, then it has the expected type.
366
- */
367
- export const validateOptionalProperty = (
368
- diagnostics: Diagnostics,
369
- container: string,
370
- key: string,
371
- value: unknown,
372
- type: string,
373
- choices?: unknown[]
374
- ): boolean => {
375
- if (value !== undefined) {
376
- return validateRequiredProperty(
377
- diagnostics,
378
- container,
379
- key,
380
- value,
381
- type,
382
- choices
383
- );
384
- }
385
- return true;
386
- };
387
-
388
- /**
389
- * Validate that the field is an array of elements of the given type.
390
- */
391
- export const validateTypedArray = (
392
- diagnostics: Diagnostics,
393
- container: string,
394
- value: unknown,
395
- type: string
396
- ): boolean => {
397
- let isValid = true;
398
- if (!Array.isArray(value)) {
399
- diagnostics.errors.push(
400
- `Expected "${container}" to be an array of ${type}s but got ${JSON.stringify(
401
- value
402
- )}`
403
- );
404
- isValid = false;
405
- } else {
406
- for (let i = 0; i < value.length; i++) {
407
- isValid =
408
- validateRequiredProperty(
409
- diagnostics,
410
- container,
411
- `[${i}]`,
412
- value[i],
413
- type
414
- ) && isValid;
415
- }
416
- }
417
- return isValid;
418
- };
419
-
420
- /**
421
- * Validate that, if the optional field exists, it is an array of elements of the given type.
422
- */
423
- export const validateOptionalTypedArray = (
424
- diagnostics: Diagnostics,
425
- container: string,
426
- value: unknown,
427
- type: string
428
- ) => {
429
- if (value !== undefined) {
430
- return validateTypedArray(diagnostics, container, value, type);
431
- }
432
- return true;
433
- };
434
-
435
- /**
436
- * Test to see if `obj` has the required property `prop` of type `type`.
437
- */
438
- export const isRequiredProperty = <T extends object>(
439
- obj: object,
440
- prop: keyof T,
441
- type: string,
442
- choices?: unknown[]
443
- ): obj is T =>
444
- hasProperty<T>(obj, prop) &&
445
- typeof obj[prop] === type &&
446
- (choices === undefined || choices.includes(obj[prop]));
447
-
448
- /**
449
- * Test to see if `obj` has the optional property `prop` of type `type`.
450
- */
451
- export const isOptionalProperty = <T extends object>(
452
- obj: object,
453
- prop: keyof T,
454
- type: string
455
- ): obj is T => !hasProperty<T>(obj, prop) || typeof obj[prop] === type;
456
-
457
- /**
458
- * Test to see if `obj` has the property `prop`.
459
- */
460
- export const hasProperty = <T extends object>(
461
- obj: object,
462
- property: keyof T
463
- ): obj is T => property in obj;
464
-
465
- /**
466
- * Add warning messages about any properties in the given field that are not expected to be there.
467
- */
468
- export const validateAdditionalProperties = (
469
- diagnostics: Diagnostics,
470
- fieldPath: string,
471
- restProps: Iterable<string>,
472
- knownProps: Iterable<string>
473
- ): boolean => {
474
- const restPropSet = new Set(restProps);
475
- for (const knownProp of knownProps) {
476
- restPropSet.delete(knownProp);
477
- }
478
- if (restPropSet.size > 0) {
479
- const fields = Array.from(restPropSet.keys()).map((field) => `"${field}"`);
480
- diagnostics.warnings.push(
481
- `Unexpected fields found in ${fieldPath} field: ${fields}`
482
- );
483
- return false;
484
- }
485
- return true;
486
- };