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.
[7.x] [Mappings Editor] Support unknown types (elastic#62149) (elasti…
…c#62491) * First iteration of supporting unknown types e2e * Add missing files * Fix types issues * When creating a new field, we check if we actually know the type If we do know the type, convert the new field to it and throw away the customTypeJson. * Fix i18n * Updated naming to be more consistent customType -> otherType * Clean up of custom type in comments and validation feedback * Codre review suggestions * Add missing serializer * Add Array validator to json * Fix types issues Do not use otherTypeName in call to getConfig rather wrap it in it's own component also add some comments. * Remove otherTypeJson from parameters * Move fieldConfig to variable outside of the UseField * Copy update Change the instruction from "Manually specify" to something more declarative. Also, manually may sound misleading (suggests there is an automatic alternative). Also change the JSON parameter label to something more accurate. Co-authored-by: Elastic Machine <elasticmachine@users.noreply.github.com> Co-authored-by: Sébastien Loix <sabee77@gmail.com> # Conflicts: # x-pack/plugins/index_management/public/application/components/mappings_editor/components/document_fields/fields/fields_list_item.tsx
- Loading branch information
1 parent
58bf070
commit 764ef93
Showing
16 changed files
with
258 additions
and
58 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
92 changes: 92 additions & 0 deletions
92
...mappings_editor/components/document_fields/field_parameters/other_type_json_parameter.tsx
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,92 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
import { i18n } from '@kbn/i18n'; | ||
|
||
import { | ||
UseField, | ||
JsonEditorField, | ||
ValidationFuncArg, | ||
fieldValidators, | ||
FieldConfig, | ||
} from '../../../shared_imports'; | ||
|
||
const { isJsonField } = fieldValidators; | ||
|
||
/** | ||
* This is a special component that does not have an explicit entry in {@link PARAMETERS_DEFINITION}. | ||
* | ||
* We use it to store custom defined parameters in a field called "otherTypeJson". | ||
*/ | ||
|
||
const fieldConfig: FieldConfig = { | ||
label: i18n.translate('xpack.idxMgmt.mappingsEditor.otherTypeJsonFieldLabel', { | ||
defaultMessage: 'Type Parameters JSON', | ||
}), | ||
defaultValue: {}, | ||
validations: [ | ||
{ | ||
validator: isJsonField( | ||
i18n.translate( | ||
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeJsonInvalidJSONErrorMessage', | ||
{ | ||
defaultMessage: 'Invalid JSON.', | ||
} | ||
) | ||
), | ||
}, | ||
{ | ||
validator: ({ value }: ValidationFuncArg<any, any>) => { | ||
const json = JSON.parse(value); | ||
if (Array.isArray(json)) { | ||
return { | ||
message: i18n.translate( | ||
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeJsonArrayNotAllowedErrorMessage', | ||
{ | ||
defaultMessage: 'Arrays are not allowed.', | ||
} | ||
), | ||
}; | ||
} | ||
}, | ||
}, | ||
{ | ||
validator: ({ value }: ValidationFuncArg<any, any>) => { | ||
const json = JSON.parse(value); | ||
if (json.type) { | ||
return { | ||
code: 'ERR_CUSTOM_TYPE_OVERRIDDEN', | ||
message: i18n.translate( | ||
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeJsonTypeFieldErrorMessage', | ||
{ | ||
defaultMessage: 'Cannot override the "type" field.', | ||
} | ||
), | ||
}; | ||
} | ||
}, | ||
}, | ||
], | ||
deserializer: (value: any) => { | ||
if (value === '') { | ||
return value; | ||
} | ||
return JSON.stringify(value, null, 2); | ||
}, | ||
serializer: (value: string) => { | ||
try { | ||
return JSON.parse(value); | ||
} catch (error) { | ||
// swallow error and return non-parsed value; | ||
return value; | ||
} | ||
}, | ||
}; | ||
|
||
export const OtherTypeJsonParameter = () => ( | ||
<UseField path="otherTypeJson" config={fieldConfig} component={JsonEditorField} /> | ||
); |
42 changes: 42 additions & 0 deletions
42
...mappings_editor/components/document_fields/field_parameters/other_type_name_parameter.tsx
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,42 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
|
||
import React from 'react'; | ||
|
||
import { i18n } from '@kbn/i18n'; | ||
import { UseField, TextField, FieldConfig } from '../../../shared_imports'; | ||
import { fieldValidators } from '../../../shared_imports'; | ||
|
||
const { emptyField } = fieldValidators; | ||
|
||
/** | ||
* This is a special component that does not have an explicit entry in {@link PARAMETERS_DEFINITION}. | ||
* | ||
* We use it to store the name of types unknown to the mappings editor in the "subType" path. | ||
*/ | ||
|
||
const fieldConfig: FieldConfig = { | ||
label: i18n.translate('xpack.idxMgmt.mappingsEditor.otherTypeNameFieldLabel', { | ||
defaultMessage: 'Type Name', | ||
}), | ||
defaultValue: '', | ||
validations: [ | ||
{ | ||
validator: emptyField( | ||
i18n.translate( | ||
'xpack.idxMgmt.mappingsEditor.parameters.validations.otherTypeNameIsRequiredErrorMessage', | ||
{ | ||
defaultMessage: 'The type name is required.', | ||
} | ||
) | ||
), | ||
}, | ||
], | ||
}; | ||
|
||
export const OtherTypeNameParameter = () => ( | ||
<UseField path="subType" config={fieldConfig} component={TextField} /> | ||
); |
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
17 changes: 17 additions & 0 deletions
17
...n/components/mappings_editor/components/document_fields/fields/field_types/other_type.tsx
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,17 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License; | ||
* you may not use this file except in compliance with the Elastic License. | ||
*/ | ||
import React from 'react'; | ||
|
||
import { OtherTypeJsonParameter } from '../../field_parameters'; | ||
import { BasicParametersSection } from '../edit_field'; | ||
|
||
export const OtherType = () => { | ||
return ( | ||
<BasicParametersSection> | ||
<OtherTypeJsonParameter /> | ||
</BasicParametersSection> | ||
); | ||
}; |
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
Oops, something went wrong.