Something went wrong. Try again.
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041<?php
declare(strict_types=1);
namespace Wotnak\Atproto\Lexicon\Schema;
use Wotnak\Atproto\Lexicon\ValidationContext;
/** * Validates objects with a required Lexicon type discriminator. * * @api */final class TypedObjectSchema extends AbstractSchema {
public function __construct( private readonly string $type, private readonly ObjectSchema $object, ) {}
/** * Validates the value. */ public function validateValue(mixed $value, ValidationContext $context): mixed { $nativeObject = $value instanceof \stdClass; $object = $nativeObject ? \get_object_vars($value) : $value; if (!\is_array($object) || !$nativeObject && \array_is_list($object)) { $context->addViolation('Expected object.', 'invalid_type'); return $value; } if (\array_key_exists('$type', $object) && $object['$type'] !== $this->type) { $context->withPath('$type', fn (ValidationContext $child) => $child->addViolation( \sprintf('Expected "%s".', $this->type), 'type_mismatch', )); } return $this->object->validateValue($value, $context); }
}