Skip to main content
Glama
validateUser.ts2.04 kB
import type { User, UserAPI } from '@/types/user.types'; import { validateEmail } from './validateEmail'; import { validatePhone } from './validatePhone'; import { validateString } from './validateString'; export type UserFields = (keyof User)[]; const defaultFieldsToCheck: UserFields = ['name', 'phone', 'email', 'phone']; export type FieldsToCheck = (typeof defaultFieldsToCheck)[number]; type ValidationErrors = Partial< Record<(typeof defaultFieldsToCheck)[number], string[]> >; export const NAMES_MIN_LENGTH = 4; export const NAMES_MAX_LENGTH = 100; /** * Validates an user object. * @param user The user object to validate. * @returns An object containing the validation errors for each field. */ export const validateUser = ( user: Partial<User | UserAPI>, fieldsToCheck = defaultFieldsToCheck ): ValidationErrors => { const errors: ValidationErrors = {}; // Define the fields to validate const fieldsToValidate = new Set<FieldsToCheck>(fieldsToCheck); const userJson = JSON.parse(JSON.stringify(user)); // Validate each field for (const field of fieldsToValidate) { const value = userJson[field]; // Initialize error array for the field errors[field] = []; // Check for name validity if (field === 'name') { const nameErrors = validateString( value, `User ${field}`, NAMES_MIN_LENGTH, NAMES_MAX_LENGTH ); if (nameErrors.length > 0) { errors[field] = nameErrors; } } // Check for email validity if (field === 'email') { const emailErrors = validateEmail(value, 'User Email'); if (emailErrors.length > 0) { errors[field] = emailErrors; } } if (field === 'phone') { const phoneErrors = validatePhone(value, 'User Phone', 8, 20); if (phoneErrors.length > 0) { errors[field] = phoneErrors; } } // Remove the error field if there are no errors if (errors[field].length === 0) { delete errors[field]; } } return errors; };

Latest Blog Posts

MCP directory API

We provide all the information about MCP servers via our MCP API.

curl -X GET 'https://glama.ai/api/mcp/v1/servers/aymericzip/intlayer'

If you have feedback or need assistance with the MCP directory API, please join our Discord server