leepowelldev / mongoose-validator

Validators for mongoose models utilising validator.js
MIT License
379 stars 43 forks source link

Can be linked with a mongoose-validator and validator.js? #19

Closed kjirou closed 9 years ago

kjirou commented 9 years ago

I tryed to extend original validator.js by validate.extend. However, I did not succeed in such a code.

From the test code, it seems to be able to extend it.

Is it possible to link each other?

leepowelldev commented 9 years ago

Why would you be testing against false here?

// But, isFoo does not exist in validator.js
assert('isFoo' in validatorjs === false);

isFoo has been added to validatorjs so you should be checking against true for your test to pass:

// But, isFoo does not exist in validator.js
assert('isFoo' in validatorjs === true);
kjirou commented 9 years ago

@leepowellcouk

Please check the following code:

$ node
> assert = require('assert')
> obj = { foo:function(){} }
{ foo: [Function] }
> assert('foo' in obj === false)
AssertionError: false == true
    at repl:1:2
    at REPLServer.self.eval (repl.js:110:21)
    at Interface.<anonymous> (repl.js:239:12)
    at Interface.emit (events.js:95:17)
    at Interface._onLine (readline.js:202:10)
    at Interface._line (readline.js:531:8)
    at Interface._ttyWrite (readline.js:760:14)
    at ReadStream.onkeypress (readline.js:99:10)
    at ReadStream.emit (events.js:98:17)
    at emitKey (readline.js:1095:12)
> assert('foo' in obj === true)

It means that 'isFoo' in validatorjs === false is true.


In addition, I inserted debug print in the test code.

false
{ version: '3.22.0',
  extend: [Function],
  init: [Function],
  toString: [Function],
  toDate: [Function],
  toFloat: [Function],
  toInt: [Function],
  toBoolean: [Function],
  equals: [Function],
  contains: [Function],
  matches: [Function],
  isEmail: [Function],
  isURL: [Function],
  isIP: [Function],
  isFQDN: [Function],
  isAlpha: [Function],
  isAlphanumeric: [Function],
  isNumeric: [Function],
  isHexadecimal: [Function],
  isHexColor: [Function],
  isLowercase: [Function],
  isUppercase: [Function],
  isInt: [Function],
  isFloat: [Function],
  isDivisibleBy: [Function],
  isNull: [Function],
  isLength: [Function],
  isByteLength: [Function],
  isUUID: [Function],
  isDate: [Function],
  isAfter: [Function],
  isBefore: [Function],
  isIn: [Function],
  isCreditCard: [Function],
  isISBN: [Function],
  isJSON: [Function],
  isMultibyte: [Function],
  isAscii: [Function],
  isFullWidth: [Function],
  isHalfWidth: [Function],
  isVariableWidth: [Function],
  isSurrogatePair: [Function],
  isBase64: [Function],
  isMongoId: [Function],
  ltrim: [Function],
  rtrim: [Function],
  trim: [Function],
  escape: [Function],
  stripLow: [Function],
  whitelist: [Function],
  blacklist: [Function],
  normalizeEmail: [Function] }

Although I forgot say, node version is 0.10.29 and 0.10.33.

leepowelldev commented 9 years ago

The way your test is working is using 2 instances of validator.js ... one is a dependancy of your project, and the other is a dependancy of mongoose-validator. They are not the same thing.

By the nature of node modules, mongoose-validator will use it's own internal version of validator.js - I admit by looking at the tests it appears you use it this way.

A way to enable you to work the way you want to would be to either:

a) Expose the internal version of validator.js from mongoose-validator b) Create the ability for the developer to override the internal version with a pointer to another version

'A' would be the safest to implement, however through the rest of your code you'd need to use this:

var validatorjs = require('mongoose-validator').validatorjs

instead of

var validatorjs = require('validator')

'B' would bring the closest solution to what you're trying to achieve, but could cause issues if an untested/unsupported version of validator.js is passed to mongoose-validator, this is my thinking (or something similar):

var validator = require('validator');
var mongooseValidator = require('mongoose-validator');

// Replace the pointer to this version...
mongooseValidator.replaceValidator(validator);

You're welcome to fork and see which one works best and then I can look at merging in if it's found to be useful.

kjirou commented 9 years ago

Thanks for your advice. I thought that A is better than B.