Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(@angular-devkit/core): make JSON schema required work with prompts #12548

Merged
merged 1 commit into from Oct 10, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 3 additions & 3 deletions packages/angular_devkit/core/src/json/schema/prompt_spec.ts
Expand Up @@ -43,9 +43,9 @@ describe('Prompt Provider', () => {

registry.usePromptProvider(async definitions => {
return {
'\'bool\'': true,
'\'test\'': 'two',
'\'obj\'/\'deep\'/\'three\'': 'test3-answer',
'/bool': true,
'/test': 'two',
'/obj/deep/three': 'test3-answer',
};
});

Expand Down
57 changes: 39 additions & 18 deletions packages/angular_devkit/core/src/json/schema/registry.ts
Expand Up @@ -355,6 +355,35 @@ export class CoreSchemaRegistry implements SchemaRegistry {
updateData,
schemaInfo.smartDefaultRecord,
)),
switchMap(updatedData => {
if (validationOptions.withPrompts === false) {
return of(updatedData);
}

const visitor: JsonVisitor = (value, pointer) => {
if (value !== undefined) {
validationContext.promptFieldsWithValue.add(pointer);
}

return value;
};

return visitJson(updatedData, visitor, schema, this._resolver, validate);
}),
switchMap(updatedData => {
if (validationOptions.withPrompts === false) {
return of(updatedData);
}

const definitions = schemaInfo.promptDefinitions
.filter(def => !validationContext.promptFieldsWithValue.has(def.id));

if (this._promptProvider && definitions.length > 0) {
return from(this._applyPrompts(updatedData, definitions));
} else {
return of(updatedData);
}
}),
switchMap(updatedData => {
const result = validate.call(validationContext, updatedData);

Expand All @@ -372,22 +401,6 @@ export class CoreSchemaRegistry implements SchemaRegistry {
return Promise.reject(err);
}));
}),
switchMap(([data, valid]: [JsonValue, boolean]) => {
if (validationOptions.withPrompts === false) {
return of([data, valid]);
}

const definitions = schemaInfo.promptDefinitions
.filter(def => !validationContext.promptFieldsWithValue.has(def.id));

if (valid && this._promptProvider && definitions.length > 0) {
return from(this._applyPrompts(data, definitions)).pipe(
map(data => [data, valid]),
);
} else {
return of([data, valid]);
}
}),
switchMap(([data, valid]: [JsonValue, boolean]) => {
if (valid) {
let result = of(data);
Expand Down Expand Up @@ -508,7 +521,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {

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

let type: string | undefined;
let items: Array<string | { label: string, value: string | number | boolean }> | undefined;
Expand Down Expand Up @@ -609,9 +622,17 @@ export class CoreSchemaRegistry implements SchemaRegistry {
return from(provider(prompts)).pipe(
map(answers => {
for (const path in answers) {
const pathFragments = path.split('/').map(pf => {
if (/^\d+$/.test(pf)) {
return pf;
} else {
return '\'' + pf + '\'';
}
});

CoreSchemaRegistry._set(
data,
path.split('/'),
pathFragments.slice(1),
answers[path] as {},
null,
undefined,
Expand Down