mirror of
https://github.com/elastic/kibana.git
synced 2025-04-24 01:38:56 -04:00
[EEM] add additional validations for entity definition IDs (#187555)
add additional validations for entity definition IDs
This commit is contained in:
parent
05c7a19ea6
commit
fac236e567
3 changed files with 34 additions and 9 deletions
|
@ -5,9 +5,9 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
export class EntityDefinitionIdTooLong extends Error {
|
||||
export class EntityDefinitionIdInvalid extends Error {
|
||||
constructor(message: string) {
|
||||
super(message);
|
||||
this.name = 'EntityDefinitionIdTooLong';
|
||||
this.name = 'EntityDefinitionIdInvalid';
|
||||
}
|
||||
}
|
|
@ -5,7 +5,7 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
import { EntityDefinitionIdTooLong } from '../errors/entity_definition_id_too_long_error';
|
||||
import { EntityDefinitionIdInvalid } from '../errors/entity_definition_id_invalid';
|
||||
import { entityDefinition } from '../helpers/fixtures/entity_definition';
|
||||
import { validateDefinitionCanCreateValidTransformIds } from './validate_transform_ids';
|
||||
|
||||
|
@ -20,7 +20,25 @@ describe('validateDefinitionCanCreateValidTransformIds(definition)', () => {
|
|||
'a-really-really-really-really-really-really-really-really-really-really-long-id';
|
||||
|
||||
expect(() => {
|
||||
validateDefinitionCanCreateValidTransformIds(entityDefinition);
|
||||
}).toThrow(EntityDefinitionIdTooLong);
|
||||
validateDefinitionCanCreateValidTransformIds(entityDefinitionWithLongID);
|
||||
}).toThrow(EntityDefinitionIdInvalid);
|
||||
});
|
||||
|
||||
it('should throw an error for a definition ID which contains invalid characters', () => {
|
||||
const entityDefinitionWithDots = entityDefinition;
|
||||
entityDefinitionWithDots.id = 'dots.are.not.allowed';
|
||||
|
||||
expect(() => {
|
||||
validateDefinitionCanCreateValidTransformIds(entityDefinitionWithDots);
|
||||
}).toThrow(EntityDefinitionIdInvalid);
|
||||
});
|
||||
|
||||
it('should throw an error for a definition ID which ends with dash or underscore', () => {
|
||||
const entityDefinitionEndingInUnderscore = entityDefinition;
|
||||
entityDefinitionEndingInUnderscore.id = 'looking-good-but_';
|
||||
|
||||
expect(() => {
|
||||
validateDefinitionCanCreateValidTransformIds(entityDefinitionEndingInUnderscore);
|
||||
}).toThrow(EntityDefinitionIdInvalid);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -5,13 +5,13 @@
|
|||
* 2.0.
|
||||
*/
|
||||
|
||||
const TRANSFORM_ID_MAX_LENGTH = 64;
|
||||
|
||||
import { EntityDefinition } from '@kbn/entities-schema';
|
||||
import { EntityDefinitionIdTooLong } from '../errors/entity_definition_id_too_long_error';
|
||||
import { EntityDefinitionIdInvalid } from '../errors/entity_definition_id_invalid';
|
||||
import { generateHistoryTransformId } from './generate_history_transform_id';
|
||||
import { generateLatestTransformId } from './generate_latest_transform_id';
|
||||
|
||||
const TRANSFORM_ID_MAX_LENGTH = 64;
|
||||
|
||||
export function validateDefinitionCanCreateValidTransformIds(definition: EntityDefinition) {
|
||||
const historyTransformId = generateHistoryTransformId(definition);
|
||||
const latestTransformId = generateLatestTransformId(definition);
|
||||
|
@ -20,10 +20,17 @@ export function validateDefinitionCanCreateValidTransformIds(definition: EntityD
|
|||
TRANSFORM_ID_MAX_LENGTH - Math.max(historyTransformId.length, latestTransformId.length);
|
||||
|
||||
if (spareChars < 0) {
|
||||
throw new EntityDefinitionIdTooLong(
|
||||
throw new EntityDefinitionIdInvalid(
|
||||
`Entity definition ID is too long (max = ${
|
||||
definition.id.length + spareChars
|
||||
}); the resulting transform ID will be invalid`
|
||||
);
|
||||
}
|
||||
|
||||
const transformIdRegex = /^[a-z0-9][a-z0-9_-]*[a-z0-9]$/;
|
||||
if (!transformIdRegex.test(definition.id)) {
|
||||
throw new EntityDefinitionIdInvalid(
|
||||
'Entity definition ID must contain only lowercase alphanumeric characters (a-z and 0-9), hyphens, and underscores. It must also start and end with alphanumeric characters.'
|
||||
);
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue