Fix the scaffolder validator for arrays when the item is a field in the object

Signed-off-by: Bogdan Nechyporenko <bnechyporenko@bol.com>
This commit is contained in:
Bogdan Nechyporenko
2023-03-02 15:37:43 +01:00
parent 0aae459629
commit 6ca13414f2
2 changed files with 55 additions and 0 deletions
@@ -406,4 +406,39 @@ describe('createAsyncValidators', () => {
expect(validators.TagField).not.toHaveBeenCalled();
});
it('should call validator for array object property from a custom field extension', async () => {
const schema: JsonObject = {
type: 'object',
properties: {
links: {
title: 'Links',
type: 'array',
items: {
type: 'object',
required: ['url', 'title', 'icon'],
properties: {
url: {
title: 'url',
description: 'url',
type: 'object',
'ui:field': 'CustomLinkField',
},
},
},
},
},
};
const validators = { CustomLinkField: jest.fn() };
const validate = createAsyncValidators(schema, validators, {
apiHolder: { get: jest.fn() },
});
await validate({
links: [{ url: 'http://my-url.spotify.com' }],
});
expect(validators.CustomLinkField).toHaveBeenCalled();
});
});
@@ -103,6 +103,26 @@ export const createAsyncValidators = (
itemsUiSchema,
);
}
} else if (
definitionInSchema &&
definitionInSchema.items &&
definitionInSchema.items.type === 'object'
) {
const properties = (definitionInSchema.items?.properties ??
[]) as JsonObject[];
for (const [, propValue] of Object.entries(properties)) {
if ('ui:field' in propValue) {
const { schema: itemsSchema, uiSchema: itemsUiSchema } =
extractSchemaFromStep(definitionInSchema.items);
await validateForm(
propValue['ui:field'] as string,
key,
value,
itemsSchema,
itemsUiSchema,
);
}
}
} else if (isObject(value)) {
formValidation[key] = await validate(formData, path, value);
}