Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 11 additions & 11 deletions packages/angular_devkit/core/src/json/schema/registry.ts
Original file line number Diff line number Diff line change
Expand Up @@ -585,11 +585,11 @@ export class CoreSchemaRegistry implements SchemaRegistry {
for (const path in answers) {
const pathFragments = path.split('/').slice(1);

CoreSchemaRegistry._set(data, pathFragments, answers[path], null, undefined, true);
await CoreSchemaRegistry._set(data, pathFragments, answers[path], null, undefined, true);
}
}

private static _set(
private static async _set(
// eslint-disable-next-line @typescript-eslint/no-explicit-any
data: any,
fragments: string[],
Expand All @@ -598,7 +598,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
parent: any = null,
parentProperty?: string,
force?: boolean,
): void {
): Promise<void> {
for (let index = 0; index < fragments.length; index++) {
const fragment = fragments[index];
if (/^i\d+$/.test(fragment)) {
Expand All @@ -607,7 +607,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
}

for (let dataIndex = 0; dataIndex < data.length; dataIndex++) {
CoreSchemaRegistry._set(
await CoreSchemaRegistry._set(
data[dataIndex],
fragments.slice(index + 1),
value,
Expand All @@ -629,7 +629,12 @@ export class CoreSchemaRegistry implements SchemaRegistry {
}

if (parent && parentProperty && (force || parent[parentProperty] === undefined)) {
parent[parentProperty] = value;
let resolvedValue = typeof value === 'function' ? value() : value;
if (isObservable(resolvedValue)) {
resolvedValue = await lastValueFrom(resolvedValue);
}

parent[parentProperty] = resolvedValue;
}
}

Expand All @@ -644,12 +649,7 @@ export class CoreSchemaRegistry implements SchemaRegistry {
continue;
}

let value = source(schema);
if (isObservable(value)) {
value = (await lastValueFrom(value)) as {};
}

CoreSchemaRegistry._set(data, fragments, value);
await CoreSchemaRegistry._set(data, fragments, () => source(schema));
}
}

Expand Down
23 changes: 23 additions & 0 deletions packages/angular_devkit/core/src/json/schema/registry_spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -305,6 +305,29 @@ describe('CoreSchemaRegistry', () => {
expect(data.obj.deep.arr).toEqual([1, 2, 3]);
});

it('does not call smart default provider when value is already defined', async () => {
const registry = new CoreSchemaRegistry();
const provider = jasmine.createSpy('provider').and.returnValue('default-val');
registry.addSmartDefaultProvider('test', provider);

const validator = await registry.compile({
properties: {
prop: {
type: 'string',
$default: {
$source: 'test',
},
},
},
});

const data = { prop: 'explicit-val' };
const result = await validator(data);
expect(result.success).toBe(true);
expect(data.prop).toBe('explicit-val');
expect(provider).not.toHaveBeenCalled();
});

it('works with true as a schema and post-transforms', async () => {
const registry = new CoreSchemaRegistry();
registry.addPostTransform(addUndefinedDefaults);
Expand Down
Loading