UNPKG

1.71 kBJavaScriptView Raw
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 */
9Object.defineProperty(exports, "__esModule", { value: true });
10exports.validateOptionsWithSchema = exports.InvalidInputOptions = void 0;
11const core_1 = require("@angular-devkit/core");
12const rxjs_1 = require("rxjs");
13const operators_1 = require("rxjs/operators");
14class 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}
19exports.InvalidInputOptions = InvalidInputOptions;
20// This can only be used in NodeJS.
21function validateOptionsWithSchema(registry) {
22 return (schematic, options, context) => {
23 // Prevent a schematic from changing the options object by making a copy of it.
24 options = (0, 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((0, operators_1.mergeMap)((validator) => validator(options, { withPrompts })), (0, operators_1.first)(), (0, operators_1.map)((result) => {
29 if (!result.success) {
30 throw new InvalidInputOptions(options, result.errors || []);
31 }
32 return options;
33 }));
34 }
35 return (0, rxjs_1.of)(options);
36 };
37}
38exports.validateOptionsWithSchema = validateOptionsWithSchema;