threeleaf/validation-engine 问题修复 & 功能扩展

解决BUG、新增功能、兼容多环境部署,快速响应你的开发需求

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

threeleaf/validation-engine

最新稳定版本:2.0.0

Composer 安装命令:

composer require threeleaf/validation-engine

包简介

A Laravel Eloquent Model for managing dynamic validation engine rules.

README 文档

README

A Laravel library for managing dynamic validation rules and configurations.

Compatible with Laravel 12 and PHP 8.2+

Overview

The ValidationEngine library provides a robust solution for managing validation rules and configurations in a dynamic manner. It allows you to define validators that group multiple rules and apply them based on various criteria, such as time and status. This library is particularly useful for scenarios where validation logic needs to be customized or adjusted without directly modifying the codebase.

ValidationEngine

Latest Stable Version GitHub last commit Build Status Coverage PHP Version License Total Downloads

Installation

Install the library via Composer:

composer require threeleaf/validation-engine

Run the migrations to create the necessary tables:

php artisan migrate

Usage

Setting Up and Use Validators

To create a new validator and associate rules with it, use the Validator and Rule models:

use Illuminate\Container\Container;
use Illuminate\Foundation\Testing\RefreshDatabase;
use Illuminate\Support\Facades\Log;
use Illuminate\Support\Facades\Validator as LaravelValidator;
use Tests\Feature\TestCase;
use ThreeLeaf\ValidationEngine\Enums\ActiveStatus;
use ThreeLeaf\ValidationEngine\Models\Rule;
use ThreeLeaf\ValidationEngine\Models\Validator;
use ThreeLeaf\ValidationEngine\Models\ValidatorRule;
use ThreeLeaf\ValidationEngine\Rules\EnumRule;

/* Create a new validator */
$newValidator = Validator::create([
    'name' => 'StateAndTimeValidator',
    'description' => 'Validates state and checks time for Monday business hours.',
    'active_status' => ActiveStatus::ACTIVE,
]);

/* Create a rule */
$newRule = Rule::create([
    'attribute' => 'active_status',
    'rule_type' => EnumRule::class,
    'parameters' => json_encode([
        'enumClass' => 'ThreeLeaf\\ValidationEngine\\Enums\\ActiveStatus',
        'allowedValues' => [ActiveStatus::ACTIVE],
    ]),
]);

/* Associate the rule with the validator */
ValidatorRule::create([
    'validator_id' => $newValidator->validator_id,
    'rule_id' => $newRule->rule_id,
    'order_number' => 1,
    'active_status' => ActiveStatus::INACTIVE,
]);

/* Retrieve the validator */
$validator = Validator::where('name', 'StateAndTimeValidator')->first();

/* Extract the rule */
$rule = $validator->rules->first->get();

/* Retrieve the rule parameters */
$parameters = json_decode($rule->parameters, true);
$compiledRules = [
    $rule->attribute => [Container::getInstance()->makeWith(EnumRule::class, $parameters)],
];

/* Serialize the value you want to validate. */
$data = ['active_status' => ActiveStatus::ACTIVE->value];

/* Create the validator */
$validator = LaravelValidator::make($data, $compiledRules);

if ($validator->passes()) {
    Log::info('Success!');
}

Contributing

Contributions are welcome! Please submit a pull request or open an issue to discuss your ideas.

License

This library is open-sourced software licensed under the GPL-3.0+.

Miscellaneous

OpenApi Documentation

OpenAPI documentation can be generated within the application using the command:

php util/generate-swagger.php

Generate Coverage Badge

After running the tests with code coverage, run the following script to update the coverage badge:

php util/generate-coverage-badge.php

统计信息

  • 总下载量: 118
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 0
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 0
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: GPL-3.0
  • 更新时间: 2024-10-10