Skip to content

Commit

Permalink
Change the custom field configuration error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho committed Jan 22, 2024
1 parent 3c87fce commit 50eb782
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 44 deletions.
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
10 changes: 6 additions & 4 deletions x-pack/plugins/cases/server/client/configure/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ export const validateCustomFieldTypesInRequest = ({
requestCustomFields,
originalCustomFields,
}: {
requestCustomFields?: Array<{ key: string; type: CustomFieldTypes }>;
requestCustomFields?: Array<{ key: string; type: CustomFieldTypes; label: string }>;
originalCustomFields: Array<{ key: string; type: CustomFieldTypes }>;
}) => {
if (!Array.isArray(requestCustomFields) || !originalCustomFields.length) {
Expand All @@ -28,7 +28,7 @@ export const validateCustomFieldTypesInRequest = ({
const originalField = originalCustomFields.find((item) => item.key === requestField.key);

if (originalField && originalField.type !== requestField.type) {
invalidFields.push(requestField.key);
invalidFields.push(`'${requestField.label}'`);
}
});

Expand All @@ -49,6 +49,7 @@ export const validateRequiredCustomFieldsInRequest = ({
key: string;
required: boolean;
defaultValue?: string | boolean | null;
label: string;
}>;
}) => {
if (!Array.isArray(requestCustomFields)) {
Expand All @@ -62,7 +63,7 @@ export const validateRequiredCustomFieldsInRequest = ({
requestField.required &&
(requestField.defaultValue === undefined || requestField.defaultValue === null)
) {
invalidFields.push(requestField.key);
invalidFields.push(`'${requestField.label}'`);
}
});

Expand All @@ -83,6 +84,7 @@ export const validateOptionalCustomFieldsInRequest = ({
key: string;
required: boolean;
defaultValue?: unknown;
label: string;
}>;
}) => {
if (!Array.isArray(requestCustomFields)) {
Expand All @@ -93,7 +95,7 @@ export const validateOptionalCustomFieldsInRequest = ({

requestCustomFields.forEach((requestField) => {
if (!requestField.required && requestField.defaultValue !== undefined) {
invalidFields.push(requestField.key);
invalidFields.push(`'${requestField.label}'`);
}
});

Expand Down

0 comments on commit 50eb782

Please sign in to comment.