Handle the Images feature, which is special because you define the rules
on the parent model.

@param  BaseModel $data
@param  array     $rules A Laravel rules array. If null, will be pulled from model
@return array
324080
public function handleImageRules($model, $rules)
    {

        // If the model is an image, the rules were passed in by NestedModels
        // and are good to go
        if (is_a($model, Image::class)) {
            return $rules;
        }

        // Otherwise, remove any image rules because this is a non-Image and
        // these rules will get applied by NestedModels
        return array_where($rules, function ($val, $key) {
            return !starts_with($key, 'images.');
        });
    }
364722
private function evaluateRequestParams($data, $ruleModel)
    {
        foreach ($data as $key => $value) {
            if ($key !== 'form_key' && $key !== 'key' && $key !== 'active_tab') {
                if ($key == 'condition') {
                    if (is_array($value)) {
                        unset($value['__empty']);
                    }
                    $ruleModel->setData('conditions', $value);
                } else {
                    $ruleModel->setData($key, $value);
                }
            }
        }

        return $ruleModel;
    }
302407
private function prepareRecord(array $data, array $rules, array $messages)
    {
        // Reset error bag
        $this->errors = new MessageBag;

        // Capture data
        $this->setDataset($data);
        $this->setRuleset($rules);
        $this->setMessageset($messages);

        // Fill model
        $this->fillModel();
    }
255200
public function setRules(array $rules)
    {
        $this->rules = [];

        foreach ($rules as $rule) {
            $controller  = strtolower($rule['controller']);
            $actions     = isset($rule['actions']) ? (array)$rule['actions'] : [];
            $permissions = (array)$rule['permissions'];

            if (empty($actions)) {
                $this->rules[$controller][0] = $permissions;
                continue;
            }

            foreach ($actions as $action) {
                $this->rules[$controller][strtolower($action)] = $permissions;
            }
        }
    }
931661
private function parseData(array $data)
    {
        $field = $data['field'];

        foreach ($data['policies'] as $rule => $policy) {
            $passes = call_user_func_array([new Rule(), $rule], [$field, $data['value'], $policy]);

            if (!$passes) {
                ErrorHandler::set(
                    str_replace(
                        [':attribute', ':policy', '_'],
                        [$field, $policy, ' '], $this->messages[$rule]), $field
                );
            }
        }
    }
787856
protected function getValidatorInstance($rules, $data, $model=null)
    {
        $factory = $this->container->make('Illuminate\Validation\Factory');

        if (method_exists($this, 'validatorInstance'))
        {
            return $this->container->call(
                [$this, 'validatorInstance'],
                compact('factory')
            );
        }

        return $factory->make(
            $data, $rules, $this->customMessages(), $this->customAttributes()
        );
    }
651338
protected function validate($data, $rules)
    {
        $data = array_filter($data);

        $validator = $this->validator->make($data, $rules);

        if ($validator->fails()) {
            throw new PagSeguroException($validator->messages()->first(), 1003);
        }
    }
1146236
protected function &handleClassExtension(&$class, &$data, Type $type, $parentName)
    {
        $property = parent::handleClassExtension($class, $data, $type, $parentName);

        $rules = $this->loadValidatorType($property, $type, false );

        $property['validation'] = array_merge(!empty($property['validation']) ? $property['validation'] : [], $rules);
        return $property;
    }
940794
private function validateData($rules, $data) {
		$errors = [];
		foreach ($rules as $field => $rule) {
			$valid = $this->validateField($field, $data, $rule);
			if ($valid !== true) {
				$errors[ $field ] = $valid;
			}
		}
		if (!empty($errors)) {
			throw new ValidateException($errors);
		}

		return true;
	}
508421
public function validateData(array $data, array $rules, array $messages)
    {
        if ($rules) {
            $this->customValidator->setData($data);
            $this->customValidator->setRules($rules);
            $this->customValidator->setCustomMessages($messages);

            //if the validator fails, kick back the errors
            if ($this->customValidator->fails()) {
                return implode('. ', $this->customValidator->messages()->all());
            }
        }

        return true;
    }
