Skip to content

Commit

Permalink
Add Invalid Encoding Channel Warning
Browse files Browse the repository at this point in the history
  • Loading branch information
invokesus committed Mar 4, 2018
1 parent e9ab717 commit cc2594c
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 2 deletions.
10 changes: 8 additions & 2 deletions src/encoding.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@

import {isArray} from 'vega-util';
import {Channel, CHANNELS, supportMark} from './channel';
import {Channel, CHANNELS, isChannel, supportMark} from './channel';
import {FacetMapping} from './facet';
import {
ChannelDef,
Expand Down Expand Up @@ -145,7 +145,13 @@ export function isAggregate(encoding: EncodingWithFacet<Field>) {
}

export function normalizeEncoding(encoding: Encoding<string>, mark: Mark): Encoding<string> {
return keys(encoding).reduce((normalizedEncoding: Encoding<string>, channel: Channel) => {
return keys(encoding).reduce((normalizedEncoding: Encoding<string>, channel: Channel | string) => {
if (!isChannel(channel)) {
// Drop invalid channel
log.warn(log.message.invalidEncodingChannel(channel));
return normalizedEncoding;
}

if (!supportMark(channel, mark)) {
// Drop unsupported channel

Expand Down
4 changes: 4 additions & 0 deletions src/log.ts
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,10 @@ export namespace message {
return `${channel} dropped as it is incompatible with "${markOrFacet}"${when ? ` when ${when}` : ''}.`;
}

export function invalidEncodingChannel(channel: string) {
return `${channel}-encoding is dropped as ${channel} is not a valid encoding channel.`;
}

export function facetChannelShouldBeDiscrete(channel: string) {
return `${channel} encoding should be discrete (ordinal / nominal / binned).`;
}
Expand Down
10 changes: 10 additions & 0 deletions test/compile/unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,16 @@ describe('UnitModel', function() {
assert.equal(localLogger.warns[0], log.message.incompatibleChannel(SHAPE, BAR));
}));

it('should drop invalid channel and throws warning', log.wrap((localLogger) => {
const _model = parseUnitModel({
mark: 'bar',
encoding: {
_y: {type: 'quantitative'}
}
} as any); // To make parseUnitModel accept the model with invalid encoding channel
assert.equal(localLogger.warns[0], log.message.invalidEncodingChannel('_y'));
}));

it('should drop channel without field and value and throws warning', log.wrap((localLogger) => {
const model = parseUnitModel({
mark: 'bar',
Expand Down

0 comments on commit cc2594c

Please sign in to comment.