-
Notifications
You must be signed in to change notification settings - Fork 337
Add type validator #80
Comments
I'm guessing this would be a mostly server side validator since the end user cannot really do anything to fix the error if it's used in a browser. The number/integer version already exists with the I'm weighing between this being a separate validators or one that does it all. |
+1 This would be helpful both client- and server-side. Also could allow for custom type comparators for custom types, or perhaps use actual objects/prototypes for comparison: var constraints = {
name: { type: String },
age: { type: Number },
multiple: { type: [String, Boolean, Number] },
custom: { type: User }
}
var data = {
age: 17,
name: 'John',
multiple: true, // can also be String and Number
custom: new User()
}
v(data, constraints) I had made a partial working validator for this already, but not a comprehensive one. |
@ansman Yes, it's mostly for the backend. I think it should be one validator. We can use |
I also was looking for this, so I added it as custom validator. Might be useful for somebody else, or maybe somebody wants to integrate it into the library? validate.validators.type = function(value, options, key, attributes) {
// allow empty values by default (needs to be checked by "presence" check)
if(value === null || typeof value === 'undefined') {
return null;
}
// allow defining object of any type using their constructor. --> options = {clazz: ClassName}
if(typeof options === 'object' && options.clazz) {
return value instanceof options.clazz ? null : ' is not of type "' + options.clazz.name + '"';
}
if(!validate.validators.type.checks[options]) {
throw new Error("Could not find validator for type " + options);
}
return validate.validators.type.checks[options](value) ? null : ' is not of type "' + options + '"' ;
};
validate.validators.type.checks = {
Object: function(value) {
return validate.isObject(value) && !validate.isArray(value);
},
Array: validate.isArray,
Integer: validate.isInteger,
Number: validate.isNumber,
String: validate.isString,
Date: validate.isDate,
Boolean: function(value) {
return typeof value === 'boolean';
}
}; It also supports something like this: {
test: {
type: {clazz: TestClass}
},
} (Sadly options cannot be the object itself like |
+1 to this (and thank you @Screeze). I'm using |
I'd like to give my vote for this. I'm really surprised there wasn't type checking to begin with. Thank you @Screeze for the validator. I'll be using that for now. |
+1, handy for server-side validation. Going to roll with the @Screeze option in the meantime. In any case, I just went with the following as per earlier suggestions but trimmed it down.
Use as follows (and I'm ok with the superfluous true):
|
+1 |
This has been implemented and will be released in the next version. |
Awesome! When will the next version be published? Anything blocking the next release that a new contributor could help with? |
Hi - echo'ing @emckay - anything we can do to get this out? |
I just tested the master branch and it seems to work. Anything that still needs to be done to get it into the next release? |
Basic types:
Example:
User should be able to add own or modify existing types:
The text was updated successfully, but these errors were encountered: