Skip to content

Commit 1747845

Browse files
clydinvikerman
authored andcommitted
fix(@angular-devkit/core): make JSON schema required work with prompts (#12548)
1 parent 431df79 commit 1747845

File tree

2 files changed

+42
-21
lines changed

2 files changed

+42
-21
lines changed

packages/angular_devkit/core/src/json/schema/prompt_spec.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,9 +43,9 @@ describe('Prompt Provider', () => {
4343

4444
registry.usePromptProvider(async definitions => {
4545
return {
46-
'\'bool\'': true,
47-
'\'test\'': 'two',
48-
'\'obj\'/\'deep\'/\'three\'': 'test3-answer',
46+
'/bool': true,
47+
'/test': 'two',
48+
'/obj/deep/three': 'test3-answer',
4949
};
5050
});
5151

packages/angular_devkit/core/src/json/schema/registry.ts

Lines changed: 39 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -355,6 +355,35 @@ export class CoreSchemaRegistry implements SchemaRegistry {
355355
updateData,
356356
schemaInfo.smartDefaultRecord,
357357
)),
358+
switchMap(updatedData => {
359+
if (validationOptions.withPrompts === false) {
360+
return of(updatedData);
361+
}
362+
363+
const visitor: JsonVisitor = (value, pointer) => {
364+
if (value !== undefined) {
365+
validationContext.promptFieldsWithValue.add(pointer);
366+
}
367+
368+
return value;
369+
};
370+
371+
return visitJson(updatedData, visitor, schema, this._resolver, validate);
372+
}),
373+
switchMap(updatedData => {
374+
if (validationOptions.withPrompts === false) {
375+
return of(updatedData);
376+
}
377+
378+
const definitions = schemaInfo.promptDefinitions
379+
.filter(def => !validationContext.promptFieldsWithValue.has(def.id));
380+
381+
if (this._promptProvider && definitions.length > 0) {
382+
return from(this._applyPrompts(updatedData, definitions));
383+
} else {
384+
return of(updatedData);
385+
}
386+
}),
358387
switchMap(updatedData => {
359388
const result = validate.call(validationContext, updatedData);
360389

@@ -372,22 +401,6 @@ export class CoreSchemaRegistry implements SchemaRegistry {
372401
return Promise.reject(err);
373402
}));
374403
}),
375-
switchMap(([data, valid]: [JsonValue, boolean]) => {
376-
if (validationOptions.withPrompts === false) {
377-
return of([data, valid]);
378-
}
379-
380-
const definitions = schemaInfo.promptDefinitions
381-
.filter(def => !validationContext.promptFieldsWithValue.has(def.id));
382-
383-
if (valid && this._promptProvider && definitions.length > 0) {
384-
return from(this._applyPrompts(data, definitions)).pipe(
385-
map(data => [data, valid]),
386-
);
387-
} else {
388-
return of([data, valid]);
389-
}
390-
}),
391404
switchMap(([data, valid]: [JsonValue, boolean]) => {
392405
if (valid) {
393406
let result = of(data);
@@ -508,7 +521,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
508521

509522
// tslint:disable-next-line:no-any
510523
const pathArray = ((it as any).dataPathArr as string[]).slice(1, it.dataLevel + 1);
511-
const path = pathArray.join('/');
524+
const path = '/' + pathArray.map(p => p.replace(/^\'/, '').replace(/\'$/, '')).join('/');
512525

513526
let type: string | undefined;
514527
let items: Array<string | { label: string, value: string | number | boolean }> | undefined;
@@ -609,9 +622,17 @@ export class CoreSchemaRegistry implements SchemaRegistry {
609622
return from(provider(prompts)).pipe(
610623
map(answers => {
611624
for (const path in answers) {
625+
const pathFragments = path.split('/').map(pf => {
626+
if (/^\d+$/.test(pf)) {
627+
return pf;
628+
} else {
629+
return '\'' + pf + '\'';
630+
}
631+
});
632+
612633
CoreSchemaRegistry._set(
613634
data,
614-
path.split('/'),
635+
pathFragments.slice(1),
615636
answers[path] as {},
616637
null,
617638
undefined,

0 commit comments

Comments
 (0)