parent
92c00ce60a
commit
69685a6d7c
3 changed files with 65 additions and 9 deletions
|
@ -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');
|
||||
|
|
48
src/test/validation.test.ts
Normal file
48
src/test/validation.test.ts
Normal 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('-'));
|
||||
});
|
||||
});
|
|
@ -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 }'`);
|
||||
}
|
||||
}
|
Loading…
Add table
Reference in a new issue