diff --git a/public/app/percona/shared/helpers/validators.test.tsx b/public/app/percona/shared/helpers/validators.test.tsx index b62af3e245a26..aa43d18184679 100644 --- a/public/app/percona/shared/helpers/validators.test.tsx +++ b/public/app/percona/shared/helpers/validators.test.tsx @@ -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'; @@ -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); diff --git a/public/app/percona/shared/helpers/validators.ts b/public/app/percona/shared/helpers/validators.ts index b973e443ea52b..8777e424e462c 100644 --- a/public/app/percona/shared/helpers/validators.ts +++ b/public/app/percona/shared/helpers/validators.ts @@ -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'; }