|
|
@@ -14,6 +14,19 @@ import {
|
|
|
} from '@/vdb/framework/form-engine/form-engine-types.js';
|
|
|
import { z, ZodRawShape, ZodType, ZodTypeAny } from 'zod';
|
|
|
|
|
|
+/**
|
|
|
+ * Centralized validation messages for form schema generation.
|
|
|
+ * Provides consistent messaging across all form validations and enables future i18n support.
|
|
|
+ */
|
|
|
+const VALIDATION_MESSAGES = {
|
|
|
+ REQUIRED: 'This field is required',
|
|
|
+ pattern: (pattern: string) => `Value must match pattern: ${pattern}`,
|
|
|
+ min: (min: number) => `Value must be at least ${min}`,
|
|
|
+ max: (max: number) => `Value must be at most ${max}`,
|
|
|
+ dateAfter: (date: string) => `Date must be after ${date}`,
|
|
|
+ dateBefore: (date: string) => `Date must be before ${date}`,
|
|
|
+} as const;
|
|
|
+
|
|
|
function mapGraphQLCustomFieldToConfig(field: StructField) {
|
|
|
const { __typename, ...rest } = field;
|
|
|
const baseConfig: CustomFieldConfig = {
|
|
|
@@ -88,8 +101,8 @@ function createDateValidationSchema(minDate: Date | undefined, maxDate: Date | u
|
|
|
|
|
|
const dateMinString = minDate?.toLocaleDateString() ?? '';
|
|
|
const dateMaxString = maxDate?.toLocaleDateString() ?? '';
|
|
|
- const dateMinMessage = minDate ? `Date must be after ${dateMinString}` : '';
|
|
|
- const dateMaxMessage = maxDate ? `Date must be before ${dateMaxString}` : '';
|
|
|
+ const dateMinMessage = minDate ? VALIDATION_MESSAGES.dateAfter(dateMinString) : '';
|
|
|
+ const dateMaxMessage = maxDate ? VALIDATION_MESSAGES.dateBefore(dateMaxString) : '';
|
|
|
|
|
|
return baseSchema.refine(
|
|
|
val => {
|
|
|
@@ -119,7 +132,7 @@ function createStringValidationSchema(pattern?: string | null): ZodType {
|
|
|
let schema = z.string();
|
|
|
if (pattern) {
|
|
|
schema = schema.regex(new RegExp(pattern), {
|
|
|
- message: `Value must match pattern: ${pattern}`,
|
|
|
+ message: VALIDATION_MESSAGES.pattern(pattern),
|
|
|
});
|
|
|
}
|
|
|
return schema;
|
|
|
@@ -137,12 +150,12 @@ function createNumberValidationSchema(min?: number | null, max?: number | null):
|
|
|
let schema = z.number();
|
|
|
if (min != null) {
|
|
|
schema = schema.min(min, {
|
|
|
- message: `Value must be at least ${min}`,
|
|
|
+ message: VALIDATION_MESSAGES.min(min),
|
|
|
});
|
|
|
}
|
|
|
if (max != null) {
|
|
|
schema = schema.max(max, {
|
|
|
- message: `Value must be at most ${max}`,
|
|
|
+ message: VALIDATION_MESSAGES.max(max),
|
|
|
});
|
|
|
}
|
|
|
return schema;
|
|
|
@@ -388,7 +401,9 @@ export function getZodTypeFromField(field: FieldInfo): ZodTypeAny {
|
|
|
case 'String':
|
|
|
case 'ID':
|
|
|
case 'DateTime':
|
|
|
- zodType = field.nullable ? z.string() : z.string().min(1, 'This field is required');
|
|
|
+ zodType = field.nullable
|
|
|
+ ? z.string()
|
|
|
+ : z.string().min(1, { message: VALIDATION_MESSAGES.REQUIRED });
|
|
|
break;
|
|
|
case 'Int':
|
|
|
case 'Float':
|