From 22763ee519ab191cf42f488f32ae6f2084b3c443 Mon Sep 17 00:00:00 2001 From: adcoelho Date: Mon, 22 Jan 2024 13:42:31 +0100 Subject: [PATCH] PR comments. --- .../server/client/cases/bulk_create.test.ts | 2 +- .../cases/server/client/cases/create.test.ts | 2 +- .../cases/server/client/cases/update.test.ts | 2 +- .../cases/server/client/cases/validators.test.ts | 16 +++++++--------- .../cases/server/client/cases/validators.ts | 14 +++++++++++++- .../cases/server/client/configure/client.test.ts | 10 +++++----- .../server/client/configure/validators.test.ts | 14 +++++++------- .../cases/server/client/configure/validators.ts | 16 ++++++++++------ 8 files changed, 45 insertions(+), 31 deletions(-) diff --git a/x-pack/plugins/cases/server/client/cases/bulk_create.test.ts b/x-pack/plugins/cases/server/client/cases/bulk_create.test.ts index fa0b4e3f584e5..920666ad1d8bf 100644 --- a/x-pack/plugins/cases/server/client/cases/bulk_create.test.ts +++ b/x-pack/plugins/cases/server/client/cases/bulk_create.test.ts @@ -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\\""` ); }); diff --git a/x-pack/plugins/cases/server/client/cases/create.test.ts b/x-pack/plugins/cases/server/client/cases/create.test.ts index b65061d403fec..f7fe4feb4af2a 100644 --- a/x-pack/plugins/cases/server/client/cases/create.test.ts +++ b/x-pack/plugins/cases/server/client/cases/create.test.ts @@ -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\\""` ); }); }); diff --git a/x-pack/plugins/cases/server/client/cases/update.test.ts b/x-pack/plugins/cases/server/client/cases/update.test.ts index 29bce4636c3f3..3c0540e73b3ed 100644 --- a/x-pack/plugins/cases/server/client/cases/update.test.ts +++ b/x-pack/plugins/cases/server/client/cases/update.test.ts @@ -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\\""` ); }); }); diff --git a/x-pack/plugins/cases/server/client/cases/validators.test.ts b/x-pack/plugins/cases/server/client/cases/validators.test.ts index abc4c27385b02..cb60e3d183025 100644 --- a/x-pack/plugins/cases/server/client/cases/validators.test.ts +++ b/x-pack/plugins/cases/server/client/cases/validators.test.ts @@ -100,7 +100,7 @@ describe('validators', () => { { key: 'first_key', type: CustomFieldTypes.TEXT, - label: 'foo', + label: 'first label', required: false, }, { @@ -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\\""` ); }); @@ -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\\""` ); }); @@ -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."`); }); }); }); diff --git a/x-pack/plugins/cases/server/client/cases/validators.ts b/x-pack/plugins/cases/server/client/cases/validators.ts index 0d76875be0114..3827185e028e1 100644 --- a/x-pack/plugins/cases/server/client/cases/validators.ts +++ b/x-pack/plugins/cases/server/client/cases/validators.ts @@ -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}"`); + } + }); + 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( + ', ' + )}` ); } } diff --git a/x-pack/plugins/cases/server/client/configure/client.test.ts b/x-pack/plugins/cases/server/client/configure/client.test.ts index 20c602dbddb87..d1420b579c687 100644 --- a/x-pack/plugins/cases/server/client/configure/client.test.ts +++ b/x-pack/plugins/cases/server/client/configure/client.test.ts @@ -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: 'text label'" + 'Failed to get patch configure in route: Error: Invalid custom field types in request for the following keys: "text label"' ); }); @@ -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: 'text label'" + 'Failed to get patch configure in route: Error: The following required custom fields are missing the default value: "text label"' ); }); @@ -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: 'text label'" + 'Failed to get patch configure in route: Error: The following optional custom fields try to define a default value: "text label"' ); }); }); @@ -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: 'text label'" + 'Failed to create case configuration: Error: The following required custom fields are missing the default value: "text label"' ); }); @@ -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: 'text label'" + 'Failed to create case configuration: Error: The following optional custom fields try to define a default value: "text label"' ); }); }); diff --git a/x-pack/plugins/cases/server/client/configure/validators.test.ts b/x-pack/plugins/cases/server/client/configure/validators.test.ts index 6e82a74689b97..dc3c1a65687bd 100644 --- a/x-pack/plugins/cases/server/client/configure/validators.test.ts +++ b/x-pack/plugins/cases/server/client/configure/validators.test.ts @@ -28,7 +28,7 @@ describe('validators', () => { ], }) ).toThrowErrorMatchingInlineSnapshot( - `"Invalid custom field types in request for the following keys: 'label 1','label 2'"` + `"Invalid custom field types in request for the following keys: \\"label 1\\", \\"label 2\\""` ); }); @@ -46,7 +46,7 @@ describe('validators', () => { ], }) ).toThrowErrorMatchingInlineSnapshot( - `"Invalid custom field types in request for the following keys: 'label 1'"` + `"Invalid custom field types in request for the following keys: \\"label 1\\""` ); }); @@ -124,7 +124,7 @@ describe('validators', () => { ], }) ).toThrowErrorMatchingInlineSnapshot( - `"The following required custom fields are missing the default value: 'label 1','label 2'"` + `"The following required custom fields are missing the default value: \\"label 1\\", \\"label 2\\""` ); }); }); @@ -161,7 +161,7 @@ describe('validators', () => { ], }) ).toThrowErrorMatchingInlineSnapshot( - `"The following optional custom fields try to define a default value: 'label 1','label 2'"` + `"The following optional custom fields try to define a default value: \\"label 1\\", \\"label 2\\""` ); }); @@ -173,7 +173,7 @@ describe('validators', () => { ], }) ).toThrowErrorMatchingInlineSnapshot( - `"The following optional custom fields try to define a default value: 'label 1'"` + `"The following optional custom fields try to define a default value: \\"label 1\\""` ); }); @@ -183,7 +183,7 @@ describe('validators', () => { requestCustomFields: [{ key: '1', required: false, defaultValue: 0, label: 'label 1' }], }) ).toThrowErrorMatchingInlineSnapshot( - `"The following optional custom fields try to define a default value: 'label 1'"` + `"The following optional custom fields try to define a default value: \\"label 1\\""` ); }); @@ -193,7 +193,7 @@ describe('validators', () => { requestCustomFields: [{ key: '1', required: false, defaultValue: '', label: 'label 1' }], }) ).toThrowErrorMatchingInlineSnapshot( - `"The following optional custom fields try to define a default value: 'label 1'"` + `"The following optional custom fields try to define a default value: \\"label 1\\""` ); }); }); diff --git a/x-pack/plugins/cases/server/client/configure/validators.ts b/x-pack/plugins/cases/server/client/configure/validators.ts index 119287f3c7b87..5eb5d9acbc93c 100644 --- a/x-pack/plugins/cases/server/client/configure/validators.ts +++ b/x-pack/plugins/cases/server/client/configure/validators.ts @@ -28,13 +28,13 @@ export const validateCustomFieldTypesInRequest = ({ const originalField = originalCustomFields.find((item) => item.key === requestField.key); if (originalField && originalField.type !== requestField.type) { - invalidFields.push(`'${requestField.label}'`); + invalidFields.push(`"${requestField.label}"`); } }); if (invalidFields.length > 0) { throw Boom.badRequest( - `Invalid custom field types in request for the following keys: ${invalidFields}` + `Invalid custom field types in request for the following keys: ${invalidFields.join(', ')}` ); } }; @@ -63,13 +63,15 @@ export const validateRequiredCustomFieldsInRequest = ({ requestField.required && (requestField.defaultValue === undefined || requestField.defaultValue === null) ) { - invalidFields.push(`'${requestField.label}'`); + invalidFields.push(`"${requestField.label}"`); } }); if (invalidFields.length > 0) { throw Boom.badRequest( - `The following required custom fields are missing the default value: ${invalidFields}` + `The following required custom fields are missing the default value: ${invalidFields.join( + ', ' + )}` ); } }; @@ -95,13 +97,15 @@ export const validateOptionalCustomFieldsInRequest = ({ requestCustomFields.forEach((requestField) => { if (!requestField.required && requestField.defaultValue !== undefined) { - invalidFields.push(`'${requestField.label}'`); + invalidFields.push(`"${requestField.label}"`); } }); if (invalidFields.length > 0) { throw Boom.badRequest( - `The following optional custom fields try to define a default value: ${invalidFields}` + `The following optional custom fields try to define a default value: ${invalidFields.join( + ', ' + )}` ); } };