mikeerickson / validatorjs

A data validation library in JavaScript for the browser and Node.js, inspired by Laravel's Validator.
https://www.npmjs.com/package/validatorjs
MIT License
1.77k stars 280 forks source link

Feature request: empty & mongoId #234

Closed AbdelrahmanHafez closed 6 years ago

AbdelrahmanHafez commented 6 years ago

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.

    let expectedBody = {
      holdingCompany: {
        alias: 'required|string',
        address: {
            cityId: 'required|mongoId',
            street: 'required|string'
        },
        contact: {
            name: 'required|string',
            mobile: 'required|integer'
        },
        isDeleted:'empty'
      }
    };

    let validation = new Validator(req.body, expectedBody);
    if (validation.fails()) return res.status(500).json({ case: 0, message: validation.errors.all() });

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.

mikeerickson commented 6 years ago

@AbdelrahmanHafez You can accomplish the mongoid request using custom validation rules

https://github.com/skaterdav85/validatorjs#register-custom-validation-rules

AbdelrahmanHafez commented 6 years ago

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?

mikeerickson commented 6 years ago

Not sure how your application is constructed, but you should be able to define it only once

vict-shevchenko commented 6 years ago

You define a rule on Validator constructor function (class) - so it is global.

sdr0x07b6 commented 6 years ago

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

AbdelrahmanHafez commented 6 years ago

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.

sdr0x07b6 commented 6 years ago

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.

AbdelrahmanHafez commented 6 years ago

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.

vict-shevchenko commented 6 years ago

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'}

mikeerickson commented 6 years ago

@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).