Skip to content

Commit

Permalink
[8.x] Allow empty spaces un Gsub processor (#197815) (#199293)
Browse files Browse the repository at this point in the history
# Backport

This will backport the following commits from `main` to `8.x`:
- [Allow empty spaces un Gsub processor
(#197815)](#197815)

<!--- Backport version: 9.4.3 -->

### Questions ?
Please refer to the [Backport tool
documentation](https://github.com/sqren/backport)

<!--BACKPORT [{"author":{"name":"Sonia Sanz
Vivas","email":"[email protected]"},"sourceCommit":{"committedDate":"2024-11-07T11:09:06Z","message":"Allow
empty spaces un Gsub processor (#197815)\n\nCloses
[#191920](https://github.com/elastic/kibana/issues/191920)\r\n\r\n##
Summary\r\nIn the pattern field of the GSUB processor, we were
validating that the\r\nfield is not empty. But for this case we must
allow spaces as values. To\r\nfix this, I've added a second parameter
`trimString` to the `emptyField`\r\nvalidator. If the parameter is set
to true by default so the behavior of\r\nthe validator doesn't change in
all the fields that already use it. But\r\nwhen the parameter is set to
false, as in this case, the entry value\r\nstring is not trimmed, so it
only validate to false if the field is\r\nactually empty. I also added
some unit tests to make sure that this new\r\ncondition doesn't break
the
validator.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/049f7424-00c6-4bb2-ab89-2ce158ac4c4e\r\n\r\n\r\n\r\n####
Notes\r\nIn the video it can be seen that a banner flashes when saving.
It is a\r\nbug that already happened before this one. I've opened
[an\r\nissue](#197810) to
address
it.","sha":"2f7f8e34db1fe38cc3a587a753533992398df691","branchLabelMapping":{"^v9.0.0$":"main","^v8.17.0$":"8.x","^v(\\d+).(\\d+).\\d+$":"$1.$2"}},"sourcePullRequest":{"labels":["Team:Kibana
Management","release_note:skip","v9.0.0","Feature:Ingest Node
Pipelines","backport:prev-major","v8.17.0"],"title":"Allow empty spaces
un Gsub
processor","number":197815,"url":"https://github.com/elastic/kibana/pull/197815","mergeCommit":{"message":"Allow
empty spaces un Gsub processor (#197815)\n\nCloses
[#191920](https://github.com/elastic/kibana/issues/191920)\r\n\r\n##
Summary\r\nIn the pattern field of the GSUB processor, we were
validating that the\r\nfield is not empty. But for this case we must
allow spaces as values. To\r\nfix this, I've added a second parameter
`trimString` to the `emptyField`\r\nvalidator. If the parameter is set
to true by default so the behavior of\r\nthe validator doesn't change in
all the fields that already use it. But\r\nwhen the parameter is set to
false, as in this case, the entry value\r\nstring is not trimmed, so it
only validate to false if the field is\r\nactually empty. I also added
some unit tests to make sure that this new\r\ncondition doesn't break
the
validator.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/049f7424-00c6-4bb2-ab89-2ce158ac4c4e\r\n\r\n\r\n\r\n####
Notes\r\nIn the video it can be seen that a banner flashes when saving.
It is a\r\nbug that already happened before this one. I've opened
[an\r\nissue](#197810) to
address
it.","sha":"2f7f8e34db1fe38cc3a587a753533992398df691"}},"sourceBranch":"main","suggestedTargetBranches":["8.x"],"targetPullRequestStates":[{"branch":"main","label":"v9.0.0","branchLabelMappingKey":"^v9.0.0$","isSourceBranch":true,"state":"MERGED","url":"https://github.com/elastic/kibana/pull/197815","number":197815,"mergeCommit":{"message":"Allow
empty spaces un Gsub processor (#197815)\n\nCloses
[#191920](https://github.com/elastic/kibana/issues/191920)\r\n\r\n##
Summary\r\nIn the pattern field of the GSUB processor, we were
validating that the\r\nfield is not empty. But for this case we must
allow spaces as values. To\r\nfix this, I've added a second parameter
`trimString` to the `emptyField`\r\nvalidator. If the parameter is set
to true by default so the behavior of\r\nthe validator doesn't change in
all the fields that already use it. But\r\nwhen the parameter is set to
false, as in this case, the entry value\r\nstring is not trimmed, so it
only validate to false if the field is\r\nactually empty. I also added
some unit tests to make sure that this new\r\ncondition doesn't break
the
validator.\r\n\r\n\r\nhttps://github.com/user-attachments/assets/049f7424-00c6-4bb2-ab89-2ce158ac4c4e\r\n\r\n\r\n\r\n####
Notes\r\nIn the video it can be seen that a banner flashes when saving.
It is a\r\nbug that already happened before this one. I've opened
[an\r\nissue](#197810) to
address
it.","sha":"2f7f8e34db1fe38cc3a587a753533992398df691"}},{"branch":"8.x","label":"v8.17.0","branchLabelMappingKey":"^v8.17.0$","isSourceBranch":false,"state":"NOT_CREATED"}]}]
BACKPORT-->

Co-authored-by: Sonia Sanz Vivas <[email protected]>
  • Loading branch information
kibanamachine and SoniaSanzV authored Nov 7, 2024
1 parent ed448dd commit 37e030a
Show file tree
Hide file tree
Showing 4 changed files with 56 additions and 4 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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 { emptyField } from './empty_field';

describe('emptyField', () => {
const message = 'test error message';
const code = 'ERR_FIELD_MISSING';
const path = 'path';

const validator = (value: string | any[], trimString?: boolean) =>
emptyField(message, trimString)({ value, path } as ValidationFuncArg<any, any>);

test('should return Validation function if value is an empty string and trimString is true', () => {
expect(validator('')).toMatchObject({ message, code, path });
});

test('should return Validation function if value is an empty string and trimString is false', () => {
expect(validator('', false)).toMatchObject({ message, code, path });
});

test('should return Validation function if value is a space and trimString is true', () => {
expect(validator(' ')).toMatchObject({ message, code, path });
});

test('should return undefined if value is a space and trimString is false', () => {
expect(validator(' ', false)).toBeUndefined();
});

test('should return undefined if value is a string and is not empty', () => {
expect(validator('not Empty')).toBeUndefined();
});

test('should return undefined if value an array and is not empty', () => {
expect(validator(['not Empty'])).toBeUndefined();
});

test('should return undefined if value an array and is empty', () => {
expect(validator([])).toMatchObject({ message, code, path });
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,14 @@ import { isEmptyArray } from '../../../validators/array';
import { ERROR_CODE } from './types';

export const emptyField =
(message: string) =>
(message: string, trimString: boolean = true) =>
(...args: Parameters<ValidationFunc>): ReturnType<ValidationFunc<any, ERROR_CODE>> => {
const [{ value, path }] = args;

if (typeof value === 'string') {
return isEmptyString(value) ? { code: 'ERR_FIELD_MISSING', path, message } : undefined;
return isEmptyString(value, trimString)
? { code: 'ERR_FIELD_MISSING', path, message }
: undefined;
}

if (Array.isArray(value)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
* License v3.0 only", or the "Server Side Public License, v 1".
*/

export const isEmptyString = (value: string) => value.trim() === '';
export const isEmptyString = (value: string, trimString: boolean = true) =>
(trimString ? value.trim() : value) === '';
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ const fieldsConfig: FieldsConfig = {
validator: emptyField(
i18n.translate('xpack.ingestPipelines.pipelineEditor.gsubForm.patternRequiredError', {
defaultMessage: 'A value is required.',
})
}),
false
),
},
{
Expand Down

0 comments on commit 37e030a

Please sign in to comment.