Skip to content

Commit

Permalink
Deprecate schema-less specs in Vega
Browse files Browse the repository at this point in the history
Closes #30951
  • Loading branch information
alexwizp committed Jul 30, 2020
1 parent 6fc193c commit f98aa01
Show file tree
Hide file tree
Showing 4 changed files with 30 additions and 33 deletions.
25 changes: 13 additions & 12 deletions src/plugins/vis_type_vega/public/data_model/vega_parser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,16 @@ jest.mock('../lib/vega', () => ({
vegaLite: jest.requireActual('vega-lite'),
}));

describe(`VegaParser.parseAsync`, () => {
test(`should throw an error in case of $spec is not defined`, async () => {
const vp = new VegaParser('{}');

await vp.parseAsync();

expect(vp.error).toBe('The input spec does not specify a "$schema"');
});
});

describe(`VegaParser._setDefaultValue`, () => {
function check(spec, expected, ...params) {
return () => {
Expand Down Expand Up @@ -149,23 +159,14 @@ describe('VegaParser._resolveEsQueries', () => {
);
});

describe('VegaParser._parseSchema', () => {
function check(schema, isVegaLite, warningCount) {
describe('VegaParser.parseSchema', () => {
function check(schema, isVegaLite) {
return () => {
const vp = new VegaParser({ $schema: schema });
expect(vp._parseSchema()).toBe(isVegaLite);
expect(vp.spec).toEqual({ $schema: schema });
expect(vp.warnings).toHaveLength(warningCount);
expect(vp.parseSchema(vp.spec).isVegaLite).toBe(isVegaLite);
};
}

test('should warn on no vega version specified', () => {
const vp = new VegaParser({});
expect(vp._parseSchema()).toBe(false);
expect(vp.spec).toEqual({ $schema: 'https://vega.github.io/schema/vega/v5.json' });
expect(vp.warnings).toHaveLength(1);
});

test(
'should not warn on current vega version',
check('https://vega.github.io/schema/vega/v5.json', false, 0)
Expand Down
36 changes: 17 additions & 19 deletions src/plugins/vis_type_vega/public/data_model/vega_parser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ const locToDirMap: Record<string, ControlsLocation> = {
top: 'column-reverse',
bottom: 'column',
};
const DEFAULT_SCHEMA: string = 'https://vega.github.io/schema/vega/v5.json';

// If there is no "%type%" parameter, use this parser
const DEFAULT_PARSER: string = 'elasticsearch';
Expand Down Expand Up @@ -117,16 +116,27 @@ export class VegaParser {
if (this.isVegaLite !== undefined) throw new Error();

if (typeof this.spec === 'string') {
this.spec = hjson.parse(this.spec, { legacyRoot: false });
const spec = hjson.parse(this.spec, { legacyRoot: false });

if (!spec.$schema) {
throw new Error(
i18n.translate('visTypeVega.vegaParser.inputSpecDoesNotSpecifySchemaErrorMessage', {
defaultMessage: 'The input spec does not specify a {schemaParam}',
values: { schemaParam: '"$schema"' },
})
);
}
this.spec = spec;
}

if (!_.isPlainObject(this.spec)) {
throw new Error(
i18n.translate('visTypeVega.vegaParser.invalidVegaSpecErrorMessage', {
defaultMessage: 'Invalid Vega specification',
})
);
}
this.isVegaLite = this._parseSchema();
this.isVegaLite = this.parseSchema(this.spec).isVegaLite;
this.useHover = !this.isVegaLite;

this._config = this._parseConfig();
Expand Down Expand Up @@ -498,23 +508,11 @@ export class VegaParser {

/**
* Parse Vega schema element
* @returns {boolean} is this a VegaLite schema?
* @returns {object} isVegaLite, libVersion
* @private
*/
_parseSchema() {
if (!this.spec) return false;
if (!this.spec.$schema) {
this._onWarning(
i18n.translate('visTypeVega.vegaParser.inputSpecDoesNotSpecifySchemaWarningMessage', {
defaultMessage:
'The input spec does not specify a {schemaParam}, defaulting to {defaultSchema}',
values: { defaultSchema: `"${DEFAULT_SCHEMA}"`, schemaParam: '"$schema"' },
})
);
this.spec.$schema = DEFAULT_SCHEMA;
}

const schema = schemaParser(this.spec.$schema);
private parseSchema(spec: VegaSpec) {
const schema = schemaParser(spec.$schema);
const isVegaLite = schema.library === 'vega-lite';
const libVersion = isVegaLite ? vegaLite.version : vega.version;

Expand All @@ -532,7 +530,7 @@ export class VegaParser {
);
}

return isVegaLite;
return { isVegaLite, libVersion };
}

/**
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/ja-JP.json
Original file line number Diff line number Diff line change
Expand Up @@ -4283,7 +4283,6 @@
"visTypeVega.vegaParser.dataExceedsSomeParamsUseTimesLimitErrorMessage": "データには {urlParam}、{valuesParam}、 {sourceParam} の内複数を含めることができません",
"visTypeVega.vegaParser.hostConfigIsDeprecatedWarningMessage": "{deprecatedConfigName} は廃止されました。代わりに {newConfigName} を使用してください。",
"visTypeVega.vegaParser.hostConfigValueTypeErrorMessage": "{configName} が含まれている場合、オブジェクトでなければなりません",
"visTypeVega.vegaParser.inputSpecDoesNotSpecifySchemaWarningMessage": "インプット仕様で {schemaParam} が指定されていないため、デフォルトで {defaultSchema} になります",
"visTypeVega.vegaParser.invalidVegaSpecErrorMessage": "無効な Vega 仕様",
"visTypeVega.vegaParser.kibanaConfigValueTypeErrorMessage": "{configName} が含まれている場合、オブジェクトでなければなりません",
"visTypeVega.vegaParser.mapStyleValueTypeWarningMessage": "{mapStyleConfigName} は {mapStyleConfigFirstAllowedValue} か {mapStyleConfigSecondAllowedValue} のどちらかです",
Expand Down
1 change: 0 additions & 1 deletion x-pack/plugins/translations/translations/zh-CN.json
Original file line number Diff line number Diff line change
Expand Up @@ -4284,7 +4284,6 @@
"visTypeVega.vegaParser.dataExceedsSomeParamsUseTimesLimitErrorMessage": "数据不得包含 {urlParam}、{valuesParam} 和 {sourceParam} 中的多个值",
"visTypeVega.vegaParser.hostConfigIsDeprecatedWarningMessage": "{deprecatedConfigName} 已弃用。请改用 {newConfigName}。",
"visTypeVega.vegaParser.hostConfigValueTypeErrorMessage": "如果存在,{configName} 必须为对象",
"visTypeVega.vegaParser.inputSpecDoesNotSpecifySchemaWarningMessage": "输入规范未指定 {schemaParam},其默认值为 {defaultSchema}",
"visTypeVega.vegaParser.invalidVegaSpecErrorMessage": "Vega 规范无效",
"visTypeVega.vegaParser.kibanaConfigValueTypeErrorMessage": "如果存在,{configName} 必须为对象",
"visTypeVega.vegaParser.mapStyleValueTypeWarningMessage": "{mapStyleConfigName} 可能为 {mapStyleConfigFirstAllowedValue} 或 {mapStyleConfigSecondAllowedValue}",
Expand Down

0 comments on commit f98aa01

Please sign in to comment.