safeValidate([])->isValid()); static::assertTrue($schema->safeValidate(['hello', 'world'])->isValid()); $result = $schema->safeValidate(['valid', NULL, 'also valid']); static::assertFalse($result->isValid()); static::assertSame(['$[1]'], \array_column($result->violations, 'path')); } public function testValidatesIntegerAndObjectItems(): void { static::assertTrue( new ArraySchema(new IntegerSchema())->safeValidate([1, 2, 3])->isValid(), ); static::assertTrue( new ArraySchema(new ObjectSchema([ 'name' => new StringSchema(), 'age' => new IntegerSchema(), ], required: [ 'name', 'age', ]))->safeValidate([ ['name' => 'Alice', 'age' => 30], ['name' => 'Bob', 'age' => 25], ])->isValid(), ); $invalid = new ArraySchema(new IntegerSchema())->safeValidate([1, 2, 'three']); static::assertFalse($invalid->isValid()); static::assertSame(['$[2]'], \array_column($invalid->violations, 'path')); } #[DataProvider('nonArrayProvider')] public function testRejectsNonArrays(mixed $value): void { $result = new ArraySchema(new StringSchema())->safeValidate($value); static::assertFalse($result->isValid()); static::assertSame(['invalid_type'], \array_column($result->violations, 'code')); } /** * @return array */ public static function nonArrayProvider(): array { return [ 'null' => [NULL], 'string' => ['value'], 'integer' => [1], 'boolean' => [TRUE], 'empty object' => [new \stdClass()], 'object-like array' => [['key' => 'value']], ]; } /** * @param list $value * @param list $codes */ #[DataProvider('lengthProvider')] public function testEnforcesLengthBoundaries(array $value, bool $valid, array $codes): void { $result = new ArraySchema(new StringSchema(), minLength: 2, maxLength: 4)->safeValidate($value); static::assertSame($valid, $result->isValid()); static::assertSame($codes, \array_column($result->violations, 'code')); } /** * @return array, bool, list}> */ public static function lengthProvider(): array { return [ 'below minimum' => [['a'], FALSE, ['min_length']], 'at minimum' => [['a', 'b'], TRUE, []], 'within range' => [['a', 'b', 'c'], TRUE, []], 'at maximum' => [['a', 'b', 'c', 'd'], TRUE, []], 'above maximum' => [['a', 'b', 'c', 'd', 'e'], FALSE, ['max_length']], ]; } public function testSafeParseAppliesItemTransformations(): void { $schema = new ArraySchema(new StringSchema(default: 'fallback')); $result = $schema->safeParse([NULL, 'value']); static::assertTrue($result->isValid()); static::assertSame(['fallback', 'value'], $result->value); } }