-
Notifications
You must be signed in to change notification settings - Fork 12k
/
Copy pathschema-option-transform.ts
53 lines (47 loc) · 1.67 KB
/
schema-option-transform.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
/**
* @license
* Copyright Google Inc. All Rights Reserved.
*
* Use of this source code is governed by an MIT-style license that can be
* found in the LICENSE file at https://angular.io/license
*/
import { deepCopy, schema } from '@angular-devkit/core';
import { Observable, of as observableOf } from 'rxjs';
import { first, map, mergeMap } from 'rxjs/operators';
import { FileSystemSchematicContext, FileSystemSchematicDescription } from './description';
export class InvalidInputOptions<T = {}> extends schema.SchemaValidationException {
constructor(options: T, errors: schema.SchemaValidatorError[]) {
super(
errors,
`Schematic input does not validate against the Schema: ${JSON.stringify(options)}\nErrors:\n`,
);
}
}
// This can only be used in NodeJS.
export function validateOptionsWithSchema(registry: schema.SchemaRegistry) {
return <T extends {}>(
schematic: FileSystemSchematicDescription,
options: T,
context?: FileSystemSchematicContext,
): Observable<T> => {
// Prevent a schematic from changing the options object by making a copy of it.
options = deepCopy(options);
const withPrompts = context ? context.interactive : true;
if (schematic.schema && schematic.schemaJson) {
// Make a deep copy of options.
return registry
.compile(schematic.schemaJson)
.pipe(
mergeMap(validator => validator(options, { withPrompts })),
first(),
map(result => {
if (!result.success) {
throw new InvalidInputOptions(options, result.errors || []);
}
return options;
}),
);
}
return observableOf(options);
};
}