"use strict";
Object.defineProperty(exports, "__esModule", {
    value: true
});
function _export(target, all) {
    for(var name in all)Object.defineProperty(target, name, {
        enumerable: true,
        get: Object.getOwnPropertyDescriptor(all, name).get
    });
}
_export(exports, {
    get Validator () {
        return Validator;
    },
    get commonSchemas () {
        return commonSchemas;
    }
});
const _zod = require("zod");
const _errors = require("./errors");
let Validator = class Validator {
    /**
   * 验证字符串
   */ static validateString(value, rule, fieldName = '字段') {
        const errors = [];
        // 类型检查
        if (rule.required && (value === undefined || value === null || value === '')) {
            errors.push(`${fieldName} 是必填项`);
            return {
                valid: false,
                errors
            };
        }
        if (value === undefined || value === null || value === '') {
            return {
                valid: true,
                errors: []
            };
        }
        if (typeof value !== 'string') {
            errors.push(`${fieldName} 必须是字符串类型`);
            return {
                valid: false,
                errors
            };
        }
        // 长度检查
        if (rule.minLength !== undefined && value.length < rule.minLength) {
            errors.push(`${fieldName} 长度不能少于 ${rule.minLength} 个字符`);
        }
        if (rule.maxLength !== undefined && value.length > rule.maxLength) {
            errors.push(`${fieldName} 长度不能超过 ${rule.maxLength} 个字符`);
        }
        // 正则表达式检查
        if (rule.pattern && !rule.pattern.test(value)) {
            errors.push(`${fieldName} 格式不正确`);
        }
        return {
            valid: errors.length === 0,
            errors
        };
    }
    /**
   * 验证数字
   */ static validateNumber(value, rule, fieldName = '字段') {
        const errors = [];
        // 类型检查
        if (rule.required && (value === undefined || value === null)) {
            errors.push(`${fieldName} 是必填项`);
            return {
                valid: false,
                errors
            };
        }
        if (value === undefined || value === null) {
            return {
                valid: true,
                errors: []
            };
        }
        if (typeof value !== 'number' || isNaN(value)) {
            errors.push(`${fieldName} 必须是数字类型`);
            return {
                valid: false,
                errors
            };
        }
        // 范围检查
        if (rule.min !== undefined && value < rule.min) {
            errors.push(`${fieldName} 不能小于 ${rule.min}`);
        }
        if (rule.max !== undefined && value > rule.max) {
            errors.push(`${fieldName} 不能大于 ${rule.max}`);
        }
        return {
            valid: errors.length === 0,
            errors
        };
    }
    /**
   * 验证对象（使用 Zod Schema）
   */ static validateObject(schema, data) {
        try {
            const result = schema.safeParse(data);
            if (result.success) {
                return {
                    valid: true,
                    data: result.data,
                    errors: []
                };
            } else {
                return {
                    valid: false,
                    errors: result.error.issues.map((err)=>`${err.path.join('.')}: ${err.message}`)
                };
            }
        } catch (error) {
            return {
                valid: false,
                errors: [
                    error instanceof Error ? error.message : '验证失败'
                ]
            };
        }
    }
    /**
   * 验证并抛出错误
   */ static validateOrThrow(schema, data, context) {
        const result = this.validateObject(schema, data);
        if (!result.valid) {
            throw new _errors.ValidationError(result.errors.join('; '), context);
        }
        return result.data;
    }
    /**
   * 验证 URL
   */ static validateUrl(value, fieldName = 'URL') {
        const errors = [];
        if (value === undefined || value === null || value === '') {
            errors.push(`${fieldName} 是必填项`);
            return {
                valid: false,
                errors
            };
        }
        if (typeof value !== 'string') {
            errors.push(`${fieldName} 必须是字符串类型`);
            return {
                valid: false,
                errors
            };
        }
        try {
            new URL(value);
        } catch  {
            errors.push(`${fieldName} 格式不正确`);
        }
        return {
            valid: errors.length === 0,
            errors
        };
    }
    /**
   * 验证邮箱
   */ static validateEmail(value, fieldName = '邮箱') {
        const emailPattern = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
        return this.validateString(value, {
            required: true,
            pattern: emailPattern
        }, fieldName);
    }
};
const commonSchemas = {
    /**
   * 非空字符串
   */ nonEmptyString: _zod.z.string().min(1, '不能为空'),
    /**
   * 正整数
   */ positiveInteger: _zod.z.number().int().positive(),
    /**
   * 非负整数
   */ nonNegativeInteger: _zod.z.number().int().nonnegative(),
    /**
   * URL
   */ url: _zod.z.string().url('URL 格式不正确'),
    /**
   * 邮箱
   */ email: _zod.z.string().email('邮箱格式不正确')
};

//# sourceMappingURL=validation.js.map