diff --git a/src/Validators/Dictionary.php b/src/Validators/Dictionary.php new file mode 100644 index 0000000..4ba0dd9 --- /dev/null +++ b/src/Validators/Dictionary.php @@ -0,0 +1,50 @@ + $this->validateArray($decoded), + $decoded instanceof stdClass => $this->validateObject($decoded), + }; + } + + private function validateArray(array $decoded) + { + foreach ($decoded as $key => $value) { + // if in required, force validation + // if in optional, still force validation + // if in neither, examine $rejectUnexpected + } + // check if any optional values are not yet in the output & apply their + // default values + } + + private function validateObject(stdClass $decoded) + { + } + + public function getDefaultValue() + { + } +} diff --git a/src/Validators/ListOf.php b/src/Validators/ListOf.php new file mode 100644 index 0000000..056bcf4 --- /dev/null +++ b/src/Validators/ListOf.php @@ -0,0 +1,38 @@ + + * @template Item + */ +class ListOf implements ValidatorInterface +{ + /** + * @param ValidatorInterface $validator + */ + public function __construct(private ValidatorInterface $validator) + { + } + + public function validate(mixed $decoded): Result + { + if (!is_iterable($decoded)) { + return Result::error('Value is not convertable to a list'); + } + // Normally array_map would be preferred here, but this avoids having + // to do any additional type juggling (for non-array iterables) or + // re-keying (for ensuring array_is_list(result)) + $output = []; + foreach ($decoded as $item) { + $output[] = $this->validator->validate($item); + } + return Result::ok( + array_map(fn ($item) => $item->unwrap(), $output) + ); + } +} diff --git a/src/Validators/Nullable.php b/src/Validators/Nullable.php new file mode 100644 index 0000000..e9f8f8f --- /dev/null +++ b/src/Validators/Nullable.php @@ -0,0 +1,27 @@ + + */ +class Nullable implements ValidatorInterface +{ + /** + * @param ValidatorInterface $validator + */ + public function __construct(private ValidatorInterface $validator) + { + } + + public function validate(mixed $decoded): Result + { + if ($decoded === null) { + return Result::ok(null); + } + return $this->validator->validate($decoded); + } +}