schema-option-transform.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738
  1. "use strict";
  2. /**
  3. * @license
  4. * Copyright Google LLC All Rights Reserved.
  5. *
  6. * Use of this source code is governed by an MIT-style license that can be
  7. * found in the LICENSE file at https://angular.dev/license
  8. */
  9. Object.defineProperty(exports, "__esModule", { value: true });
  10. exports.InvalidInputOptions = void 0;
  11. exports.validateOptionsWithSchema = validateOptionsWithSchema;
  12. const core_1 = require("@angular-devkit/core");
  13. const rxjs_1 = require("rxjs");
  14. const operators_1 = require("rxjs/operators");
  15. class InvalidInputOptions extends core_1.schema.SchemaValidationException {
  16. constructor(options, errors) {
  17. super(errors, `Schematic input does not validate against the Schema: ${JSON.stringify(options)}\nErrors:\n`);
  18. }
  19. }
  20. exports.InvalidInputOptions = InvalidInputOptions;
  21. // This can only be used in NodeJS.
  22. function validateOptionsWithSchema(registry) {
  23. return (schematic, options, context) => {
  24. // Prevent a schematic from changing the options object by making a copy of it.
  25. options = (0, core_1.deepCopy)(options);
  26. const withPrompts = context ? context.interactive : true;
  27. if (schematic.schema && schematic.schemaJson) {
  28. // Make a deep copy of options.
  29. return (0, rxjs_1.from)(registry.compile(schematic.schemaJson)).pipe((0, operators_1.mergeMap)((validator) => validator(options, { withPrompts })), (0, operators_1.first)(), (0, operators_1.map)((result) => {
  30. if (!result.success) {
  31. throw new InvalidInputOptions(options, result.errors || []);
  32. }
  33. return options;
  34. }));
  35. }
  36. return (0, rxjs_1.of)(options);
  37. };
  38. }