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.io/license
|
---|
8 | */
|
---|
9 | Object.defineProperty(exports, "__esModule", { value: true });
|
---|
10 | exports.validateOptionsWithSchema = exports.InvalidInputOptions = void 0;
|
---|
11 | const core_1 = require("@angular-devkit/core");
|
---|
12 | const rxjs_1 = require("rxjs");
|
---|
13 | const operators_1 = require("rxjs/operators");
|
---|
14 | class InvalidInputOptions extends core_1.schema.SchemaValidationException {
|
---|
15 | constructor(options, errors) {
|
---|
16 | super(errors, `Schematic input does not validate against the Schema: ${JSON.stringify(options)}\nErrors:\n`);
|
---|
17 | }
|
---|
18 | }
|
---|
19 | exports.InvalidInputOptions = InvalidInputOptions;
|
---|
20 | // This can only be used in NodeJS.
|
---|
21 | function validateOptionsWithSchema(registry) {
|
---|
22 | return (schematic, options, context) => {
|
---|
23 | // Prevent a schematic from changing the options object by making a copy of it.
|
---|
24 | options = core_1.deepCopy(options);
|
---|
25 | const withPrompts = context ? context.interactive : true;
|
---|
26 | if (schematic.schema && schematic.schemaJson) {
|
---|
27 | // Make a deep copy of options.
|
---|
28 | return registry.compile(schematic.schemaJson).pipe(operators_1.mergeMap((validator) => validator(options, { withPrompts })), operators_1.first(), operators_1.map((result) => {
|
---|
29 | if (!result.success) {
|
---|
30 | throw new InvalidInputOptions(options, result.errors || []);
|
---|
31 | }
|
---|
32 | return options;
|
---|
33 | }));
|
---|
34 | }
|
---|
35 | return rxjs_1.of(options);
|
---|
36 | };
|
---|
37 | }
|
---|
38 | exports.validateOptionsWithSchema = validateOptionsWithSchema;
|
---|