当前位置: 首页 > news >正文

【Laravel】 Laravel 智能验证规则生成器

Laravel 智能验证规则生成器:企业级增强方案

<?phpnamespace App\Services\Validation;use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Validator;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use InvalidArgumentException;class SchemaDrivenValidator
{protected $config;protected $schemaCacheKey = 'database_schema_v2';protected $connection;protected $databaseDriver;protected $customRules = [];protected $ignoreRules = [];protected $tableAliases = [];protected $currentTable;protected $nestedValidators = [];public function __construct(){$this->loadConfig();$this->connection = config('database.default');$this->databaseDriver = config("database.connections.{$this->connection}.driver");}/*** 加载配置*/protected function loadConfig(){$this->config = config('validation', ['cache_ttl' => 86400,'auto_relation' => true,'strict_types' => false,'default_string_max' => 255,'password_min_length' => 8,'image_max_size' => 2048,'custom_rules' => [],'ignore_fields' => [],'table_aliases' => [],'type_mappings' => ['int' => 'integer','varchar' => 'string','text' => 'string','datetime' => 'date','timestamp' => 'date','json' => 'array',],'special_field_handlers' => ['email' => ['email'],'phone' => ['phone'],'password' => ['min::password_min_length', 'confirmed'],'image' => ['image', 'mimes:jpg,png,jpeg', 'max::image_max_size'],'_at' => ['date'],'_image' => ['image', 'mimes:jpg,png,jpeg', 'max::image_max_size'],'_avatar' => ['image', 'mimes:jpg,png,jpeg', 'max::image_max_size'],]]);$this->customRules = $this->config['custom_rules'] ?? [];$this->ignoreRules = $this->config['ignore_fields'] ?? [];$this->tableAliases = $this->config['table_aliases'] ?? [];}/*** 获取数据库元数据*/public function getDatabaseSchema(bool $forceRefresh = false){$cacheKey = $this->schemaCacheKey . '_' . $this->connection;if ($forceRefresh) {Cache::forget($cacheKey);}return Cache::remember($cacheKey, $this->config['cache_ttl'], function () {return $this->fetchDatabaseSchema();});}/*** 从数据库获取元数据*/protected function fetchDatabaseSchema(){switch ($this->databaseDriver) {case 'mysql':return $this->getMysqlSchema();case 'pgsql':return $this->getPostgresSchema();case 'sqlite':return $this->getSqliteSchema();default:throw new InvalidArgumentException("Unsupported database driver: {$this->databaseDriver}");}}/*** MySQL 元数据获取*/protected function getMysqlSchema(){$databaseName = config("database.connections.{$this->connection}.database");$columns = DB::connection($this->connection)->table('information_schema.columns')->select('table_name','column_name','column_comment','data_type','character_maximum_length','is_nullable','column_default','column_type','numeric_precision','numeric_scale','extra')->where('table_schema', $databaseName)->get();$indexes = DB::connection($this->connection)->table('information_schema.statistics')->select('table_name','column_name','index_name','non_unique')->where('table_schema', $databaseName)->get();$foreignKeys = DB::connection($this->connection)->table('information_schema.key_column_usage')->select('table_name','column_name','referenced_table_name','referenced_column_name')->where('table_schema', $databaseName)->whereNotNull('referenced_table_name')->get();return $this->processSchemaData($columns, $indexes, $foreignKeys);}/*** PostgreSQL 元数据获取*/protected function getPostgresSchema(){$schema = config("database.connections.{$this->connection}.schema", 'public');$columns = DB::connection($this->connection)->table('information_schema.columns')->select('table_name','column_name','column_default','is_nullable','data_type','character_maximum_length','numeric_precision','numeri
http://www.dtcms.com/a/265983.html

相关文章:

  • Java操作word实战
  • LiteHub中间件之跨域访问CORS
  • P2392 kkksc03考前临时抱佛脚(动态规划)
  • 纯前端批量下载
  • Python 爬虫实战 | 国家医保
  • MySQL 8.0 OCP 1Z0-908 题目解析(16)
  • Part 0:射影几何,变换与估计-第三章:3D射影几何与变换
  • 爬虫经验分享:淘宝整店商品爬取全过程|API接口实战
  • 【数据结构】 map 和 set
  • stm32第十三天串口发送数据
  • 从0到1实战!用Docker部署Qwerty Learner输入法的完整实践过程
  • Dijkstra 算法#图论
  • MySQL JSON数据类型完全指南:从版本演进到企业实践的深度对话
  • Windows 上使用 vscode + mingw 调试 python 程序
  • 国内MCP服务平台推荐!aibase.cn上线MCP服务器集合平台
  • 二叉树的右视图C++
  • MySQL的窗口函数介绍
  • 每日算法刷题Day41 6.28:leetcode前缀和2道题,用时1h20min(要加快)
  • golang json omitempty 标签研究
  • 图论基础算法入门笔记
  • OOM电商系统订单缓存泄漏,这是泄漏还是溢出
  • 免费一键自动化申请、续期、部署、监控所有 SSL/TLS 证书,ALLinSSL开源免费的 SSL 证书自动化管理平台
  • 【进阶篇-消息队列】——RocketMQ如何实现事务的
  • HarmonyOS ArkTS卡片堆叠滑动组件实战与原理详解(含源码)
  • 如何挖掘客户的隐性需求
  • 474. 一和零
  • 【华为od刷题(C++)】HJ22 汽水瓶
  • ubuntu22 桌面版开启root登陆
  • ubuntu22.04安装anaconda
  • embbding you should know