Skip to content

Commit 7159920

Browse files
sumitaroraBrocco
authored andcommitted
fix(@angular/cli): only values in enum should be allowed to update
1 parent 8683c3c commit 7159920

File tree

6 files changed

+43
-10
lines changed

6 files changed

+43
-10
lines changed

packages/@angular/cli/commands/set.ts

+6-2
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,12 @@ const SetCommand = Command.extend({
7474
updateLintForPrefix(this.project.root + '/tslint.json', value);
7575
}
7676

77-
config.set(jsonPath, value);
78-
config.save();
77+
try {
78+
config.set(jsonPath, value);
79+
config.save();
80+
} catch (error) {
81+
throw new SilentError(error.message);
82+
}
7983
resolve();
8084
});
8185
}

packages/@ngtools/json-schema/src/schema-class-factory.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,8 @@ import {JsonSchemaErrorBase} from './error';
44

55
import './mimetypes';
66

7-
87
export class InvalidJsonPath extends JsonSchemaErrorBase {}
98

10-
119
// The schema tree node property of the SchemaClass.
1210
const kSchemaNode = Symbol('schema-node');
1311
// The value property of the SchemaClass.
@@ -132,6 +130,7 @@ class SchemaClassBase<T> implements SchemaClass<T> {
132130
/** Set a value from a JSON path. */
133131
$$set(path: string, value: any): void {
134132
const node = _getSchemaNodeForPath(this.$$schema(), path);
133+
135134
if (node) {
136135
node.set(value);
137136
} else {

packages/@ngtools/json-schema/src/schema-tree.spec.ts

+19-3
Original file line numberDiff line numberDiff line change
@@ -55,12 +55,11 @@ describe('@ngtools/json-schema', () => {
5555
});
5656

5757
expect(proto.a instanceof Array).toBe(true);
58-
expect(proto.a).toEqual([undefined, 'v1', undefined, 'v3']);
58+
expect(proto.a).toEqual(['v1', 'v3']);
5959

6060
// Set it to a string, which is valid.
6161
proto.a[0] = 'v2';
62-
proto.a[1] = 'INVALID';
63-
expect(proto.a).toEqual(['v2', undefined, undefined, 'v3']);
62+
expect(proto.a).toEqual(['v2', 'v3']);
6463
});
6564

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

7372
expect(schema.children['b'].get()).toEqual('default');
7473
});
74+
75+
76+
it('should throw error when setting invalid value', () => {
77+
const proto: any = Object.create(null);
78+
// tslint:disable-next-line
79+
new RootSchemaTreeNode(proto, {
80+
value: valueJson,
81+
schema: schemaJson
82+
});
83+
84+
try {
85+
proto.a[0] = 'INVALID';
86+
} catch (error) {
87+
expect(error.message).toBe('Invalid value can only be one of these: v1,v2,v3');
88+
}
89+
});
90+
7591
});
7692

7793
});

packages/@ngtools/json-schema/src/schema-tree.ts

+8-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ export class InvalidSchema extends JsonSchemaErrorBase {}
77
export class InvalidValueError extends JsonSchemaErrorBase {}
88
export class MissingImplementationError extends JsonSchemaErrorBase {}
99
export class SettingReadOnlyPropertyError extends JsonSchemaErrorBase {}
10-
10+
export class InvalidUpdateValue extends JsonSchemaErrorBase {}
1111

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

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

485+
set(value: string, init = false, force = false) {
486+
if (!(value === undefined || this._isInEnum(value))) {
487+
throw new InvalidUpdateValue('Invalid value can only be one of these: ' + this.items);
488+
}
489+
super.set(value, init, force);
490+
}
491+
485492
isCompatible(v: any) {
486493
return this._isInEnum(v);
487494
}
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
{
22
"a": [
3-
"INVALID",
43
"v1",
5-
"INVALID",
64
"v3"
75
]
86
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import {ng} from '../../../utils/process';
2+
import {expectToFail} from '../../../utils/utils';
3+
4+
export default function() {
5+
return Promise.resolve()
6+
.then(() => expectToFail(() => ng('set', 'defaults.component.aaa', 'bbb')))
7+
.then(() => expectToFail(() => ng('set', 'defaults.component.viewEncapsulation', 'bbb')))
8+
.then(() => ng('set', 'defaults.component.viewEncapsulation', 'Emulated'));
9+
}

0 commit comments

Comments
 (0)