validate publisher and extension name

fixes #20
This commit is contained in:
Joao Moreno 2015-11-17 15:38:32 +01:00
parent 92c00ce60a
commit 69685a6d7c
3 changed files with 65 additions and 9 deletions

View file

@ -10,6 +10,7 @@ import * as minimatch from 'minimatch';
import * as denodeify from 'denodeify';
import * as mime from 'mime';
import * as urljoin from 'url-join';
import { validatePublisher, validateExtensionName } from './validation';
interface IReadFile {
(filePath: string): Promise<Buffer>;
@ -226,13 +227,8 @@ class IconProcessor extends BaseProcessor {
}
export function validateManifest(manifest: Manifest): Manifest {
if (!manifest.publisher) {
throw new Error('Manifest missing field: publisher');
}
if (!manifest.name) {
throw new Error('Manifest missing field: name');
}
validatePublisher(manifest.publisher);
validateExtensionName(manifest.name);
if (!manifest.version) {
throw new Error('Manifest missing field: version');
@ -245,7 +241,7 @@ export function validateManifest(manifest: Manifest): Manifest {
if (!manifest.engines['vscode']) {
throw new Error('Manifest missing field: engines.vscode');
}
return manifest;
}

View file

@ -0,0 +1,48 @@
import * as assert from 'assert';
import { validatePublisher, validateExtensionName } from '../validation';
describe('validatePublisher', () => {
it('should throw with empty', () => {
assert.throws(() => validatePublisher(null));
assert.throws(() => validatePublisher(void 0));
assert.throws(() => validatePublisher(''));
});
it('should validate', () => {
validatePublisher('hello');
validatePublisher('Hello');
validatePublisher('HelloWorld');
validatePublisher('Hello-World');
validatePublisher('Hell0-World');
assert.throws(() => validatePublisher('hello.'));
assert.throws(() => validatePublisher('.hello'));
assert.throws(() => validatePublisher('h ello'));
assert.throws(() => validatePublisher('hello world'));
assert.throws(() => validatePublisher('-hello'));
assert.throws(() => validatePublisher('-'));
});
});
describe('validateExtensionName', () => {
it('should throw with empty', () => {
assert.throws(() => validateExtensionName(null));
assert.throws(() => validateExtensionName(void 0));
assert.throws(() => validateExtensionName(''));
});
it('should validate', () => {
validateExtensionName('hello');
validateExtensionName('Hello');
validateExtensionName('HelloWorld');
validateExtensionName('Hello-World');
validateExtensionName('Hell0-World');
assert.throws(() => validateExtensionName('hello.'));
assert.throws(() => validateExtensionName('.hello'));
assert.throws(() => validateExtensionName('h ello'));
assert.throws(() => validateExtensionName('hello world'));
assert.throws(() => validateExtensionName('-hello'));
assert.throws(() => validateExtensionName('-'));
});
});

View file

@ -1,9 +1,21 @@
const nameRegex = /^[a-z0-9][a-z0-9\-]*$/i;
export function validatePublisher(publisher: string): void {
if (!publisher) {
throw new Error(`Missing publisher name`);
}
if (!/^[a-z0-9][a-z0-9\-]*$/i.test(publisher)) {
if (!nameRegex.test(publisher)) {
throw new Error(`Invalid publisher '${ publisher }'`);
}
}
export function validateExtensionName(name: string): void {
if (!name) {
throw new Error(`Missing extension name`);
}
if (!nameRegex.test(name)) {
throw new Error(`Invalid extension name '${ name }'`);
}
}