@sanity/blueprints 0.17.1 → 0.18.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -53,7 +53,7 @@ import { runValidation } from '../utils/validation.js';
53
53
  */
54
54
  export function defineBlueprint(blueprintConfig) {
55
55
  const { organizationId, projectId, stackId, blueprintVersion, resources, values, outputs } = blueprintConfig;
56
- runValidation(() => validateBlueprint(blueprintConfig));
56
+ runValidation(() => validateBlueprint(blueprintConfig), { throwError: true });
57
57
  function blueprint() {
58
58
  return {
59
59
  $schema: 'https://schemas.sanity.io/blueprints/v2024-10-01/blueprint.schema.json',
@@ -1 +1 @@
1
- {"version":3,"file":"blueprints.js","sourceRoot":"","sources":["../../src/definers/blueprints.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiE,iBAAiB,EAAC,MAAM,aAAa,CAAA;AAC7G,OAAO,EAAC,aAAa,EAAC,MAAM,wBAAwB,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,eAAe,CAAC,eAAkE;IAChG,MAAM,EAAC,cAAc,EAAE,SAAS,EAAE,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAC,GAAG,eAAe,CAAA;IAE1G,aAAa,CAAC,GAAG,EAAE,CAAC,iBAAiB,CAAC,eAAe,CAAC,CAAC,CAAA;IAEvD,SAAS,SAAS;QAChB,OAAO;YACL,OAAO,EAAE,wEAAwE;YACjF,gBAAgB,EAAE,gBAAgB,IAAI,YAAY;YAClD,SAAS;YACT,MAAM;YACN,OAAO;SACR,CAAA;IACH,CAAC;IAED,SAAS,CAAC,cAAc,GAAG,cAAc,CAAA;IACzC,SAAS,CAAC,SAAS,GAAG,SAAS,CAAA;IAC/B,SAAS,CAAC,OAAO,GAAG,OAAO,CAAA;IAE3B,OAAO,SAAS,CAAA;AAClB,CAAC"}
1
+ {"version":3,"file":"blueprints.js","sourceRoot":"","sources":["../../src/definers/blueprints.ts"],"names":[],"mappings":"AAAA,OAAO,EAAiE,iBAAiB,EAAC,MAAM,aAAa,CAAA;AAC7G,OAAO,EAAC,aAAa,EAAC,MAAM,wBAAwB,CAAA;AAEpD;;;;;;;;;;;;;;;;;;;;;;;;GAwBG;AACH;;;;;;;;;;;;;;;;;;;;;;;;;GAyBG;AACH,MAAM,UAAU,eAAe,CAAC,eAAkE;IAChG,MAAM,EAAC,cAAc,EAAE,SAAS,EAAE,OAAO,EAAE,gBAAgB,EAAE,SAAS,EAAE,MAAM,EAAE,OAAO,EAAC,GAAG,eAAe,CAAA;IAE1G,aAAa,CAAC,GAAG,EAAE,CAAC,iBAAiB,CAAC,eAAe,CAAC,EAAE,EAAC,UAAU,EAAE,IAAI,EAAC,CAAC,CAAA;IAE3E,SAAS,SAAS;QAChB,OAAO;YACL,OAAO,EAAE,wEAAwE;YACjF,gBAAgB,EAAE,gBAAgB,IAAI,YAAY;YAClD,SAAS;YACT,MAAM;YACN,OAAO;SACR,CAAA;IACH,CAAC;IAED,SAAS,CAAC,cAAc,GAAG,cAAc,CAAA;IACzC,SAAS,CAAC,SAAS,GAAG,SAAS,CAAA;IAC/B,SAAS,CAAC,OAAO,GAAG,OAAO,CAAA;IAE3B,OAAO,SAAS,CAAA;AAClB,CAAC"}
@@ -1,10 +1,24 @@
1
1
  import type { BlueprintError } from '../types/errors';
2
2
  /**
3
- * Executes the given validator function and throws a formatted error if any are returned.
3
+ * Executes the given validator function and throws a formatted error if any are returned and throwError is enabled.
4
+ * Otherwise collects errors for use later during blueprint validation.
4
5
  * @param validator A function that returns a list of validation errors.
5
6
  * @internal
6
7
  */
7
- export declare function runValidation(validator: () => BlueprintError[]): void;
8
+ export declare function runValidation(validator: () => BlueprintError[], options?: {
9
+ throwError?: boolean;
10
+ }): void;
11
+ /**
12
+ * Gets the current set of errors that were returned during validation when resource definers were invoked.
13
+ * @returns The errors collected during the validation of resources
14
+ * @internal
15
+ */
16
+ export declare function getCollectedErrors(): BlueprintError[];
17
+ /**
18
+ * Resets the list of collected errors back to an empty list.
19
+ * @internal
20
+ */
21
+ export declare function resetCollectedErrors(): void;
8
22
  /**
9
23
  * Checks whether a value is a blueprint reference expression.
10
24
  *
@@ -1,14 +1,32 @@
1
+ const collectedErrors = [];
1
2
  /**
2
- * Executes the given validator function and throws a formatted error if any are returned.
3
+ * Executes the given validator function and throws a formatted error if any are returned and throwError is enabled.
4
+ * Otherwise collects errors for use later during blueprint validation.
3
5
  * @param validator A function that returns a list of validation errors.
4
6
  * @internal
5
7
  */
6
- export function runValidation(validator) {
8
+ export function runValidation(validator, options) {
7
9
  const errors = validator();
8
- if (errors.length > 0) {
10
+ if (options?.throwError && errors.length > 0) {
9
11
  const message = errors.map((err) => err.message).join('\n');
10
12
  throw new Error(message);
11
13
  }
14
+ collectedErrors.push(...errors);
15
+ }
16
+ /**
17
+ * Gets the current set of errors that were returned during validation when resource definers were invoked.
18
+ * @returns The errors collected during the validation of resources
19
+ * @internal
20
+ */
21
+ export function getCollectedErrors() {
22
+ return collectedErrors;
23
+ }
24
+ /**
25
+ * Resets the list of collected errors back to an empty list.
26
+ * @internal
27
+ */
28
+ export function resetCollectedErrors() {
29
+ collectedErrors.splice(0, collectedErrors.length);
12
30
  }
13
31
  const REFERENCE_PREFIXES = ['$.resources.', '$.values.', '$.parameters.', '$.params.'];
14
32
  /**
@@ -1 +1 @@
1
- {"version":3,"file":"validation.js","sourceRoot":"","sources":["../../src/utils/validation.ts"],"names":[],"mappings":"AAEA;;;;GAIG;AACH,MAAM,UAAU,aAAa,CAAC,SAAiC;IAC7D,MAAM,MAAM,GAAG,SAAS,EAAE,CAAA;IAC1B,IAAI,MAAM,CAAC,MAAM,GAAG,CAAC,EAAE,CAAC;QACtB,MAAM,OAAO,GAAG,MAAM,CAAC,GAAG,CAAC,CAAC,GAAG,EAAE,EAAE,CAAC,GAAG,CAAC,OAAO,CAAC,CAAC,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AE,WAAW,CAAC,CAAA;AAEtF;;;;;;;GAOG;AACH,MAAM,UAAU,WAAW,CAAC,KAAc;IACxC,OAAO,OAAO,KAAK,KAAK,QAAQ,IAAI,kBAAkB,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,KAAK,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAA;AACnG,CAAC"}
@@ -1,3 +1,4 @@
1
+ import { getCollectedErrors, resetCollectedErrors } from '../utils/validation.js';
1
2
  /**
2
3
  * Validates that the given input is a valid Blueprint
3
4
  * @param blueprintConfig The blueprint configuration to be validated
@@ -15,6 +16,9 @@ export function validateBlueprint(blueprintConfig) {
15
16
  if (resources && !Array.isArray(resources))
16
17
  errors.push({ type: 'invalid_format', message: '`resources` must be an array' });
17
18
  }
19
+ // add any errors that were collected when definers were called
20
+ errors.push(...getCollectedErrors());
21
+ resetCollectedErrors();
18
22
  return errors;
19
23
  }
20
24
  //# sourceMappingURL=blueprints.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"blueprints.js","sourceRoot":"","sources":["../../src/validation/blueprints.ts"],"names":[],"mappings":"AAEA;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,eAAwB;IACxD,IAAI,CAAC,eAAe;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,mCAAmC,EAAC,CAAC,CAAA;IACpG,IAAI,OAAO,eAAe,KAAK,QAAQ;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,oCAAoC,EAAC,CAAC,CAAA;IAEvH,MAAM,MAAM,GAAqB,EAAE,CAAA;IAEnC,IAAI,WAAW,IAAI,eAAe,EAAE,CAAC;QACnC,MAAM,EAAC,SAAS,EAAC,GAAG,eAAe,CAAA;QAEnC,IAAI,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;YAAE,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,8BAA8B,EAAC,CAAC,CAAA;IAC5H,CAAC;IAED,OAAO,MAAM,CAAA;AACf,CAAC"}
1
+ {"version":3,"file":"blueprints.js","sourceRoot":"","sources":["../../src/validation/blueprints.ts"],"names":[],"mappings":"AACA,OAAO,EAAC,kBAAkB,EAAE,oBAAoB,EAAC,MAAM,wBAAwB,CAAA;AAE/E;;;;;GAKG;AACH,MAAM,UAAU,iBAAiB,CAAC,eAAwB;IACxD,IAAI,CAAC,eAAe;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,eAAe,EAAE,OAAO,EAAE,mCAAmC,EAAC,CAAC,CAAA;IACpG,IAAI,OAAO,eAAe,KAAK,QAAQ;QAAE,OAAO,CAAC,EAAC,IAAI,EAAE,cAAc,EAAE,OAAO,EAAE,oCAAoC,EAAC,CAAC,CAAA;IAEvH,MAAM,MAAM,GAAqB,EAAE,CAAA;IAEnC,IAAI,WAAW,IAAI,eAAe,EAAE,CAAC;QACnC,MAAM,EAAC,SAAS,EAAC,GAAG,eAAe,CAAA;QAEnC,IAAI,SAAS,IAAI,CAAC,KAAK,CAAC,OAAO,CAAC,SAAS,CAAC;YAAE,MAAM,CAAC,IAAI,CAAC,EAAC,IAAI,EAAE,gBAAgB,EAAE,OAAO,EAAE,8BAA8B,EAAC,CAAC,CAAA;IAC5H,CAAC;IAED,+DAA+D;IAC/D,MAAM,CAAC,IAAI,CAAC,GAAG,kBAAkB,EAAE,CAAC,CAAA;IACpC,oBAAoB,EAAE,CAAA;IAEtB,OAAO,MAAM,CAAA;AACf,CAAC"}
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@sanity/blueprints",
3
- "version": "0.17.1",
3
+ "version": "0.18.0",
4
4
  "description": "Helper methods and type definitions for Sanity Blueprints",
5
5
  "keywords": [
6
6
  "sanity",
@@ -55,7 +55,7 @@ import {runValidation} from '../utils/validation.js'
55
55
  export function defineBlueprint(blueprintConfig: Partial<Blueprint> & Partial<BlueprintsApiConfig>): BlueprintModule {
56
56
  const {organizationId, projectId, stackId, blueprintVersion, resources, values, outputs} = blueprintConfig
57
57
 
58
- runValidation(() => validateBlueprint(blueprintConfig))
58
+ runValidation(() => validateBlueprint(blueprintConfig), {throwError: true})
59
59
 
60
60
  function blueprint(): Blueprint {
61
61
  return {
@@ -1,16 +1,37 @@
1
1
  import type {BlueprintError} from '../types/errors'
2
2
 
3
+ const collectedErrors: BlueprintError[] = []
4
+
3
5
  /**
4
- * Executes the given validator function and throws a formatted error if any are returned.
6
+ * Executes the given validator function and throws a formatted error if any are returned and throwError is enabled.
7
+ * Otherwise collects errors for use later during blueprint validation.
5
8
  * @param validator A function that returns a list of validation errors.
6
9
  * @internal
7
10
  */
8
- export function runValidation(validator: () => BlueprintError[]) {
11
+ export function runValidation(validator: () => BlueprintError[], options?: {throwError?: boolean}) {
9
12
  const errors = validator()
10
- if (errors.length > 0) {
13
+ if (options?.throwError && errors.length > 0) {
11
14
  const message = errors.map((err) => err.message).join('\n')
12
15
  throw new Error(message)
13
16
  }
17
+ collectedErrors.push(...errors)
18
+ }
19
+
20
+ /**
21
+ * Gets the current set of errors that were returned during validation when resource definers were invoked.
22
+ * @returns The errors collected during the validation of resources
23
+ * @internal
24
+ */
25
+ export function getCollectedErrors(): BlueprintError[] {
26
+ return collectedErrors
27
+ }
28
+
29
+ /**
30
+ * Resets the list of collected errors back to an empty list.
31
+ * @internal
32
+ */
33
+ export function resetCollectedErrors() {
34
+ collectedErrors.splice(0, collectedErrors.length)
14
35
  }
15
36
 
16
37
  const REFERENCE_PREFIXES = ['$.resources.', '$.values.', '$.parameters.', '$.params.']
@@ -1,4 +1,5 @@
1
1
  import type {BlueprintError} from '../index.js'
2
+ import {getCollectedErrors, resetCollectedErrors} from '../utils/validation.js'
2
3
 
3
4
  /**
4
5
  * Validates that the given input is a valid Blueprint
@@ -18,5 +19,9 @@ export function validateBlueprint(blueprintConfig: unknown): BlueprintError[] {
18
19
  if (resources && !Array.isArray(resources)) errors.push({type: 'invalid_format', message: '`resources` must be an array'})
19
20
  }
20
21
 
22
+ // add any errors that were collected when definers were called
23
+ errors.push(...getCollectedErrors())
24
+ resetCollectedErrors()
25
+
21
26
  return errors
22
27
  }