Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Inject possible dependencies in codec and validator #14

Merged
merged 2 commits into from
Dec 17, 2015

Conversation

bburnichon
Copy link
Contributor

Make it possible to configure options on validators used

@webmozart
Copy link
Owner

Hi @bburnichon, thank you for this PR! :) What's the use case for this change?

@bburnichon
Copy link
Contributor Author

I have a silex project where I need to configure specific validation for each encoding/decoding. This way, library is fully decoupled and I can have a configured JsonDecoder/JsonEncoder in Pimple container for each use case.

Also, I already have a JsonSchema\UriRetriever in my container and want to use it for all validations to minimize Http roundtrips. I inject my UriRetriever in $validatorFactory:

$app['json-schema.retriever'] = $app->share(function () {
  return new UriRetriever();
});
$app['json-schema.validator_factory'] = $app->protect(function () use ($app) {
  return new Validator($app['json-schema.url_retriever']);
});

$app['json.validator'] = $app->share(function (Application $app) {
  return new JsonValidator($app['json-schema.validator_factory']);
});

$app['json.decoder'] = $app->share(function (Application $app) {
  return new JsonDecoder($app['json.validator']);
});

/**
* JsonValidator constructor.
*
* @param callable|null $validatorFactory Factory creating JsonSchema\Validator instances.
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why do you need support for callables in here instead of directly injecting the Validator?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because a new instance of Validator should be used at each call to check.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why don't you simply reset() the validator before using it?

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(which, of course, I could have done too :) )

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Because I did not knew it exists! ;)

@bburnichon
Copy link
Contributor Author

This PR includes all changes from #9.

@webmozart I can squash commits if you prefer to keep a clean history.

/**
* @return Validator
*/
private function prepareValidator()
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for updating! :) I don't think this needs to live in a separate method. You can simply inline the reset() call above.

@webmozart
Copy link
Owner

No, looks good :) Thanks a lot @bburnichon!

webmozart added a commit that referenced this pull request Dec 17, 2015
Inject possible dependencies in codec and validator
@webmozart webmozart merged commit e143cda into webmozart:master Dec 17, 2015
@bburnichon bburnichon deleted the dependency-injection branch May 19, 2016 14:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants