[EEM] add additional validations for entity definition IDs (#187555)

add additional validations for entity definition IDs
This commit is contained in:
Tom Myers 2024-07-04 15:17:55 +01:00 committed by GitHub
parent 05c7a19ea6
commit fac236e567
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
3 changed files with 34 additions and 9 deletions

View file

@ -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';
}
}

View file

@ -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);
});
});

View file

@ -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.'
);
}
}