Skip to content

Commit

Permalink
[EEM] add additional validations for entity definition IDs (#187555)
Browse files Browse the repository at this point in the history
add additional validations for entity definition IDs
  • Loading branch information
tommyers-elastic authored Jul 4, 2024
1 parent 05c7a19 commit fac236e
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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';
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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.'
);
}
}

0 comments on commit fac236e

Please sign in to comment.