Skip to content

Commit

Permalink
chore: Applying filters while creating a validator
Browse files Browse the repository at this point in the history
  • Loading branch information
matapatos committed May 18, 2024
1 parent e98dbe0 commit d9aff8b
Show file tree
Hide file tree
Showing 4 changed files with 17 additions and 5 deletions.
5 changes: 3 additions & 2 deletions src/Contracts/JsonSchema.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}

/**
Expand Down Expand Up @@ -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);
}
}
3 changes: 2 additions & 1 deletion src/Schemas/ResponseMiddleware.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
13 changes: 12 additions & 1 deletion tests/Unit/Schemas/BaseTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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');
1 change: 0 additions & 1 deletion tests/Unit/Schemas/SchemaMiddlewareTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down

0 comments on commit d9aff8b

Please sign in to comment.