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.
[Unified Search] Show error message for invalid date filter value (el…
…astic#131290) * feat: added show error message for invalid date * refact: move logic in HOC * feat: refactoring code and added translation * refact show error * refact: show error message * refact: remove translation * refactor: changed menu for show FilterEdit * fix: open/close popover * feat: field.type => KBN_FIELD_TYPES * feat: remove extra code with with input check and refactored filter item * feat: added tests and refactoring code * refact: getFieldValidityAndErrorMessage * feat: return isInvalid checking in valur input type for string, ip
- Loading branch information
Showing
5 changed files
with
122 additions
and
21 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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
/* | ||
* 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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import { getFieldValidityAndErrorMessage } from './helpers'; | ||
import { IFieldType } from '@kbn/data-views-plugin/common'; | ||
|
||
const mockField = { | ||
type: 'date', | ||
} as IFieldType; | ||
|
||
describe('Check field validity and error message', () => { | ||
it('should return a message that the entered date is not incorrect', () => { | ||
const output = getFieldValidityAndErrorMessage(mockField, Date()); | ||
|
||
expect(output).toEqual({ | ||
isInvalid: false, | ||
}); | ||
}); | ||
|
||
it('should show error', () => { | ||
const output = getFieldValidityAndErrorMessage(mockField, 'Date'); | ||
|
||
expect(output).toEqual({ | ||
isInvalid: true, | ||
errorMessage: 'Invalid date format provided', | ||
}); | ||
}); | ||
}); |
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 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 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import type { IFieldType } from '@kbn/data-views-plugin/common'; | ||
import { i18n } from '@kbn/i18n'; | ||
import { KBN_FIELD_TYPES } from '@kbn/data-plugin/public'; | ||
import { isEmpty } from 'lodash'; | ||
import { validateParams } from '../filter_bar/filter_editor/lib/filter_editor_utils'; | ||
|
||
export const getFieldValidityAndErrorMessage = ( | ||
field: IFieldType, | ||
value?: string | undefined | ||
): { isInvalid: boolean; errorMessage?: string } => { | ||
const type = field.type; | ||
switch (type) { | ||
case KBN_FIELD_TYPES.DATE: | ||
case KBN_FIELD_TYPES.DATE_RANGE: | ||
if (!isEmpty(value) && !validateParams(value, field)) { | ||
return invalidFormatError(); | ||
} | ||
break; | ||
default: | ||
break; | ||
} | ||
return noError(); | ||
}; | ||
|
||
const noError = (): { isInvalid: boolean } => { | ||
return { isInvalid: false }; | ||
}; | ||
|
||
const invalidFormatError = (): { isInvalid: boolean; errorMessage?: string } => { | ||
return { | ||
isInvalid: true, | ||
errorMessage: i18n.translate( | ||
'unifiedSearch.filter.filterBar.invalidDateFormatProvidedErrorMessage', | ||
{ | ||
defaultMessage: 'Invalid date format provided', | ||
} | ||
), | ||
}; | ||
}; |