diff --git a/src/plugins/es_ui_shared/static/forms/helpers/field_validators/empty_field.test.ts b/src/plugins/es_ui_shared/static/forms/helpers/field_validators/empty_field.test.ts new file mode 100644 index 0000000000000..e05f7c86b8a60 --- /dev/null +++ b/src/plugins/es_ui_shared/static/forms/helpers/field_validators/empty_field.test.ts @@ -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); + + 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 }); + }); +}); diff --git a/src/plugins/es_ui_shared/static/forms/helpers/field_validators/empty_field.ts b/src/plugins/es_ui_shared/static/forms/helpers/field_validators/empty_field.ts index 3b09e165984d4..9917b273d666c 100644 --- a/src/plugins/es_ui_shared/static/forms/helpers/field_validators/empty_field.ts +++ b/src/plugins/es_ui_shared/static/forms/helpers/field_validators/empty_field.ts @@ -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): ReturnType> => { 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)) { diff --git a/src/plugins/es_ui_shared/static/validators/string/is_empty.ts b/src/plugins/es_ui_shared/static/validators/string/is_empty.ts index f70cbd36213ed..197d707f5edbf 100644 --- a/src/plugins/es_ui_shared/static/validators/string/is_empty.ts +++ b/src/plugins/es_ui_shared/static/validators/string/is_empty.ts @@ -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) === ''; diff --git a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/gsub.tsx b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/gsub.tsx index 7e72848485c11..8e12be6880d00 100644 --- a/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/gsub.tsx +++ b/x-pack/plugins/ingest_pipelines/public/application/components/pipeline_editor/components/processor_form/processors/gsub.tsx @@ -37,7 +37,8 @@ const fieldsConfig: FieldsConfig = { validator: emptyField( i18n.translate('xpack.ingestPipelines.pipelineEditor.gsubForm.patternRequiredError', { defaultMessage: 'A value is required.', - }) + }), + false ), }, {