Laravel 学习笔记之 model validation

在对database进行写操做前,须要对数据进行validation,如type-check 每个 model column 的定义('type' 这个column必须是enum('card','loan')),这里使用model event来作。数据库

在EventServiceProvider(或自定义一个ValidationServiceProvider)中写上:app

public function boot()
{
        /**
         * Inspired by @see \Illuminate\Foundation\Providers\FormRequestServiceProvider::boot()
         *
         * Note: saving event is always triggered before creating and updating events
         */
        $this->app['events']->listen('eloquent.saving: *', function (string $event_name, array $data): void {
            /** @var \App\Extensions\Illuminate\Database\Eloquent\Model $object */
            $object = $data[0];
            
            $object->validate();
        });
}

'eloquent.saving: *'是表示listen全部model的saving,即任何一个model的写操做都会触发该事件。ide

而后写一个abstract model extends EloquentModel:ui

// \App\Extensions\Illuminate\Database\Eloquent\Model

use Illuminate\Database\Eloquent\Model as EloquentModel;
use Illuminate\Validation\ValidationException;

abstract class Model extends EloquentModel
{
    public function validate():void
    {
        // 1. validate type rules (type-check)
        $validator = $this->getTypeValidator();
        
        if ($validator->fails()) {
            throw new ValidationException($validator);
        }
        
        // $validator = $this->getConstraintValidator();
        // 2. validate constraint rules (sanity-check)
    }

    protected function getTypeValidator()
    {
        return $this->getValidationFactory()->make($this->attributes, static::COLUMN_TYPE_RULES);
    }
    
    protected function getValidationFactory()
    {
        return app(Factory::class);
    }
    
    protected function getConstraintValidator()
    {
        // return $this->getValidationFactory()->make($attributes, static::COLUMN_CONSTRAINT_RULES);
    } 
}

这样,在每个继承abstract model的子类中,定义const COLUMN_TYPE_RULES就行,如:this

class Account extends Model
{
    public const COLUMN_TYPE_RULES = [
        'id' => 'integer|between:0,4294967295',
        'source' => 'nullable|in:schwab,orion,yodlee',
        'type' => 'required|in:bank,card,loan',
    ];
}

在写操做时,提早对每个 model 的 schema definition进行type-check,避免无效碰撞 database。这个feature的目的是从model schema去校验输入数据的字段定义是否合法。
另一般除了type-check schema definition 外,还得根据业务须要进行逻辑校验sanity-check constraint rules,如当建立一个account时,输入inputs里的字段person_id不能是child未成年人,等等。这里业务不一样,constraint rules不一样,不作过多解释。这个feature的目的主要是从逻辑上校验输入数据的合法性。code

OK,总之通常状况下,在写数据库前都须要作 model validation,避免无效hit db。orm

相关文章
相关标签/搜索