598269
public function applyRules(array $rules = [], array $data = []): array
    {
        $data  = $data ?: $this->_data;
        $rules = $rules ?: $this->_rules;
        // save clean data
        $filtered = [];

        foreach ($rules as $rule) {
            if (!isset($rule[0], $rule[1])) {
                continue;
            }

            if (!$fields = $rule[0]) {
                continue;
            }

            $fields = \is_string($fields) ? Filters::explode($fields) : (array)$fields;

            foreach ($fields as $field) {
                if (!isset($data[$field])) {
                    $filtered[$field] = $rule['default'] ?? null;
                } else {
                    $filtered[$field] = $this->valueFiltering($data[$field], $rule[1]);
                }
            }
        }

        return $filtered;
    }
703564
public function setRules(array $rules): void
    {
        foreach ($rules as $key => $fields) {
            foreach ($fields as $fieldName => $fieldOptions) {
                // nested?
                @ $fieldType = $fieldOptions['type'];
                if ($fieldType == self::TYPE_ARRAY) {
                    @ $fieldSpec = $fieldOptions['spec'];
                    if (empty($fieldSpec)) {
                        throw new ValidationException(
                            "For array types, 'spec' field must be a non-empty array (field: {$fieldName})");
                    }
                    foreach ((array) $fieldSpec as $fieldSpecFieldName => $fieldSpecFieldOptions) {
                        $this->rules[$key][$fieldName][$fieldSpecFieldName] = new ValidationRule(
                            $fieldSpecFieldName, $fieldSpecFieldOptions
                        );
                    }
                } else {
                    $this->rules[$key][$fieldName] = new ValidationRule($fieldName, $fieldOptions);
                }
            }
        }
    }
1091187
public function validate(array $data, $rules = null, $custom = false)
    {
        if (!$custom) {
            $rules = $this->rules($rules);
        }

        return $this->validator->make($data, $rules);
    }
442841
public function setPictureUploadRules($rules)
    {
        $this->_pictureUploadRules = [];
        if (!is_array($rules)) {
            return;
        }
        foreach ($rules as $rule) {
            $this->_pictureUploadRules[] = array_merge(['picture'], $rule);
        }
    }
1090969
protected function normalizeValidationRuleSourceData($rules)
    {
        if (false === $rules || empty($rules)) {
            return [];
        }

        if (is_string($rules)) {
            $rules = (array) $rules;
        }

        if ( ! is_array($rules)) {
            throw new UnexpectedValueException("Form field base validation rules not array or arrayable");
        }

        // Loop through and cast anything not already a data object
        $rules = array_map([$this, 'normalizeRulesProperty'], $rules, array_keys($rules));

        // Make sure there is only one entry per rule key
        $rules = $this->getRuleMerger()->collapseRulesForDuplicateKeys($rules);

        // Key the array by the rule key
        return array_combine(
            array_map(function (ValidationRuleDataInterface $rule) {
                return $rule->key() ?: '';
            }, $rules),
            $rules
        );
    }
376900
protected function _validateData($rule, $key, $data)
    {
        if (
            (is_callable($rule) && call_user_func_array($rule, [$key, $data, $this]))
            || @preg_match($rule, $data)
        ) {
            return true;
        }

        if ($rule instanceof Closure) {
            $reflection = new ReflectionFunction($rule);
            $rule       = $reflection->__toString();
        }

        $this->_errorsList[] = [
            'message'   => 'validation_mismatch',
            'key'       => $key,
            'data'      => $data,
            'rule'      => $rule,
        ];
        $this->_hasErrors = true;

        return false;
    }
476810
public function validate(array $data, array $rules, array $messages = [], array $customAttributes = [])
    {
        return $this->make($data, $rules, $messages, $customAttributes)->validate();
    }