forked from elastic/kibana
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Create a common Int Validator and use it in ingest_pipelines and Inde…
…x_lifecycle_management (elastic#196527) Closes [elastic#110417 ](elastic#110417) ## Summary In the Ingest Node Pipelines section, when the users created a new pipeline selecting de Community ID processor the users could set a non-integer number in this field. Then, they received a server side error when tried to create a pipeline. For fixing this, a validation must be added in the client. We didn't have a reusable validation for this case, but we did have a custom validation for integer values in the Index lifecycle management plugin. We also had the necessary translation in that plugin. So I went forward with: * I created a new integer validator in the `es_ui_shared` package as it is a fairly common validation and we could take advantage of it in the future. Also added a couple of unit test there for this validator. * I reused in the `ingest_pipelines` plugin the strings that already existed in `index_lifecycle_management`. * I added the new validation in the Community ID form in the `ingest_pipelines` plugin. Also added some test verifying that the processor doesn't create when the seeds validation fails. * Changed the method in the `index_lifecycle_management` validator so now it uses the reusable one. Now the Ingest pipeline forms shows the validation when the number is not an integer: ![Screenshot 2024-10-16 at 12 16 47](https://github.com/user-attachments/assets/1db9ad22-b144-44a5-9012-d3ebd5a19b6f) And the `index_lifecycle_management` still shows the validations as expected: <img width="756" alt="Screenshot 2024-10-16 at 11 49 53" src="https://github.com/user-attachments/assets/680886a5-355e-4637-9da9-4b93b396e751">
- Loading branch information
1 parent
855456b
commit 5a42e58
Showing
12 changed files
with
151 additions
and
28 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
src/plugins/es_ui_shared/static/forms/helpers/field_validators/is_integer.test.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { ValidationFuncArg } from '../../hook_form_lib'; | ||
import { isInteger } from './is_integer'; | ||
|
||
describe('isInteger', () => { | ||
const message = 'test error message'; | ||
const code = 'ERR_NOT_INT_NUMBER'; | ||
|
||
const validate = isInteger({ message }); | ||
const validator = (value: unknown) => validate({ value } as ValidationFuncArg<any, any>); | ||
|
||
test('should return undefined if value is integer number', () => { | ||
expect(validator(5)).toBeUndefined(); | ||
}); | ||
|
||
test('should return undefined if value string that can be parsed to integer', () => { | ||
expect(validator('5')).toBeUndefined(); | ||
}); | ||
|
||
test('should return Validation function if value is not integer number', () => { | ||
expect(validator(5.3)).toMatchObject({ message, code }); | ||
}); | ||
|
||
test('should return Validation function if value a string that can not be parsed to number but is not an integer', () => { | ||
expect(validator('5.3')).toMatchObject({ message, code }); | ||
}); | ||
|
||
test('should return Validation function if value a string that can not be parsed to number', () => { | ||
expect(validator('test')).toMatchObject({ message, code }); | ||
}); | ||
|
||
test('should return Validation function if value is boolean', () => { | ||
expect(validator(false)).toMatchObject({ message, code }); | ||
}); | ||
|
||
test('should return undefined if value is empty', () => { | ||
expect(validator('')).toBeUndefined(); | ||
}); | ||
}); |
27 changes: 27 additions & 0 deletions
27
src/plugins/es_ui_shared/static/forms/helpers/field_validators/is_integer.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the "Elastic License | ||
* 2.0", the "GNU Affero General Public License v3.0 only", and the "Server Side | ||
* Public License v 1"; you may not use this file except in compliance with, at | ||
* your election, the "Elastic License 2.0", the "GNU Affero General Public | ||
* License v3.0 only", or the "Server Side Public License, v 1". | ||
*/ | ||
|
||
import { ValidationFunc } from '../../hook_form_lib'; | ||
import { ERROR_CODE } from './types'; | ||
|
||
export const isInteger = | ||
({ message }: { message: string }) => | ||
(...args: Parameters<ValidationFunc>): ReturnType<ValidationFunc<any, ERROR_CODE>> => { | ||
const [{ value }] = args; | ||
|
||
if ( | ||
value === '' || | ||
(typeof value === 'number' && Number.isInteger(value)) || | ||
(typeof value === 'string' && Number.isInteger(Number(value))) | ||
) { | ||
return undefined; | ||
} | ||
|
||
return { message, code: 'ERR_NOT_INT_NUMBER' }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters