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

Custom function #493

Closed
ravipxm opened this issue Feb 8, 2018 · 3 comments
Closed

Custom function #493

ravipxm opened this issue Feb 8, 2018 · 3 comments

Comments

@ravipxm
Copy link

ravipxm commented Feb 8, 2018

Is there a way to define custom validation function?

Thanks for this amazing lib

@erayd
Copy link
Contributor

erayd commented Feb 8, 2018

There is not.

What are you trying to achieve? If you can explain what you want to do, we may be able to suggest an alternative method.

@codejunkie84
Copy link

@ravipxm
I've looked also for this feature and actually this feature is not available in this validator. The opis/json-schema validator supports this.

If you want to try to archive this you need to do the following:

  • Extend JsonSchema\Constraints\Factory to add a method like addConstraint(string $name, BaseConstraint $constraint) which addes a implementation of JsonSchema\Constraints\BaseConstraint to the constraintMap attribute.
  • Extend JsonSchema\Validator to overload the constructor and inject by using parent::__construct(new MyNewFeatureRichFactory()) the new factory which supports your user defined constraints.

But actually I do not recommend this to you. First of all you should take a look to a different schema validator, because this Validator is nice but not very extendable, due the consequent violation of SOLID principles. This leads to this "inheritance incest" I've described above.

@erayd
Common use case for me: In my projects I build mostly REST-APIs, also to register or login users. For example, if I receive a json for my register user operation I want to validate this json. So a given e-mail address is valid from the format but after using this validator I also need to check this in a second step if this e-mail address is already registred.

If I could combine the "e-mail address format constraint" and a "e-mail address is not registred constraint" in a composite design pattern, so that I'm able to handle the validation in one step. So there would be no need to do a second instance for "verifing" the data plausibility. The same could be needed for "is email address registred constraint" for login and a forgot password feature or to check relations.

But as I said before, this package is not very extensible for this.

@DannyvdSluijs
Copy link
Collaborator

@ravipxm in an attempt to cleanup this repo we are trying to filter the issues and see which ones might be closed. Is it safe to assume this is a rather old issue, which sadly was left unanswered, and can be closed? Feel free to close it yourself with some comments if helpful.

@codejunkie84 from your comments I can see you want to do some contextual validation where you would have some specific validations you would like to apply as callback. From the perspective of the library this would seem out of scope as it's goal is to validate the JSON schema. And as it is just keeping up with only doing that is already a decent challenge.

What I believe @erayd is hinting at is see how you can use a validated JSON document for validation down the line. As an example in some projects I've been part of we use Symfony and have a class which resembles the JSON structure and do contextual validation using the #[Assert/...] annotations.

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

No branches or pull requests

4 participants