Skip to content

Commit 358ba2d

Browse files
committed
fix(validator): tighten pipeline callable handling
1 parent 8fc5acf commit 358ba2d

2 files changed

Lines changed: 14 additions & 10 deletions

File tree

src/Lemmon/Validator/FieldValidator.php

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -96,15 +96,13 @@ public function satisfies(
9696
callable|FieldValidator $validation,
9797
null|string $message = null,
9898
): self {
99-
$rule = $validation;
100-
101-
if ($validation instanceof FieldValidator) {
99+
$rule = $validation instanceof FieldValidator
102100
// Convert FieldValidator to callable
103-
$rule = static function ($value, $key = null, $input = null) use ($validation) {
101+
? static function ($value, $key = null, $input = null) use ($validation) {
104102
[$valid] = $validation->tryValidate($value, $key, $input);
105103
return $valid;
106-
};
107-
}
104+
}
105+
: $validation;
108106

109107
$this->pipeline[] = [
110108
'type' => PipelineType::VALIDATION,
@@ -353,7 +351,7 @@ public function tryValidate(mixed $value, string $key = '', mixed $input = null)
353351
foreach ($this->pipeline as $step) {
354352
$operation = $step['operation'];
355353
$type = $step['type'];
356-
$skipNull = $step['skipNull'] ?? false;
354+
$skipNull = $step['skipNull'];
357355

358356
// Smart null handling: validations always skip null, transformations skip based on skipNull flag
359357
// pipe() transformations skip null (type-preserving, expect specific type)
@@ -492,13 +490,16 @@ public function __clone()
492490
foreach ($this->pipeline as $step) {
493491
$operation = $step['operation'];
494492
if ($operation instanceof \Closure) {
495-
$operation = $operation->bindTo($this);
493+
$boundOperation = $operation->bindTo($this);
494+
if ($boundOperation !== null) {
495+
$operation = $boundOperation;
496+
}
496497
}
497498

498499
$rebuiltPipeline[] = [
499500
'type' => $step['type'],
500501
'operation' => $operation,
501-
'skipNull' => $step['skipNull'] ?? false,
502+
'skipNull' => $step['skipNull'],
502503
];
503504
}
504505

src/Lemmon/Validator/ValidationException.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ public function __construct(
1313
private array $errors,
1414
) {
1515
$message = json_encode($errors, JSON_PRETTY_PRINT);
16-
parent::__construct($message ?? 'JSON encoding of errors failed');
16+
if ($message === false) {
17+
$message = 'JSON encoding of errors failed';
18+
}
19+
parent::__construct($message);
1720
}
1821

1922
/**

0 commit comments

Comments
 (0)