diff --git a/src/Contracts/JsonSchema.php b/src/Contracts/JsonSchema.php index f06e934..3fd345c 100644 --- a/src/Contracts/JsonSchema.php +++ b/src/Contracts/JsonSchema.php @@ -67,9 +67,9 @@ public function __construct(string|array $schema, ?SchemaResolver $schemaResolve { parent::__construct(); $this->schema = $schema; + $this->suffix = $this->getSuffix(); $this->validator = $this->createValidatorWithResolver($schemaResolver); $this->errorFormatter = new ErrorFormatter(); - $this->suffix = $this->getSuffix(); } /** @@ -128,7 +128,8 @@ public function createValidatorWithResolver(?SchemaResolver $resolver): Validato { $resolver = $resolver ?? new SchemaResolver(); $schemaLoader = new SchemaLoader(new SchemaParser(), $resolver, true); + $validator = apply_filters('fastendpoints_validator', new Validator($schemaLoader), $this); - return new Validator($schemaLoader); + return apply_filters($this->suffix.'_validator', $validator, $this); } } diff --git a/src/Schemas/ResponseMiddleware.php b/src/Schemas/ResponseMiddleware.php index b1b06dc..b3d8baa 100644 --- a/src/Schemas/ResponseMiddleware.php +++ b/src/Schemas/ResponseMiddleware.php @@ -255,7 +255,8 @@ public function createValidatorWithResolver(?SchemaResolver $resolver): Validato { $resolver = $resolver ?? new SchemaResolver(); $schemaLoader = new SchemaLoader(new ResponseSchemaParser(), $resolver, true); + $validator = apply_filters('fastendpoints_validator', new Validator($schemaLoader), $this); - return new Validator($schemaLoader); + return apply_filters($this->suffix.'_validator', $validator, $this); } } diff --git a/tests/Unit/Schemas/BaseTest.php b/tests/Unit/Schemas/BaseTest.php index 1aea3e3..6c58b3b 100644 --- a/tests/Unit/Schemas/BaseTest.php +++ b/tests/Unit/Schemas/BaseTest.php @@ -70,7 +70,7 @@ // getSuffix() -test('Checking correct ResponseMiddleware suffix', function (string $class) { +test('Checking correct Middleware suffix', function (string $class) { $schema = new $class([]); $suffix = Helpers::invokeNonPublicClassMethod($schema, 'getSuffix'); $expectedSuffix = Helpers::getHooksSuffixFromClass($schema); @@ -118,3 +118,14 @@ ->and($validator->resolver()) ->toBeInstanceOf(SchemaResolver::class); })->with('base_classes')->group('base', 'createValidatorWithResolver'); + +test('Calling hooks while creating JSON schema validator', function (string $class) { + $suffix = Helpers::getHooksSuffixFromClass($class); + Filters\expectApplied('fastendpoints_validator') + ->once() + ->with(Mockery::type(Validator::class), Mockery::type($class)); + Filters\expectApplied($suffix.'_validator') + ->once() + ->with(Mockery::type(Validator::class), Mockery::type($class)); + new $class([]); +})->with('base_classes')->group('base', 'createValidatorWithResolver'); diff --git a/tests/Unit/Schemas/SchemaMiddlewareTest.php b/tests/Unit/Schemas/SchemaMiddlewareTest.php index 9d730b3..e743ca4 100644 --- a/tests/Unit/Schemas/SchemaMiddlewareTest.php +++ b/tests/Unit/Schemas/SchemaMiddlewareTest.php @@ -153,7 +153,6 @@ $result = $schema->onRequest($req); expect($result)->toBeNull(); $this->assertEquals(Filters\applied('fastendpoints_schema_params'), 0); - $this->assertEquals(Filters\applied('fastendpoints_schema_validator'), 0); $this->assertEquals(Filters\applied('fastendpoints_schema_is_valid'), 0); })->group('schema', 'validate');