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

[Cases] Update custom field error toast message #175197

Merged
Show file tree
Hide file tree
Changes from 2 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
Original file line number Diff line number Diff line change
Expand Up @@ -1019,7 +1019,7 @@ describe('bulkCreate', () => {
casesClient
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to bulk create cases: Error: The following custom fields have the wrong type in the request: first_key,second_key"`
`"Failed to bulk create cases: Error: The following custom fields have the wrong type in the request: \\"missing field 1\\", \\"missing field 2\\""`
);
});

Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/client/cases/create.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -729,7 +729,7 @@ describe('create', () => {
casesClient
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to create case: Error: The following custom fields have the wrong type in the request: first_key,second_key"`
`"Failed to create case: Error: The following custom fields have the wrong type in the request: \\"missing field 1\\", \\"missing field 2\\""`
);
});
});
Expand Down
2 changes: 1 addition & 1 deletion x-pack/plugins/cases/server/client/cases/update.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1338,7 +1338,7 @@ describe('update', () => {
casesClient
)
).rejects.toThrowErrorMatchingInlineSnapshot(
`"Failed to update case, ids: [{\\"id\\":\\"mock-id-1\\",\\"version\\":\\"WzAsMV0=\\"}]: Error: The following custom fields have the wrong type in the request: first_key,second_key"`
`"Failed to update case, ids: [{\\"id\\":\\"mock-id-1\\",\\"version\\":\\"WzAsMV0=\\"}]: Error: The following custom fields have the wrong type in the request: \\"missing field 1\\", \\"foo\\""`
);
});
});
Expand Down
16 changes: 7 additions & 9 deletions x-pack/plugins/cases/server/client/cases/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ describe('validators', () => {
{
key: 'first_key',
type: CustomFieldTypes.TEXT,
label: 'foo',
label: 'first label',
required: false,
},
{
Expand All @@ -112,7 +112,7 @@ describe('validators', () => {
] as CustomFieldsConfiguration,
})
).toThrowErrorMatchingInlineSnapshot(
`"The following custom fields have the wrong type in the request: first_key"`
`"The following custom fields have the wrong type in the request: \\"first label\\""`
);
});

Expand Down Expand Up @@ -141,25 +141,25 @@ describe('validators', () => {
{
key: 'first_key',
type: CustomFieldTypes.TEXT,
label: 'foo',
label: 'first label',
required: false,
},
{
key: 'second_key',
type: CustomFieldTypes.TEXT,
label: 'foo',
label: 'second label',
required: false,
},
{
key: 'third_key',
type: CustomFieldTypes.TOGGLE,
label: 'foo',
label: 'third label',
required: false,
},
] as CustomFieldsConfiguration,
})
).toThrowErrorMatchingInlineSnapshot(
`"The following custom fields have the wrong type in the request: first_key,second_key,third_key"`
`"The following custom fields have the wrong type in the request: \\"first label\\", \\"second label\\", \\"third label\\""`
);
});

