schema-option-transform.js 1.6 KB

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