Skip to content

Commit

Permalink
fix(@angular/cli): only values in enum should be allowed to update
Browse files Browse the repository at this point in the history
  • Loading branch information
sumitarora authored and Brocco committed May 24, 2017
1 parent 8683c3c commit 7159920
Show file tree
Hide file tree
Showing 6 changed files with 43 additions and 10 deletions.
8 changes: 6 additions & 2 deletions packages/@angular/cli/commands/set.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,12 @@ const SetCommand = Command.extend({
updateLintForPrefix(this.project.root + '/tslint.json', value);
}

config.set(jsonPath, value);
config.save();
try {
config.set(jsonPath, value);
config.save();
} catch (error) {
throw new SilentError(error.message);
}
resolve();
});
}
Expand Down
3 changes: 1 addition & 2 deletions packages/@ngtools/json-schema/src/schema-class-factory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,8 @@ import {JsonSchemaErrorBase} from './error';

import './mimetypes';


export class InvalidJsonPath extends JsonSchemaErrorBase {}


// The schema tree node property of the SchemaClass.
const kSchemaNode = Symbol('schema-node');
// The value property of the SchemaClass.
Expand Down Expand Up @@ -132,6 +130,7 @@ class SchemaClassBase<T> implements SchemaClass<T> {
/** Set a value from a JSON path. */
$$set(path: string, value: any): void {
const node = _getSchemaNodeForPath(this.$$schema(), path);

if (node) {
node.set(value);
} else {
Expand Down
22 changes: 19 additions & 3 deletions packages/@ngtools/json-schema/src/schema-tree.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,12 +55,11 @@ describe('@ngtools/json-schema', () => {
});

expect(proto.a instanceof Array).toBe(true);
expect(proto.a).toEqual([undefined, 'v1', undefined, 'v3']);
expect(proto.a).toEqual(['v1', 'v3']);

// Set it to a string, which is valid.
proto.a[0] = 'v2';
proto.a[1] = 'INVALID';
expect(proto.a).toEqual(['v2', undefined, undefined, 'v3']);
expect(proto.a).toEqual(['v2', 'v3']);
});

it('supports default values', () => {
Expand All @@ -72,6 +71,23 @@ describe('@ngtools/json-schema', () => {

expect(schema.children['b'].get()).toEqual('default');
});


it('should throw error when setting invalid value', () => {
const proto: any = Object.create(null);
// tslint:disable-next-line
new RootSchemaTreeNode(proto, {
value: valueJson,
schema: schemaJson
});

try {
proto.a[0] = 'INVALID';
} catch (error) {
expect(error.message).toBe('Invalid value can only be one of these: v1,v2,v3');
}
});

});

});
9 changes: 8 additions & 1 deletion packages/@ngtools/json-schema/src/schema-tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ export class InvalidSchema extends JsonSchemaErrorBase {}
export class InvalidValueError extends JsonSchemaErrorBase {}
export class MissingImplementationError extends JsonSchemaErrorBase {}
export class SettingReadOnlyPropertyError extends JsonSchemaErrorBase {}

export class InvalidUpdateValue extends JsonSchemaErrorBase {}

export interface Schema {
[key: string]: any;
Expand Down Expand Up @@ -482,6 +482,13 @@ class EnumSchemaTreeNode extends LeafSchemaTreeNode<any> {

get items() { return this._schema['enum']; }

set(value: string, init = false, force = false) {
if (!(value === undefined || this._isInEnum(value))) {
throw new InvalidUpdateValue('Invalid value can only be one of these: ' + this.items);
}
super.set(value, init, force);
}

isCompatible(v: any) {
return this._isInEnum(v);
}
Expand Down
2 changes: 0 additions & 2 deletions packages/@ngtools/json-schema/tests/value2-1.json
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
{
"a": [
"INVALID",
"v1",
"INVALID",
"v3"
]
}
9 changes: 9 additions & 0 deletions tests/e2e/tests/commands/set/set-enum-check.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import {ng} from '../../../utils/process';
import {expectToFail} from '../../../utils/utils';

export default function() {
return Promise.resolve()
.then(() => expectToFail(() => ng('set', 'defaults.component.aaa', 'bbb')))
.then(() => expectToFail(() => ng('set', 'defaults.component.viewEncapsulation', 'bbb')))
.then(() => ng('set', 'defaults.component.viewEncapsulation', 'Emulated'));
}

0 comments on commit 7159920

Please sign in to comment.