Validierung Part II, neue Komponenten

This commit is contained in:
2024-08-01 22:43:32 +02:00
parent 2955c034a0
commit 29f88d610f
18 changed files with 1431 additions and 242 deletions

View File

@@ -19,6 +19,9 @@
// customerSubType?: 'broker' | 'cpa' | 'attorney' | 'titleCompany' | 'surveyor' | 'appraiser';
// created?: Date;
// updated?: Date;
import { z } from 'zod';
// }
export interface UserData {
id?: string;
@@ -46,30 +49,108 @@ export type Gender = 'male' | 'female';
export type CustomerType = 'buyer' | 'professional';
export type CustomerSubType = 'broker' | 'cpa' | 'attorney' | 'titleCompany' | 'surveyor' | 'appraiser';
export type ListingsCategory = 'commercialProperty' | 'business';
export interface User {
id: string; // UUID as a string
firstname: string;
lastname: string;
email: string;
phoneNumber?: string;
description?: string;
companyName?: string;
companyOverview?: string;
companyWebsite?: string;
companyLocation?: string;
offeredServices?: string;
areasServed?: AreasServed[];
hasProfile?: boolean;
hasCompanyLogo?: boolean;
licensedIn?: LicensedIn[];
gender?: Gender;
customerType?: CustomerType;
customerSubType?: CustomerSubType;
created?: Date;
updated?: Date;
latitude?: number;
longitude?: number;
}
// export interface User {
// id: string; // UUID as a string
// firstname: string;
// lastname: string;
// email: string;
// phoneNumber?: string;
// description?: string;
// companyName?: string;
// companyOverview?: string;
// companyWebsite?: string;
// companyLocation?: string;
// offeredServices?: string;
// areasServed?: AreasServed[];
// hasProfile?: boolean;
// hasCompanyLogo?: boolean;
// licensedIn?: LicensedIn[];
// gender?: Gender;
// customerType?: CustomerType;
// customerSubType?: CustomerSubType;
// created?: Date;
// updated?: Date;
// latitude?: number;
// longitude?: number;
// }
// export interface AreasServed {
// county: string;
// state: string;
// }
// export interface LicensedIn {
// registerNo: string;
// state: string;
// }
// --------------------------------
//
// --------------------------------
export const GenderEnum = z.enum(['male', 'female']);
export const CustomerTypeEnum = z.enum(['buyer', 'professional']);
export const CustomerSubTypeEnum = z.enum(['broker', 'cpa', 'attorney', 'titleCompany', 'surveyor', 'appraiser']);
export const ListingsCategoryEnum = z.enum(['commercialProperty', 'business']);
export const AreasServedSchema = z.object({
county: z.string().nonempty('County is required'),
state: z.string().nonempty('State is required'),
});
export const LicensedInSchema = z.object({
registerNo: z.string().nonempty('Registration number is required'),
state: z.string().nonempty('State is required'),
});
export const UserSchema = z
.object({
id: z.string().uuid('Invalid ID format. Must be a valid UUID').optional(),
firstname: z.string().min(2, 'First name must be at least 2 characters long'),
lastname: z.string().min(2, 'Last name must be at least 2 characters long'),
email: z.string().email('Invalid email address'),
phoneNumber: z.string().optional().nullable(),
description: z.string().min(10, 'Description must be at least 10 characters long').optional().nullable(),
companyName: z.string().optional().nullable(),
companyOverview: z.string().min(10, 'Company overview must be at least 10 characters long').optional().nullable(),
companyWebsite: z.string().url('Invalid company website URL').optional().nullable(),
companyLocation: z.string().optional().nullable(), // Additional validation for US locations could be implemented here
offeredServices: z.string().min(10, 'Offered services must be at least 10 characters long').optional().nullable(),
areasServed: z.array(AreasServedSchema).optional().nullable(),
hasProfile: z.boolean().optional().nullable(),
hasCompanyLogo: z.boolean().optional().nullable(),
licensedIn: z.array(LicensedInSchema).optional().nullable(),
gender: GenderEnum.optional().nullable(),
customerType: CustomerTypeEnum.optional().nullable(),
customerSubType: CustomerSubTypeEnum.optional().nullable(),
created: z.date().optional().nullable(),
updated: z.date().optional().nullable(),
latitude: z.number().optional().nullable(),
longitude: z.number().optional().nullable(),
})
.refine(
data => {
if (data.customerType === 'professional') {
return !!data.customerSubType && !!data.phoneNumber && !!data.companyOverview && !!data.description && !!data.offeredServices && !!data.companyLocation && data.areasServed && data.areasServed.length > 0;
}
return true;
},
{
message: 'For professional customers, additional fields are required: customer subtype, phone number, company overview, description, offered services, company location, and at least one area served',
path: ['customerType'],
},
)
.refine(
data => {
if (data.customerType === 'professional') {
return /\(\d{3}\) \d{3}-\d{4}$/.test(data.phoneNumber || '');
}
return true;
},
{
message: 'Phone number must be in US format: +1 (XXX) XXX-XXXX',
path: ['phoneNumber'],
},
);
export type AreasServed = z.infer<typeof AreasServedSchema>;
export type LicensedIn = z.infer<typeof LicensedInSchema>;
export type User = z.infer<typeof UserSchema>;
export interface BusinessListing {
id: string; // UUID as a string
email: string; // References users.email
@@ -131,11 +212,3 @@ export interface CommercialPropertyListing {
longitude?: number; // double precision
// embedding?: number[]; // Uncomment if needed for vector embedding
}
export interface AreasServed {
county: string;
state: string;
}
export interface LicensedIn {
registerNo: string;
state: string;
}