UNPKG

@letsparky/api-v2-client

Version:

TypeScript client for the LetsParky API V2

181 lines (180 loc) 6.06 kB
import { CreateDeviceRequest, UpdateDeviceRequest, WebhookCreateParams, WebhookUpdateParams, ParkingCreateParams, ParkingUpdateParams, UserUpdateParams, MembershipCreateParams, MembershipUpdateParams, Environment, UUID } from './types'; /** * Comprehensive validation utilities for the LetsParky API client. * * This class provides static methods for validating various types of data * before sending requests to the API. It includes environment-aware validation * that can be more lenient in development/test modes. * * Key features: * - Parameter validation for all API operations * - Environment-aware validation (test mode support) * - Detailed error messages with field-specific feedback * - UUID format validation with test ID support * - Location coordinate validation * - Pagination parameter validation * * @example * ```typescript * // Validate device creation parameters * try { * Validators.validateDeviceCreate({ * name: 'Sensor 1', * type: 'ultrasonic', * location: { lat: 40.7128, lng: -74.0060 } * }); * } catch (error) { * if (error instanceof ValidationError) { * console.log('Validation errors:', error.validationErrors); * } * } * * // Set environment for test mode * Validators.setEnvironment(Environment.DEVELOPMENT); * Validators.setTestMode(true); * ``` * * @since 1.0.0 */ export declare class Validators { private static currentEnvironment; private static isTestMode; /** * Sets the current environment for validation. * * Different environments may have different validation rules. * For example, development environment allows mock IDs. * * @param environment - The environment to set * * @example * ```typescript * // Enable development mode validation * Validators.setEnvironment(Environment.DEVELOPMENT); * * // Switch to production mode (strict validation) * Validators.setEnvironment(Environment.PRODUCTION); * ``` * * @since 1.0.0 */ static setEnvironment(environment: Environment): void; /** * Enables or disables test mode for validation. * * In test mode, validation is more lenient to allow for testing * with mock data. For example, mock IDs starting with 'mock-' or 'test-' * are accepted instead of requiring strict UUID format. * * @param enabled - Whether test mode should be enabled * * @example * ```typescript * // Enable test mode for unit tests * Validators.setTestMode(true); * * // Disable test mode for production * Validators.setTestMode(false); * ``` * * @since 1.0.0 */ static setTestMode(enabled: boolean): void; /** * Validates device creation parameters. * * Ensures all required fields are present and valid for creating a new device. * Validates location coordinates if provided and tenant ID format. * * @param params - The device creation parameters to validate * @param params.name - Device name (required, non-empty) * @param params.type - Device type (required, non-empty) * @param params.description - Optional device description * @param params.location - Optional location with lat/lng coordinates * @param params.is_public - Optional public accessibility flag * @param params.tenant_id - Optional tenant ID (must be valid UUID) * * @throws {ValidationError} When validation fails, with detailed field-specific errors * * @example * ```typescript * // Valid device creation * Validators.validateDeviceCreate({ * name: 'Parking Sensor 1', * type: 'ultrasonic', * location: { lat: 40.7128, lng: -74.0060 }, * is_public: true * }); * * // This will throw ValidationError * try { * Validators.validateDeviceCreate({ * name: '', // Empty name * type: 'sensor', * location: { lat: 200, lng: -74.0060 } // Invalid latitude * }); * } catch (error) { * console.log(error.validationErrors); * // { name: ['Device name is required'], location: ['Latitude must be between -90 and 90'] } * } * ``` * * @since 1.0.0 */ static validateDeviceCreate(params: CreateDeviceRequest): void; /** * Validates device update parameters */ static validateDeviceUpdate(params: UpdateDeviceRequest): void; /** * Validates webhook creation parameters */ static validateWebhookCreate(params: WebhookCreateParams): void; /** * Validates webhook update parameters */ static validateWebhookUpdate(params: WebhookUpdateParams): void; /** * Validate an ID string (UUID format) * @param id The ID to validate * @param entityName The name of the entity for the error message */ static validateId(id: UUID, entityName?: string): void; /** * Check if an ID is valid (allows test IDs in development mode) * @param id The ID to check * @returns True if the ID is valid */ private static isValidId; /** * Validates parking creation parameters */ static validateParking(params: ParkingCreateParams): void; /** * Validates parking update parameters */ static validateParkingUpdate(params: ParkingUpdateParams): void; /** * Validates user update parameters */ static validateUserUpdate(params: UserUpdateParams): void; /** * Validate pagination parameters */ static validatePaginationParams(params: { page?: number; limit?: number; }): void; /** * Validate location coordinates */ static validateLocation(lat: number, lng: number): void; /** * Validates membership creation parameters */ static validateMembershipCreate(params: MembershipCreateParams): void; /** * Validates membership update parameters */ static validateMembershipUpdate(params: MembershipUpdateParams): void; }