Skip to content

Commit

Permalink
Add schema check to statically typed APIs.
Browse files Browse the repository at this point in the history
Signed-off-by: Cai Xuye <a1994846931931@gmail.com>
  • Loading branch information
a1994846931931 committed Sep 6, 2019
1 parent fea73da commit 013dbf1
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 3 deletions.
20 changes: 17 additions & 3 deletions packages/core/src/browser/preferences/preference-proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,15 +68,29 @@ export function createPreferenceProxy<T>(preferences: PreferenceService, schema:
throw new Error('Unsupported operation');
};
const getValue: PreferenceRetrieval<any>['get'] = (arg, defaultValue, resourceUri) => {
const preferenceName = typeof arg === 'object' && arg.overrideIdentifier ?
const isArgOverridePreferenceName = typeof arg === 'object' && arg.overrideIdentifier;
const preferenceName = isArgOverridePreferenceName ?
preferences.overridePreferenceName(<OverridePreferenceName>arg) :
<string>arg;
return preferences.get(preferenceName, defaultValue, resourceUri);
const value = preferences.get(preferenceName, defaultValue, resourceUri);
if (preferences.validate(isArgOverridePreferenceName ? (<OverridePreferenceName>arg).preferenceName : preferenceName, value)) {
return value;
}
if (defaultValue !== undefined) {
return defaultValue;
}
const values = preferences.inspect(preferenceName, resourceUri);
return values && values.defaultValue;
};
return new Proxy({}, {
get: (_, property: string) => {
if (schema.properties[property]) {
return preferences.get(property);
const value = preferences.get(property);
if (preferences.validate(property, value)) {
return value;
}
const values = preferences.inspect(property);
return values && values.defaultValue;
}
if (property === 'onPreferenceChanged') {
return onPreferenceChangedEmitter.event;
Expand Down
4 changes: 4 additions & 0 deletions packages/core/src/browser/preferences/preference-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,7 @@ export interface PreferenceService extends Disposable {
overriddenPreferenceName(preferenceName: string): OverridePreferenceName | undefined;

resolve<T>(preferenceName: string, defaultValue?: T, resourceUri?: string): PreferenceResolveResult<T>;
validate(name: string, value: any): boolean;
}

/**
Expand Down Expand Up @@ -412,4 +413,7 @@ export class PreferenceServiceImpl implements PreferenceService, FrontendApplica
};
}

validate(name: string, value: any): boolean {
return this.schema.validate(name, value);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,7 @@ export class MockPreferenceService implements PreferenceService {
overriddenPreferenceName(preferenceName: string): OverridePreferenceName | undefined {
return undefined;
}

// tslint:disable-next-line:no-any
validate(name: string, value: any): boolean { return true; }
}
Original file line number Diff line number Diff line change
Expand Up @@ -85,10 +85,12 @@ describe('editor-preview-manager', () => {

it('should handle preview requests if editor.enablePreview enabled', async () => {
(mockPreference.get as sinon.SinonStub).returns(true);
(mockPreference.validate as sinon.SinonStub).returns(true);
expect(await previewManager.canHandle(new URI(), {preview: true})).to.be.greaterThan(0);
});
it('should not handle preview requests if editor.enablePreview disabled', async () => {
(mockPreference.get as sinon.SinonStub).returns(false);
(mockPreference.validate as sinon.SinonStub).returns(true);
expect(await previewManager.canHandle(new URI(), {preview: true})).to.equal(0);
});
it('should not handle requests that are not preview or currently being previewed', async () => {
Expand Down

0 comments on commit 013dbf1

Please sign in to comment.