Security News
Input Validation Vulnerabilities Dominate MITRE's 2024 CWE Top 25 List
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
backbone.validator
Advanced tools
Validation plugin for Backbone allowing for setting of default values
is_instance
pre-defined validatorto_equal
to use _.isEqual
for better comparison. Added comments for annotated source code, and started porting in tests from our internal codebase. Matching latest Backbone version tested against.Array.prototype.indexOf
, switched to _.indexOf()
error
fires correctly even when use_defaults
is true
. Passes 17 test cases so far.format
, removed is_url
validator (not useful)The version of this plugin you should use should match the version of Backbone that you're using, following Semver rules. Underscore will need to be the minimum version required for that version of Backbone.
Override the prototype.validate
for your created model with the package.
var PrimateModel = Backbone.Model.extend({
useDefaults: true,
defaults: {
type: "Gorilla",
name: "Magilla"
},
validators: {
type: {
inList: ['Gorilla', 'Human', 'Monkey']
},
name: {
isType: 'string'
}
}
});
PrimateModel.prototype.validate = require('backbone.validator');
If you want the system to substitute your defaults, set useDefaults
on your model defintion to true, or pass in {useDefaults: true}
to your options hash when set
ting or save
-ing a model.
Remember: if you want to validate on set
, you need to also include {validate: true}
Validators are defined in the validator
object as part of the model setup. If the value passed in doesn't meet your criteria for a valid value, return any value. If it does match your criteria, return nothing (undefined
). You may attach multiple validators to each attribute -- they will be run in the order in which they are attached. If one of them fails, the entire validation will fail and error
will be triggered.
var TestModel = Backbone.Model.extend({
validators: {
title: {
fn: function(value){
if(typeof(value) !== 'string'){
return "The title has to be a valid string";
}
}
}
}
});
var test_model = new TestModel();
test_model.set({title: "I am a title!"});
test_model.get('title');
"I am a title!"
test_model.set({title: false});
test_model.get('title');
"I am a title!"
You can catch errors and do something with them by attaching a listener to the invalid
event which is triggered when the validation fails.
TestModel.extend({
initialize: function(){
this.on('invalid', this.displayError);
},
displayError: function(errors){
errors.forEach(function(error){
console.log(errors.attr, errors.error);
});
}
});
var test_model = new TestModel();
test_model.set({title: "I am a title!"});
test_model.get('title');
"I am a title!"
test_model.set({title: false});
"The title has to be a valid string"
test_model.get('title');
"I am a title!"
You can have the validation framework substitute a reasonable default for an invalid option. This is useful when bootstrapping the model from an untrusted source.
TestModel.extend({
useDefaults: true,
defaults: {
title: "BAD TITLE"
}
});
var test_model = new TestModel();
test_model.set({title: "I am a title!"});
test_model.get('title');
"I am a title!"
test_model.set({title: false});
"The title has to be a valid string"
test_model.get('title');
"BAD TITLE"
Tests if a value is a member of a given array
Name | Type | Description |
---|---|---|
value | mixed | value to test |
list | array | the list of acceptable values |
attribute | string | the name of the model attribute |
error message, if any
Tests to see if the value is the key on an object
Name | Type | Description |
---|---|---|
value | string | the value to test |
obj | object | the object to test for keys |
attribute | string | the name of the model attribute |
error message, if any
Tests to see if the value is under a max length
Name | Type | Description |
---|---|---|
value | mixed | the value to test: string or array |
length | number | the maximum length for value |
attribute | string | the name of the model attribute |
error message, if any
Test a number fo make sure it's lower than a specified value
Name | Type | Description |
---|---|---|
value | number | the number to test |
limit | number | the maximum value for this number |
attribute | string | the name of the model attribute |
error message, if any
Test to see if the value is over a min length
Name | Type | Description |
---|---|---|
value | mixed | the value to test: string or array |
length | number | the minumum value for length |
attribute | string | the name of the model attribute |
error message, if any
Tests a number to make sure it's at least a specified value or higher
Name | Type | Description |
---|---|---|
value | number | the number to test |
limit | number | the minimum value |
attribute | string | the name of the model attribute |
error message, if any
Test to see if two values are shallow equal
Name | Type | Description |
---|---|---|
value | mixed | the value to test |
example | mixed | the desired value |
attribute | string | the name of the model attribute |
error message, if any
A drop-in replacement for the validate
method on a Backbone model.
Usage:
var MyModel = Backbone.Model.extend({});
MyModel.prototype.validate = require('backbone.validate');
Name | Type | Description |
---|---|---|
attributes | object | the attributes being set |
options | object | the options hash |
array of error objects, if any.
generated with docme
The inspiration for this comes directly (along with the format
function) from Thomas Pedersen's Backbone.Validation. There are a lot of similarities in structure, but different logic on how to perform the validations.
Backbone.Validator is copyright (c) 2012 Broadcastr.
Copyright (C) 2012 Broadcastr
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
FAQs
Validation plugin for Backbone allowing for setting of default values
The npm package backbone.validator receives a total of 3 weekly downloads. As such, backbone.validator popularity was classified as not popular.
We found that backbone.validator demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
MITRE's 2024 CWE Top 25 highlights critical software vulnerabilities like XSS, SQL Injection, and CSRF, reflecting shifts due to a refined ranking methodology.
Security News
In this segment of the Risky Business podcast, Feross Aboukhadijeh and Patrick Gray discuss the challenges of tracking malware discovered in open source softare.
Research
Security News
A threat actor's playbook for exploiting the npm ecosystem was exposed on the dark web, detailing how to build a blockchain-powered botnet.