-
Notifications
You must be signed in to change notification settings - Fork 1.5k
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
Possible to extend with custom validation functions? #125
Comments
No. There is little point in that. You can just run whatever functions you want on the data before or after. By keeping all the validation logic well specified, we can do other smart things like auto doc generation. |
I need to validate dates stronger than what joi currently allows, that is :
How do you suggest I go about it ? I'm ok to submit a PR to complete |
How would that work? example? |
Since base objects are not currently exposed and I'm not sure you're willing to, what would you think of a backbone-like api ? var customValidator = Joi.any.extend({
type: 'CustomDate',
initialize: function() {
this._test('customdate.base', ...)
},
format: function(str) {
this._test('format' ...)
},
_private: function() {}
});
var error = customValidator().format('DD/MM/YYYY').validate('....'); Not sure about |
+1 I've been using EVE for validation for a long time, and would like to migrate to joi (since that one seem to be unsupported), but an absence of custom validators/transformers here just make that impossible. |
+1 would be interested into custom validation functions, too |
@Marsup I would be happy to support the date requirements. I am still opposed to extensible types. |
Would that be ok to include moment for that ? I don't see any reason to reinvent the wheel. |
I guess. |
+1 some more. Putting your schema all in one place is advantageous. Forcing the user to perform multiple separate validation steps kind of defeats the purpose of using a validator library. Here's an example use-case: I'm implementing a Syslog library with support for structured data. By the RFC, the 'language' value should be a valid BCP_47/RFC4646 language tag. There's a module for that. But can I do anything so simple as It seems like "automatic documentation" is your sticking point, and I just don't understand it. Where does Joi provide anything to generate documentation? Or are you talking about Hapi, in which case, why is it a concern of Joi's to begin with? Or perhaps you're talking about the core Joi documentation, in which case, why would you feel like custom functions need to be documented in Joi's core? Of course they are beyond the scope of the Joi documentation. Custom validators aren't there to fork and extend Joi, they're to add purpose-specific validation to a specific local use of the library. You want to encourage 'configuration over code', but here you are making a decision that forces code over configuration. No matter how I look at it, I can't see a good reason for sticking so stubbornly to this refusal to implement a wholly useful and obviously desired feature that will take almost no effort to include. Please reconsider. P.S. - Another problem this causes: inconsistency in validatior error reporting. |
As the long time maintainer of |
I'm not suggesting so much that it's easy as that it's necessary. I know what you mean about synchronized code and documentation, I'm just confused as to how custom validation functions are any harder to document than anything else, from joi's core's side of things. Its not your responsibility to document the functions people write into it, nor could you possibly do so. The api is pretty obvious given the existing code: it'd be an any() method like .regex() but instead it would call a function. It's nice to hear there's a possibility of getting some action on this; what are the problems that need solving? |
+1(million) |
@rlidwka @myndzi @jordancardwell @gebrits #577 is now open for discussion, chime in please. |
@Marsup Any update on this? I am sifting through and looking to go with a validation library. Since I am validating dynamic data and I need to do cross-reference checks, it would be nice to be able to create custom validation strategies, or types. |
As the last part of #577 says, it's currently in a branch, I'll try to release an alpha on npm since no one seems interested in giving feedback based on a branch. |
@Marsup Is this working? I need to make extensions for document numbers (my country). |
Hello @hueniverse , I’ve been searching through many validators and found this one, which made me very happy: not overloaded syntax and clear functional style. However, for our big ecosystem we need to validate custom data (such as check company registration number, etc). If joi cannot provide option for custom validation, it would be very inconvenient. I would like to know if you still opposing this change or not, because we are making important choice between class-validator and joi. I definitely like joi more, except that we need this feature. |
It's been here for quite a while now, this issue is from 2016. See https://hapi.dev/family/joi/?v=16.1.8#extendextensions. |
@Marsup I missed that out, thank you. UPD:is there a way to add async validation for custom function? Because I need to query db and other facilities for validating data. |
@georgyfarniev you are being a bit lazy. You can't search the API page for |
@hueniverse cannot disagree about lazy, but I wasn’t able to find that out. I will double check it from my laptop tomorrow. On iPad mini docs little bit inconvenient. |
Is it possible to extend Joi with custom validation functions?
The text was updated successfully, but these errors were encountered: