|
|
@@ -17,7 +17,11 @@ import {
|
|
|
* Validates the value of a custom field input against any configured constraints.
|
|
|
* If validation fails, an error is thrown.
|
|
|
*/
|
|
|
-export function validateCustomFieldValue(config: CustomFieldConfig, value: any, languageCode?: LanguageCode): void {
|
|
|
+export function validateCustomFieldValue(
|
|
|
+ config: CustomFieldConfig,
|
|
|
+ value: any,
|
|
|
+ languageCode?: LanguageCode,
|
|
|
+): void {
|
|
|
switch (config.type) {
|
|
|
case 'string':
|
|
|
case 'localeString':
|
|
|
@@ -38,25 +42,37 @@ export function validateCustomFieldValue(config: CustomFieldConfig, value: any,
|
|
|
validateCustomFunction(config, value, languageCode);
|
|
|
}
|
|
|
|
|
|
-function validateCustomFunction<T extends TypedCustomFieldConfig<any, any>>(config: T, value: any, languageCode?: LanguageCode) {
|
|
|
+function validateCustomFunction<T extends TypedCustomFieldConfig<any, any>>(
|
|
|
+ config: T,
|
|
|
+ value: any,
|
|
|
+ languageCode?: LanguageCode,
|
|
|
+) {
|
|
|
if (typeof config.validate === 'function') {
|
|
|
const error = config.validate(value);
|
|
|
if (typeof error === 'string') {
|
|
|
throw new UserInputError(error);
|
|
|
}
|
|
|
if (Array.isArray(error)) {
|
|
|
- const localizedError = error.find(e => e.languageCode === (languageCode || DEFAULT_LANGUAGE_CODE)) || error[0];
|
|
|
+ const localizedError =
|
|
|
+ error.find(e => e.languageCode === (languageCode || DEFAULT_LANGUAGE_CODE)) || error[0];
|
|
|
throw new UserInputError(localizedError.value);
|
|
|
}
|
|
|
}
|
|
|
}
|
|
|
|
|
|
-function validateStringField(config: StringCustomFieldConfig | LocaleStringCustomFieldConfig, value: string): void {
|
|
|
+function validateStringField(
|
|
|
+ config: StringCustomFieldConfig | LocaleStringCustomFieldConfig,
|
|
|
+ value: string,
|
|
|
+): void {
|
|
|
const { pattern } = config;
|
|
|
if (pattern) {
|
|
|
const re = new RegExp(pattern);
|
|
|
if (!re.test(value)) {
|
|
|
- throw new UserInputError('error.field-invalid-string-pattern', { value, pattern });
|
|
|
+ throw new UserInputError('error.field-invalid-string-pattern', {
|
|
|
+ name: config.name,
|
|
|
+ value,
|
|
|
+ pattern,
|
|
|
+ });
|
|
|
}
|
|
|
}
|
|
|
const options = (config as StringCustomFieldConfig).options;
|
|
|
@@ -64,6 +80,7 @@ function validateStringField(config: StringCustomFieldConfig | LocaleStringCusto
|
|
|
const validOptions = options.map(o => o.value);
|
|
|
if (!validOptions.includes(value)) {
|
|
|
throw new UserInputError('error.field-invalid-string-option', {
|
|
|
+ name: config.name,
|
|
|
value,
|
|
|
validOptions: validOptions.map(o => `'${o}'`).join(', '),
|
|
|
});
|
|
|
@@ -74,19 +91,27 @@ function validateStringField(config: StringCustomFieldConfig | LocaleStringCusto
|
|
|
function validateNumberField(config: IntCustomFieldConfig | FloatCustomFieldConfig, value: number): void {
|
|
|
const { min, max } = config;
|
|
|
if (min != null && value < min) {
|
|
|
- throw new UserInputError('error.field-invalid-number-range-min', { value, min });
|
|
|
+ throw new UserInputError('error.field-invalid-number-range-min', { name: config.name, value, min });
|
|
|
}
|
|
|
if (max != null && max < value) {
|
|
|
- throw new UserInputError('error.field-invalid-number-range-max', { value, max });
|
|
|
+ throw new UserInputError('error.field-invalid-number-range-max', { name: config.name, value, max });
|
|
|
}
|
|
|
}
|
|
|
function validateDateTimeField(config: DateTimeCustomFieldConfig, value: string): void {
|
|
|
const { min, max } = config;
|
|
|
const valueDate = new Date(value);
|
|
|
if (min != null && valueDate < new Date(min)) {
|
|
|
- throw new UserInputError('error.field-invalid-datetime-range-min', { value: valueDate.toISOString(), min });
|
|
|
+ throw new UserInputError('error.field-invalid-datetime-range-min', {
|
|
|
+ name: config.name,
|
|
|
+ value: valueDate.toISOString(),
|
|
|
+ min,
|
|
|
+ });
|
|
|
}
|
|
|
if (max != null && new Date(max) < valueDate) {
|
|
|
- throw new UserInputError('error.field-invalid-datetime-range-max', { value: valueDate.toISOString(), max });
|
|
|
+ throw new UserInputError('error.field-invalid-datetime-range-max', {
|
|
|
+ name: config.name,
|
|
|
+ value: valueDate.toISOString(),
|
|
|
+ max,
|
|
|
+ });
|
|
|
}
|
|
|
}
|