Skip to content

Commit

Permalink
Merge branch 'main' into PMM-11148-inventory-manage-labels
Browse files Browse the repository at this point in the history
  • Loading branch information
matejkubinec committed Aug 28, 2023
2 parents f3a73bf + 6c59e50 commit 1b66f01
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 2 deletions.
45 changes: 44 additions & 1 deletion public/app/percona/shared/helpers/validators.test.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ describe('validatePort test', () => {
});

describe('validateKeyValue test', () => {
const errorMessage = 'Values have to be in key:value format, and separated with new line or space';

it('return empty string when key:value is separated by whitespace', () => {
const testString = 'key:value key2:value2';

Expand All @@ -35,8 +37,49 @@ describe('validateKeyValue test', () => {
expect(validators.validateKeyValue(testString)).toEqual(undefined);
});

it('returns empty string if value containts dash', () => {
const testString = 'key:value-';

expect(validators.validateKeyValue(testString)).toEqual(undefined);
});

it('returns empty string if key starts with a dash', () => {
const testString = '_key:value';

expect(validators.validateKeyValue(testString)).toEqual(undefined);
});

it('returns correct error message when key contains dash', () => {
const testString = 'key-:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key starts with two underscores', () => {
const testString = '__key:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key starts with two underscores', () => {
const testString = '__key:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key starts with number', () => {
const testString = '0key:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('returns correct error message when key contains non ASCII character', () => {
const testString = 'keý:value';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
});

it('return correct error message when value is invalid', () => {
const errorMessage = 'Values have to be in key:value format, and separated with new line or space';
const testString = 'key:value-key2:value2';

expect(validators.validateKeyValue(testString)).toEqual(errorMessage);
Expand Down
12 changes: 11 additions & 1 deletion public/app/percona/shared/helpers/validators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,17 @@ export const validators = {
!value
.split(/[\n\s]/)
.filter(Boolean)
.every((element) => /^[a-z0-9]+:[a-z0-9]+$/.test(element))
.every((element) => {
const [key, value, ...rest] = element.split(':');

// check key against prometheus data model
// https://prometheus.io/docs/concepts/data_model/#metric-names-and-labels
if (/^[a-zA-Z_][a-zA-Z0-9_]*$/.test(key) && !key.startsWith('__') && !!value && !rest.length) {
return true;
}

return false;
})
) {
return 'Values have to be in key:value format, and separated with new line or space';
}
Expand Down

0 comments on commit 1b66f01

Please sign in to comment.