diff --git a/lib/validation/queue/QueueName.js b/lib/validation/queue/QueueName.js index 57da0f5..c347502 100644 --- a/lib/validation/queue/QueueName.js +++ b/lib/validation/queue/QueueName.js @@ -16,14 +16,8 @@ class QueueName { if (name.length < 3 || name.length > 63) { throw new AError(ErrorCodes.OutOfRangeInput); } - - if (name.includes('--') || name[0] === '-' || name[name.length - 1] === '-') { - throw new AError(ErrorCodes.InvalidInput); - } - - // Matches non-alphanumeric characters - const re = new RegExp(/\W|_/); - if (re.test(name.replace('-', ''))) { // Fixme: exclude '-' in regex + + if (/^([a-z0-9]+)(-[a-z0-9]+)*$/i.test(name) === false) { throw new AError(ErrorCodes.InvalidInput); } } diff --git a/test/validation.js b/test/validation.js new file mode 100644 index 0000000..e5cca21 --- /dev/null +++ b/test/validation.js @@ -0,0 +1,50 @@ +const QueueName = require('../lib/validation/queue/QueueName'), + AError = require('../lib/core/AzuriteError') + ErrorCodes = require('../lib/core/ErrorCodes') + expect = require('chai').expect; + +describe('validation', () => { + describe('QueueName', () => { + const createQueueNameRequest = (queueName) => { return { request: { queueName } } }; + + it('should throw out of range if name is less than three characters', () => { + expect(() => QueueName.validate(createQueueNameRequest(''))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest('a'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest('aa'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest('aaa'))).not.to.throw(); + }); + + it('should throw out of range if name is greater than sixty three characters', () => { + const sixtyThreeCharacterStringName = '012345678901234567890123456789012345678901234567890123456789012'; + + expect(() => QueueName.validate(createQueueNameRequest(sixtyThreeCharacterStringName))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest(sixtyThreeCharacterStringName + '3'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + expect(() => QueueName.validate(createQueueNameRequest(sixtyThreeCharacterStringName + '34'))).to.throw(AError, ErrorCodes.OutOfRangeInput); + }); + + it('should throw invalid input if name starts with a dash', () => { + expect(() => QueueName.validate(createQueueNameRequest("-queue"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("-queue-name"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + + it('should throw invalid input if name ends with a dash', () => { + expect(() => QueueName.validate(createQueueNameRequest("queue-"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue-name-"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + + it('should throw invalid input if contians two consecutive dashes', () => { + expect(() => QueueName.validate(createQueueNameRequest("queue--name"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + + it('should throw invalid input if contians anything except alphanumeric characters and dashes', () => { + expect(() => QueueName.validate(createQueueNameRequest("queue-name"))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest("queue1"))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest("QUEUE-name-1"))).not.to.throw(); + expect(() => QueueName.validate(createQueueNameRequest("queue_name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue~name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue@name"))).to.throw(AError, ErrorCodes.InvalidInput); + expect(() => QueueName.validate(createQueueNameRequest("queue:name"))).to.throw(AError, ErrorCodes.InvalidInput); + }); + }); +});