Expand Down Expand Up @@ -568,9 +568,7 @@ describe('validators', () => {
customFieldsConfiguration,
customFields: customFieldsMax,
})
).toThrowErrorMatchingInlineSnapshot(
`"Maximum ${MAX_CUSTOM_FIELDS_PER_CASE} customFields are allowed."`
);
).toThrowErrorMatchingInlineSnapshot(`"Maximum 10 customFields are allowed."`);
});
});
});
14 changes: 13 additions & 1 deletion x-pack/plugins/cases/server/client/cases/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,20 @@ export function validateCustomFieldTypesInRequest({
}

if (invalidCustomFieldKeys.length) {
const invalidCustomFieldLabels: string[] = [];

invalidCustomFieldKeys.forEach((invalidFieldKey) => {
const configuration = customFieldsConfiguration.find((item) => item.key === invalidFieldKey);

if (configuration !== undefined) {
invalidCustomFieldLabels.push(`"${configuration.label}"`);
adcoelho marked this conversation as resolved.
Show resolved Hide resolved
}
});

throw Boom.badRequest(
`The following custom fields have the wrong type in the request: ${invalidCustomFieldKeys}`
`The following custom fields have the wrong type in the request: ${invalidCustomFieldLabels.join(
', '
)}`
);
}
}
Expand Down
20 changes: 10 additions & 10 deletions x-pack/plugins/cases/server/client/configure/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ describe('client', () => {
customFields: [
{
key: 'wrong_type_key',
label: 'text',
label: 'text label',
type: CustomFieldTypes.TEXT,
required: false,
},
Expand All @@ -343,7 +343,7 @@ describe('client', () => {
casesClientInternal
)
).rejects.toThrow(
'Failed to get patch configure in route: Error: Invalid custom field types in request for the following keys: wrong_type_key'
'Failed to get patch configure in route: Error: Invalid custom field types in request for the following keys: "text label"'
);
});

Expand All @@ -356,7 +356,7 @@ describe('client', () => {
customFields: [
{
key: 'missing_default',
label: 'text',
label: 'text label',
type: CustomFieldTypes.TEXT,
required: true,
},
Expand All @@ -366,7 +366,7 @@ describe('client', () => {
casesClientInternal
)
).rejects.toThrow(
'Failed to get patch configure in route: Error: The following required custom fields are missing the default value: missing_default'
'Failed to get patch configure in route: Error: The following required custom fields are missing the default value: "text label"'
);
});

Expand All @@ -379,7 +379,7 @@ describe('client', () => {
customFields: [
{
key: 'extra_default',
label: 'text',
label: 'text label',
type: CustomFieldTypes.TEXT,
required: false,
defaultValue: 'foobar',
Expand All @@ -390,7 +390,7 @@ describe('client', () => {
casesClientInternal
)
).rejects.toThrow(
'Failed to get patch configure in route: Error: The following optional custom fields try to define a default value: extra_default'
'Failed to get patch configure in route: Error: The following optional custom fields try to define a default value: "text label"'
);
});
});
Expand Down Expand Up @@ -463,7 +463,7 @@ describe('client', () => {
customFields: [
{
key: 'missing_default',
label: 'text',
label: 'text label',
type: CustomFieldTypes.TEXT,
required: true,
},
Expand All @@ -473,7 +473,7 @@ describe('client', () => {
casesClientInternal
)
).rejects.toThrow(
'Failed to create case configuration: Error: The following required custom fields are missing the default value: missing_default'
'Failed to create case configuration: Error: The following required custom fields are missing the default value: "text label"'
);
});

Expand All @@ -485,7 +485,7 @@ describe('client', () => {
customFields: [
{
key: 'extra_default',
label: 'text',
label: 'text label',
type: CustomFieldTypes.TEXT,
required: false,
defaultValue: 'foobar',
Expand All @@ -496,7 +496,7 @@ describe('client', () => {
casesClientInternal
)
).rejects.toThrow(
'Failed to create case configuration: Error: The following optional custom fields try to define a default value: extra_default'
'Failed to create case configuration: Error: The following optional custom fields try to define a default value: "text label"'
);
});
});
Expand Down
64 changes: 34 additions & 30 deletions x-pack/plugins/cases/server/client/configure/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,33 +18,35 @@ describe('validators', () => {
expect(() =>
validateCustomFieldTypesInRequest({
requestCustomFields: [
{ key: '1', type: CustomFieldTypes.TOGGLE },
{ key: '2', type: CustomFieldTypes.TEXT },
{ key: '1', type: CustomFieldTypes.TOGGLE, label: 'label 1' },
{ key: '2', type: CustomFieldTypes.TEXT, label: 'label 2' },
],

originalCustomFields: [
{ key: '1', type: CustomFieldTypes.TEXT },
{ key: '2', type: CustomFieldTypes.TOGGLE },
],
})
).toThrowErrorMatchingInlineSnapshot(
`"Invalid custom field types in request for the following keys: 1,2"`
`"Invalid custom field types in request for the following keys: \\"label 1\\", \\"label 2\\""`
);
});

it('throws an error when not all custom field types are invalid', () => {
expect(() =>
validateCustomFieldTypesInRequest({
requestCustomFields: [
{ key: '1', type: CustomFieldTypes.TOGGLE },
{ key: '2', type: CustomFieldTypes.TOGGLE },
{ key: '1', type: CustomFieldTypes.TOGGLE, label: 'label 1' },
{ key: '2', type: CustomFieldTypes.TOGGLE, label: 'label 2' },
],

originalCustomFields: [
{ key: '1', type: CustomFieldTypes.TEXT },
{ key: '2', type: CustomFieldTypes.TOGGLE },
],
})
).toThrowErrorMatchingInlineSnapshot(
`"Invalid custom field types in request for the following keys: 1"`
`"Invalid custom field types in request for the following keys: \\"label 1\\""`
);
});

Expand All @@ -63,8 +65,8 @@ describe('validators', () => {
expect(() =>
validateCustomFieldTypesInRequest({
requestCustomFields: [
{ key: '1', type: CustomFieldTypes.TOGGLE },
{ key: '2', type: CustomFieldTypes.TEXT },
{ key: '1', type: CustomFieldTypes.TOGGLE, label: 'label 1' },
{ key: '2', type: CustomFieldTypes.TEXT, label: 'label 2' },
],
originalCustomFields: [],
})
Expand All @@ -77,8 +79,8 @@ describe('validators', () => {
expect(() =>
validateRequiredCustomFieldsInRequest({
requestCustomFields: [
{ key: '1', required: false },
{ key: '2', required: false },
{ key: '1', required: false, label: 'label 1' },
{ key: '2', required: false, label: 'label 2' },
],
})
).not.toThrow();
Expand All @@ -88,8 +90,8 @@ describe('validators', () => {
expect(() =>
validateRequiredCustomFieldsInRequest({
requestCustomFields: [
{ key: '1', required: true, defaultValue: false },
{ key: '2', required: true, defaultValue: 'foobar' },
{ key: '1', required: true, defaultValue: false, label: 'label 1' },
{ key: '2', required: true, defaultValue: 'foobar', label: 'label 2' },
],
})
).not.toThrow();
Expand All @@ -107,7 +109,7 @@ describe('validators', () => {
it('does not throw an error for other falsy defaultValues (empty string)', () => {
expect(() =>
validateRequiredCustomFieldsInRequest({
requestCustomFields: [{ key: '1', required: true, defaultValue: '' }],
requestCustomFields: [{ key: '1', required: true, defaultValue: '', label: 'label' }],
})
).not.toThrow();
});
Expand All @@ -116,13 +118,13 @@ describe('validators', () => {
expect(() =>
validateRequiredCustomFieldsInRequest({
requestCustomFields: [
{ key: '1', required: true, defaultValue: null },
{ key: '2', required: true },
{ key: '3', required: false },
{ key: '1', required: true, defaultValue: null, label: 'label 1' },
{ key: '2', required: true, label: 'label 2' },
{ key: '3', required: false, label: 'label 3' },
],
})
).toThrowErrorMatchingInlineSnapshot(
`"The following required custom fields are missing the default value: 1,2"`
`"The following required custom fields are missing the default value: \\"label 1\\", \\"label 2\\""`
);
});
});
Expand All @@ -132,8 +134,8 @@ describe('validators', () => {
expect(() =>
validateOptionalCustomFieldsInRequest({
requestCustomFields: [
{ key: '1', required: false },
{ key: '2', required: false },
{ key: '1', required: false, label: 'label 1' },
{ key: '2', required: false, label: 'label 2' },
],
})
).not.toThrow();
Expand All @@ -143,8 +145,8 @@ describe('validators', () => {
expect(() =>
validateOptionalCustomFieldsInRequest({
requestCustomFields: [
{ key: '1', required: true, defaultValue: false },
{ key: '2', required: true, defaultValue: 'foobar' },
{ key: '1', required: true, defaultValue: false, label: 'label 1' },
{ key: '2', required: true, defaultValue: 'foobar', label: 'label 2' },
],
})
).not.toThrow();
Expand All @@ -154,42 +156,44 @@ describe('validators', () => {
expect(() =>
validateOptionalCustomFieldsInRequest({
requestCustomFields: [
{ key: '1', required: false, defaultValue: false },
{ key: '2', required: false, defaultValue: 'foobar' },
{ key: '1', required: false, defaultValue: false, label: 'label 1' },
{ key: '2', required: false, defaultValue: 'foobar', label: 'label 2' },
],
})
).toThrowErrorMatchingInlineSnapshot(
`"The following optional custom fields try to define a default value: 1,2"`
`"The following optional custom fields try to define a default value: \\"label 1\\", \\"label 2\\""`
);
});

it('throws an error for other falsy defaultValues (null)', () => {
expect(() =>
validateOptionalCustomFieldsInRequest({
requestCustomFields: [{ key: '1', required: false, defaultValue: null }],
requestCustomFields: [
{ key: '1', required: false, defaultValue: null, label: 'label 1' },
],
})
).toThrowErrorMatchingInlineSnapshot(
`"The following optional custom fields try to define a default value: 1"`
`"The following optional custom fields try to define a default value: \\"label 1\\""`
);
});

it('throws an error for other falsy defaultValues (0)', () => {
expect(() =>
validateOptionalCustomFieldsInRequest({
requestCustomFields: [{ key: '1', required: false, defaultValue: 0 }],
requestCustomFields: [{ key: '1', required: false, defaultValue: 0, label: 'label 1' }],
})
).toThrowErrorMatchingInlineSnapshot(
`"The following optional custom fields try to define a default value: 1"`
`"The following optional custom fields try to define a default value: \\"label 1\\""`
);
});

it('throws an error for other falsy defaultValues (empty string)', () => {
expect(() =>
validateOptionalCustomFieldsInRequest({
requestCustomFields: [{ key: '1', required: false, defaultValue: '' }],
requestCustomFields: [{ key: '1', required: false, defaultValue: '', label: 'label 1' }],
})
).toThrowErrorMatchingInlineSnapshot(
`"The following optional custom fields try to define a default value: 1"`
`"The following optional custom fields try to define a default value: \\"label 1\\""`
);
});
});
Expand Down
Loading