Closed AbdelrahmanHafez closed 6 years ago
@AbdelrahmanHafez You can accomplish the mongoid
request using custom validation rules
https://github.com/skaterdav85/validatorjs#register-custom-validation-rules
Do I have to define the validation rule on every single controller in the project or do I only do it once and it's globally defined?
Not sure how your application is constructed, but you should be able to define it only once
You define a rule on Validator
constructor function (class) - so it is global.
I think that it is better to use custom validation rules.
Since this library concept is Laravel's Validator, it is more beautiful to not increase the rules not defined in Laravel to ad hoc.
If increases from now on, I think that the rules defined in Laravel, such as present
, nullable
, are preferable.
https://laravel.com/docs/5.5/validation#available-validation-rules
It's inspired by Laravel's Validator, but it's in JS. We should add features that support things used by the JS community, such as MongoDB.
It is possible to use Mongo DB in PHP, it is not specific to JavaScript.
Laravel's original validator has rules about databases, like the exists
and the unique
rule.
I think that specification that gives validatorjs the information of database drivers as option and allows it to be inspected with exists
or unique
rule is a good idea 😃
Connection is the responsibility of the external driver. The job of validatorjs is only the examination of the obtained value.
// If could write a `exists` rule like Laravel...
// true if there is a document whose _id is cityId in the `cities` collection
cityId: 'required|exists:cities,_id'
I think that vendor-specific rules like "mongoId" are more beautiful realized with custom rules.
I am not sure if we're on the same page here, I am not trying to verify if that ID indeed exists on my database, I am just trying to verify that the given string follows a mongoid pattern, 24 hexadecimal strings.
I am aware it could be achieved using custom rules, but it would sure make many people's lives easier to have such features built-in in the package itself.
In my opinion, adding a mongoId validator is a too specific rule. And in general, this is not a good idea.
Probably, adding a rule like 'hex' - may be much better. In your situation, you can combine it with other rules to reach desired results.
for example, your rules may look like:
{mongoId: 'hex|size:24'}
@vict-shevchenko @AbdelrahmanHafez I agree, this request is really to specific and outside the core scope of the project. It can be accomplished pretty easily using a custom rule (as described above).
I came across this package today, and I love how it makes validation simpler, however, I'd love it even more if it had an 'empty' rule, that makes sure a specific field is not included.
isDeleted
in this example would be how I would use it.Edit: a little hack that could solve this is using
max:0
instead of empty, now I am looking forward to have a 'mongoid' feature, that's the only thing missing so far for my needs.