ansman / validate.js

A declarative validation library written javascript
https://validatejs.org
MIT License
2.63k stars 336 forks source link

Examples are not working properly #120

Closed raja-i4 closed 8 years ago

raja-i4 commented 8 years ago

Hi,

I'm facing problem while running the given examples in node js environment.

this is how i'm using it.

var validatorJS = 
    require('validator.js').Validator;
var validator = new validatorJS();

var constraints = {
          creditCardNumber: {
            presence: true,
            format: {
              pattern: /^(34|37|4|5[1-5]).*$/,
              message: function(value, attribute, validatorOptions, attributes, globalOptions) {
                return validator.validate.format("^%{num} is not a valid credit card number", {
                  num: value
                });
              }
            },
            length: function(value, attributes, attributeName, options, constraints) {
              if (value) {
                // Amex
                if ((/^(34|37).*$/).test(value)) return {is: 15};
                // Visa, Mastercard
                if ((/^(4|5[1-5]).*$/).test(value)) return {is: 16};
              }
              // Unknown card, don't validate length
              return false;
            }
          },
          creditCardZip: function(value, attributes, attributeName, options, constraints) {
            if (!(/^(34|37).*$/).test(attributes.creditCardNumber)) return null;
            return {
              presence: {message: "is required when using AMEX"},
              length: {is: 5}
            };
          }
        };

validator.validate({creditCardNumber: "4"}, constraints);

While it is building constraints from the given config, it is failing by saying 'Should give a valid mapping object to Constraint'. It is failing while processing "presence" : true attribute.

Please find the screenshot below:

image

Please let me know how to resolve this problem.

ansman commented 8 years ago

You've included the library validator.js, this library name is called validate.js.

raja-i4 commented 8 years ago

like presence, length, type, format, etc. for length: minimum, maximum, etc

I would like to see complete list of these. I mean complete schema definition.

ansman commented 8 years ago

These is no place where they are all shown together, but you have the list of all the constraints and if you click on a constraint you can read about all the options.

raja-i4 commented 8 years ago

https://validatejs.org/#constraints . this page has minimal list.

If i look at source code, i got the following: presence, length, numericality, datetime, date, format, inclusion, exclusion, email, url, equality, pattern.

Pls let me know if i miss anything.

ansman commented 8 years ago

https://validatejs.org/#validators

raja-i4 commented 8 years ago

ok, thanks. It has the same. :)

raja-i4 commented 8 years ago

Hi,

I’m just reposting on the same as I couldn’t open a browser to create a new case.

My concern here is : I would like to find out excess data in “data” to be evaluated. For instance, My constraints = { “a” : { “presence”: true}, “b” : { “presence”: true}, “c” : { “presence”: true} }

My data:

{ “a” : 1, “b” : 2, “c” : 3, “d” : 4 }

My data has excess data “d”, do you have logic inplace to determine such things with any options. (without any options, it is unable to find out excess data)

Here my concern mainly is strict validation: data must match the constraints. No more or no less data should be there.

Thanks, Rajashekhar

ansman commented 8 years ago

No, there is currently no way to validate that. But the cleanAttributes function might help.

raja-i4 commented 8 years ago

cleanAttributes should have named as retainAttributes, and cleanAttributes should remove the given attributes.

From: Nicklas Ansman Giertz [mailto:notifications@github.com] Sent: Tuesday, April 26, 2016 1:02 PM To: ansman/validate.js validate.js@noreply.github.com Cc: Rajashekhar G rajashekhar.g@intellifour.com; Author author@noreply.github.com Subject: Re: [ansman/validate.js] Examples are not working properly (#120)

No, there is currently no way to validate that. But the cleanAttributeshttp://validatejs.org/#utilities-clean-attributes function might help.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/ansman/validate.js/issues/120#issuecomment-214653903

raja-i4 commented 8 years ago

validate.extend(validate.validators.datetime, { // The value is guaranteed not to be null or undefined but otherwise it // could be anything. parse: function(value, options) { return +moment.utc(value); }, // Input is a unix timestamp format: function(value, options) { var format = options.dateOnly ? "YYYY-MM-DD" : "YYYY-MM-DD hh:mm:ss"; return moment.utc(value).format(format); } });

validate({departure: "foobar"}, {departure: {datetime: true}}); ReferenceError: moment is not defined at Function.validate.extend.parse (repl:5:9) at Function.validate.validators.datetime.v.extend.parse (d:\workspace\nodejs\test\node_modules\validate.js\validate.js:881:20) at Function.v.extend.runValidations (d:\workspace\nodejs\test\node_modules\validate.js\validate.js:133:30) at validate (d:\workspace\nodejs\test\node_modules\validate.js\validate.js:25:21) at repl:1:1 at REPLServer.defaultEval (repl.js:262:27) at bound (domain.js:287:14) at REPLServer.runBound as eval at REPLServer. (repl.js:431:12) at emitOne (events.js:82:20)

From: Nicklas Ansman Giertz [mailto:notifications@github.com] Sent: Tuesday, April 26, 2016 1:02 PM To: ansman/validate.js validate.js@noreply.github.com Cc: Rajashekhar G rajashekhar.g@intellifour.com; Author author@noreply.github.com Subject: Re: [ansman/validate.js] Examples are not working properly (#120)

No, there is currently no way to validate that. But the cleanAttributeshttp://validatejs.org/#utilities-clean-attributes function might help.

— You are receiving this because you authored the thread. Reply to this email directly or view it on GitHubhttps://github.com/ansman/validate.js/issues/120#issuecomment-214653903

ansman commented 8 years ago

Please read the documentation: http://validatejs.org/#validators-datetime