poppinss / indicative

Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
https://indicative.adonisjs.com/
MIT License
417 stars 52 forks source link

Solved: ENGINE_EXCEPTION: e.hasOwnProperty is not a function #206

Closed rostamiani closed 6 years ago

rostamiani commented 6 years ago

Package version

5.0.8

Node.js and npm version

10.8.0, 6.2.0

Sample Code (to reproduce the issue)

var validate = require("indicative").validate;
validation_rules = {

    phone_number: 'regex:/^\d{8,12}$/',
    mobile_number: 'regex:/^09\d{9}$/'
    }
validate(data, this.validation_rules)

The validation error:

[{"message":"e.hasOwnProperty is not a function","field":"phone_number","validation":"ENGINE_EXCEPTION"}]

rostamiani commented 6 years ago

After debugging I found the line of the error. But cannot solve it This is the first lines of main.js from npm package

! function (e, t) {
    "object" == typeof exports && "undefined" != typeof module ? module.exports = t() : "function" == typeof define && define.amd ? define(t) : e.indicative = t()
}(this, function () {
    "use strict";

    function e(e, t) {
        if (! function (e) {
                return null !== e && "object" == typeof e
            }(e) || "string" != typeof t) return e;
        for (var r = t.split("."), n = 0; n < r.length; n++) {
            var o = r[n];
            if (null === (e = e.hasOwnProperty(o) ? e[o] : null)) break // <------------ This is the error line
        }
        return e
    }
...

After putting a breakpoint there, I found out that the line is executed three times. This is the first and second time:

argument:Array(0) []
field:"phone_number"
validation:"required"
__proto__:Object {constructor: , __defineGetter__: , __defineSetter__: , …}

And the last time 'e' is my field list and does not have hasOwnProperty. this is the last time 'e':

Object {username: "yazdan", company_id: "1", first_name: "احمد", last_name: "یزدی", mobile_number: "", …}
address:""
company_id:"1"
date_birth:"1212-12-12"
first_name:"احمد"
id:"5"
last_name:"یزدی"
mobile_number:""
password:""
phone_number:""
type:"0"
username:"yazdan"
rostamiani commented 6 years ago

Solved

In real world field list was comming from res.body in Express middleware. The res.body is not a standard javascript object and does not have 'hasOwnProperty' method. A solution is this:

MatiasOlivera commented 5 years ago

I have the same problem. @thetutlage, is there any way to fix it? I am not using AdonisJS. Thanks!

jpcbarros commented 5 years ago

I am having the same issue right now, is there any fix?

RomainLanz commented 5 years ago

Please, create a new issue with a proper way to reproduce your issue.