Skip to content

Commit

Permalink
PR comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
adcoelho committed Jan 22, 2024
1 parent 50eb782 commit 22763ee
Show file tree
Hide file tree
Showing 8 changed files with 45 additions and 31 deletions.
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}"`);
}
});

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
10 changes: 5 additions & 5 deletions x-pack/plugins/cases/server/client/configure/client.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"'
);
});

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: 'text label'"
'Failed to get patch configure in route: Error: The following required custom fields are missing the default value: "text label"'
);
});

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: 'text label'"
'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 @@ -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"'
);
});

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: 'text label'"
'Failed to create case configuration: Error: The following optional custom fields try to define a default value: "text label"'
);
});
});
Expand Down
14 changes: 7 additions & 7 deletions x-pack/plugins/cases/server/client/configure/validators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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\\""`
);
});

Expand All @@ -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\\""`
);
});

Expand Down Expand Up @@ -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\\""`
);
});
});
Expand Down Expand Up @@ -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\\""`
);
});

Expand All @@ -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\\""`
);
});

Expand All @@ -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\\""`
);
});

Expand All @@ -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\\""`
);
});
});
Expand Down
16 changes: 10 additions & 6 deletions x-pack/plugins/cases/server/client/configure/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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(', ')}`
);
}
};
Expand Down Expand Up @@ -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(
', '
)}`
);
}
};
Expand All @@ -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(
', '
)}`
);
}
};

0 comments on commit 22763ee

Please sign in to comment.