mirror of
https://github.com/angular/angular-cli.git
synced 2025-05-21 14:02:43 +08:00
fix(@angular/cli): only values in enum should be allowed to update
This commit is contained in:
parent
dbfa851709
commit
d954ed1eab
@ -74,8 +74,12 @@ const SetCommand = Command.extend({
|
|||||||
updateLintForPrefix(this.project.root + '/tslint.json', value);
|
updateLintForPrefix(this.project.root + '/tslint.json', value);
|
||||||
}
|
}
|
||||||
|
|
||||||
config.set(jsonPath, value);
|
try {
|
||||||
config.save();
|
config.set(jsonPath, value);
|
||||||
|
config.save();
|
||||||
|
} catch (error) {
|
||||||
|
throw new SilentError(error.message);
|
||||||
|
}
|
||||||
resolve();
|
resolve();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
@ -4,10 +4,8 @@ import {JsonSchemaErrorBase} from './error';
|
|||||||
|
|
||||||
import './mimetypes';
|
import './mimetypes';
|
||||||
|
|
||||||
|
|
||||||
export class InvalidJsonPath extends JsonSchemaErrorBase {}
|
export class InvalidJsonPath extends JsonSchemaErrorBase {}
|
||||||
|
|
||||||
|
|
||||||
// The schema tree node property of the SchemaClass.
|
// The schema tree node property of the SchemaClass.
|
||||||
const kSchemaNode = Symbol('schema-node');
|
const kSchemaNode = Symbol('schema-node');
|
||||||
// The value property of the SchemaClass.
|
// The value property of the SchemaClass.
|
||||||
@ -132,6 +130,7 @@ class SchemaClassBase<T> implements SchemaClass<T> {
|
|||||||
/** Set a value from a JSON path. */
|
/** Set a value from a JSON path. */
|
||||||
$$set(path: string, value: any): void {
|
$$set(path: string, value: any): void {
|
||||||
const node = _getSchemaNodeForPath(this.$$schema(), path);
|
const node = _getSchemaNodeForPath(this.$$schema(), path);
|
||||||
|
|
||||||
if (node) {
|
if (node) {
|
||||||
node.set(value);
|
node.set(value);
|
||||||
} else {
|
} else {
|
||||||
|
@ -55,12 +55,11 @@ describe('@ngtools/json-schema', () => {
|
|||||||
});
|
});
|
||||||
|
|
||||||
expect(proto.a instanceof Array).toBe(true);
|
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.
|
// Set it to a string, which is valid.
|
||||||
proto.a[0] = 'v2';
|
proto.a[0] = 'v2';
|
||||||
proto.a[1] = 'INVALID';
|
expect(proto.a).toEqual(['v2', 'v3']);
|
||||||
expect(proto.a).toEqual(['v2', undefined, undefined, 'v3']);
|
|
||||||
});
|
});
|
||||||
|
|
||||||
it('supports default values', () => {
|
it('supports default values', () => {
|
||||||
@ -72,6 +71,23 @@ describe('@ngtools/json-schema', () => {
|
|||||||
|
|
||||||
expect(schema.children['b'].get()).toEqual('default');
|
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');
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -7,7 +7,7 @@ export class InvalidSchema extends JsonSchemaErrorBase {}
|
|||||||
export class InvalidValueError extends JsonSchemaErrorBase {}
|
export class InvalidValueError extends JsonSchemaErrorBase {}
|
||||||
export class MissingImplementationError extends JsonSchemaErrorBase {}
|
export class MissingImplementationError extends JsonSchemaErrorBase {}
|
||||||
export class SettingReadOnlyPropertyError extends JsonSchemaErrorBase {}
|
export class SettingReadOnlyPropertyError extends JsonSchemaErrorBase {}
|
||||||
|
export class InvalidUpdateValue extends JsonSchemaErrorBase {}
|
||||||
|
|
||||||
export interface Schema {
|
export interface Schema {
|
||||||
[key: string]: any;
|
[key: string]: any;
|
||||||
@ -482,6 +482,13 @@ class EnumSchemaTreeNode extends LeafSchemaTreeNode<any> {
|
|||||||
|
|
||||||
get items() { return this._schema['enum']; }
|
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) {
|
isCompatible(v: any) {
|
||||||
return this._isInEnum(v);
|
return this._isInEnum(v);
|
||||||
}
|
}
|
||||||
|
@ -1,8 +1,6 @@
|
|||||||
{
|
{
|
||||||
"a": [
|
"a": [
|
||||||
"INVALID",
|
|
||||||
"v1",
|
"v1",
|
||||||
"INVALID",
|
|
||||||
"v3"
|
"v3"
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
9
tests/e2e/tests/commands/set/set-enum-check.ts
Normal file
9
tests/e2e/tests/commands/set/set-enum-check.ts
Normal file
@ -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'));
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user