132
}
133
135
>
const uriRegex = /^(([^:/?#]+?):)?(\/\/([^/?#]*))?([^?#]*)(\?([^#]*))?(#(.*))?/;
136
>
let patternRegex: RegExp | undefined;
137
>
if (typeof prop.pattern === 'string') {
138
patternRegex = toRegExp(prop.pattern);
139
}
141
>
return [
142
>
{
143
>
enabled: prop.maxLength !== undefined,
144
>
isValid: ((value: { length: number }) => value.length <= prop.maxLength!),
145
>
message: nls.localize('validations.maxLength', "Value must be {0} or fewer characters long.", prop.maxLength)
146
>
},
147
>
{
148
>
enabled: prop.minLength !== undefined,
149
>
isValid: ((value: { length: number }) => value.length >= prop.minLength!),
150
>
message: nls.localize('validations.minLength', "Value must be {0} or more characters long.", prop.minLength)
151
>
},
152
>
{
153
>
enabled: patternRegex !== undefined,
154
>
isValid: ((value: string) => patternRegex!.test(value)),
155
>
message: prop.patternErrorMessage || nls.localize('validations.regex', "Value must match regex `{0}`.", prop.pattern)
156
>
},
157
>
{
158
>
enabled: prop.format === 'color-hex',
159
>
isValid: ((value: string) => Color.Format.CSS.parseHex(value)),
160
>
message: nls.localize('validations.colorFormat', "Invalid color format. Use #RGB, #RGBA, #RRGGBB or #RRGGBBAA.")
161
>
},
162
>
{
163
>
enabled: prop.format === 'uri' || prop.format === 'uri-reference',
164
>
isValid: ((value: string) => !!value.length),
165
>
message: nls.localize('validations.uriEmpty', "URI expected.")
166
>
},
167
>
{
168
>
enabled: prop.format === 'uri' || prop.format === 'uri-reference',
169
>
isValid: ((value: string) => uriRegex.test(value)),
170
>
message: nls.localize('validations.uriMissing', "URI is expected.")
171
>
},
172
>
{
173
>
enabled: prop.format === 'uri',
174
>
isValid: ((value: string) => {
175
const matches = value.match(uriRegex);
176
return !!(matches && matches[2]);
177
}),
179
>
},
180
>
{
181
>
enabled: prop.enum !== undefined,
182
>
isValid: ((value: string) => {
183
return prop.enum!.includes(value);
184
}),
185
>
message: nls.localize('validations.invalidStringEnumValue', "Value is not accepted. Valid values: {0}.",
preferencesValidation.ts
186
>
prop.enum ? prop.enum.map(key => `"${key}"`).join(', ') : '[]')
187
>
}
188
>
].filter(validation => validation.enabled);
189
>
}
190
192
>
const type: (string | undefined)[] = Array.isArray(prop.type) ? prop.type : [prop.type];
193
>
194
>
const isNullable = canBeType(type, 'null');
195
>
const isIntegral = (canBeType(type, 'integer')) && (type.length === 1 || type.length === 2 && isNullable);
196
>
const isNumeric = canBeType(type, 'number', 'integer') && (type.length === 1 || type.length === 2 && isNullable);
197
>
if (!isNumeric) {
198
return [];
199
}