UNPKG

guardz-event

Version:

Type-safe event handling with runtime validation using guardz for unsafe data from 3rd parties

71 lines 2.53 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.GuardzValidator = void 0; const guardz_1 = require("guardz"); /** * Guardz-specific validation service */ class GuardzValidator { /** * Validate data using a type guard with optional tolerance mode */ static validate(data, guard, config = {}) { try { if (config.tolerance) { const validationResult = (0, guardz_1.guardWithTolerance)(data, guard, { identifier: config.identifier || 'data', callbackOnError: (error) => { if (config.onError) { config.onError(error, { type: 'validation', eventType: config.eventType || 'unknown' }); } } }); if (validationResult) { return { isValid: true, data: data }; } else { return { isValid: false, error: `Validation failed: ${config.identifier || 'data'}` }; } } else { const validationResult = guard(data); if (validationResult) { return { isValid: true, data: data }; } else { return { isValid: false, error: `Validation failed: ${config.identifier || 'data'}` }; } } } catch (error) { const errorMessage = `Validation error: ${error instanceof Error ? error.message : 'Unknown error'}`; if (config.onError) { config.onError(errorMessage, { type: 'unknown', eventType: config.eventType || 'unknown', originalError: error }); } return { isValid: false, error: errorMessage }; } } /** * Create a validation function with pre-configured settings */ static createValidator(guard, defaultConfig = {}) { return (data, config = {}) => { return this.validate(data, guard, { ...defaultConfig, ...config }); }; } } exports.GuardzValidator = GuardzValidator; //# sourceMappingURL=GuardzValidator.js.map