提交 b82e5905 编写于 作者: R Rob Lourens

Don't show validations for default setting values in settings editor

Fix #97034
上级 cdc37119
......@@ -55,30 +55,41 @@ export function createValidator(prop: IConfigurationPropertySchema): (value: any
};
}
/**
* Returns an error string if the value is invalid and can't be displayed in the settings UI for the given type.
*/
export function getInvalidTypeError(value: any, type: undefined | string | string[]): string | undefined {
let typeArr = Array.isArray(type) ? type : [type];
const isNullable = canBeType(typeArr, 'null');
if (canBeType(typeArr, 'number', 'integer') && (typeArr.length === 1 || typeArr.length === 2 && isNullable)) {
if (value === '' || isNaN(+value)) {
return nls.localize('validations.expectedNumeric', "Value must be a number.");
}
if (typeof type === 'undefined') {
return;
}
const valueType = typeof value;
if (
(valueType === 'boolean' && !canBeType(typeArr, 'boolean')) ||
(valueType === 'object' && !canBeType(typeArr, 'object', 'null', 'array')) ||
(valueType === 'string' && !canBeType(typeArr, 'string', 'number', 'integer')) ||
(Array.isArray(value) && !canBeType(typeArr, 'array'))
) {
if (typeof type !== 'undefined') {
return nls.localize('invalidTypeError', "Setting has an invalid type, expected {0}. Fix in JSON.", JSON.stringify(type));
}
const typeArr = Array.isArray(type) ? type : [type];
if (!typeArr.some(_type => valueValidatesAsType(value, _type))) {
return nls.localize('invalidTypeError', "Setting has an invalid type, expected {0}. Fix in JSON.", JSON.stringify(type));
}
return;
}
function valueValidatesAsType(value: any, type: string): boolean {
const valueType = typeof value;
if (type === 'boolean') {
return valueType === 'boolean';
} else if (type === 'object') {
return value && !Array.isArray(value) && valueType === 'object';
} else if (type === 'null') {
return value === null;
} else if (type === 'array') {
return Array.isArray(value);
} else if (type === 'string') {
return valueType === 'string';
} else if (type === 'number' || type === 'integer') {
return valueType === 'number';
}
return true;
}
function getStringValidators(prop: IConfigurationPropertySchema) {
let patternRegex: RegExp | undefined;
if (typeof prop.pattern === 'string') {
......
......@@ -5,10 +5,10 @@
import * as assert from 'assert';
import { IConfigurationPropertySchema } from 'vs/platform/configuration/common/configurationRegistry';
import { createValidator } from 'vs/workbench/services/preferences/common/preferencesValidation';
import { createValidator, getInvalidTypeError } from 'vs/workbench/services/preferences/common/preferencesValidation';
suite('Preferences Model test', () => {
suite('Preferences Validation', () => {
class Tester {
private validator: (value: any) => string | null;
......@@ -334,4 +334,43 @@ suite('Preferences Model test', () => {
arr.rejects(['a', 'a']).withMessage(`Array has duplicate items`);
});
test('getInvalidTypeError', () => {
function testInvalidTypeError(value: any, type: string | string[], shouldValidate: boolean) {
const message = `value: ${value}, type: ${JSON.stringify(type)}, expected: ${shouldValidate ? 'valid' : 'invalid'}`;
if (shouldValidate) {
assert.ok(!getInvalidTypeError(value, type), message);
} else {
assert.ok(getInvalidTypeError(value, type), message);
}
}
testInvalidTypeError(1, 'number', true);
testInvalidTypeError(1.5, 'number', true);
testInvalidTypeError([1], 'number', false);
testInvalidTypeError('1', 'number', false);
testInvalidTypeError({ a: 1 }, 'number', false);
testInvalidTypeError(null, 'number', false);
testInvalidTypeError('a', 'string', true);
testInvalidTypeError('1', 'string', true);
testInvalidTypeError([], 'string', false);
testInvalidTypeError({}, 'string', false);
testInvalidTypeError([1], 'array', true);
testInvalidTypeError([], 'array', true);
testInvalidTypeError([{}, [[]]], 'array', true);
testInvalidTypeError({ a: ['a'] }, 'array', false);
testInvalidTypeError('hello', 'array', false);
testInvalidTypeError(true, 'boolean', true);
testInvalidTypeError('hello', 'boolean', false);
testInvalidTypeError(null, 'boolean', false);
testInvalidTypeError([true], 'boolean', false);
testInvalidTypeError(null, 'null', true);
testInvalidTypeError(false, 'null', false);
testInvalidTypeError([null], 'null', false);
testInvalidTypeError('null', 'null', false);
});
});
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册