Security News
tea.xyz Spam Plagues npm and RubyGems Package Registries
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
backbone-validator
Advanced tools
Readme
A super simple validator module for Backbone. It works both on the browser and nodejs.
In node:
npm install backbone-validator --save
In the browser make sure that you add the backbone-validator-min.js script after you have loaded both Underscore and Backbone. The minimised file is 2k.
var Backbone = require('Backbone');
var validator = require('validator');
var MyModel = Backbone.Model.extend({
validate: validator.create({
type: { equal: 'user', msg: "type must be `user`" },
firstname: { type: 'string', minLength: 3, maxlength: 20 },
email: { type: 'email' }
})
});
var model = new MyModel();
model.on('invalid', function (m, err) {
// Validation failed
// `err` will be an object with the error message {type:'message'}.
});
model.set({ type: 'not user' }, { validate: true });
To use this module you basically invoke validator.create()
passing it a
schema
object. This will return a function, and we set the model's validate
property to this function, so that Backbone
can use when setting attribute
values (ie: when model.save()
is invoked).
A schema
object contains a property for each attribute
we want to validate,
the property name is the attribute
name and the value is an object containing
a set of rules.
In the example below we want to validate the ctime
, status
and message
attribues in our model, so our schema will look something like this:
validator.create({
ctime: { type: 'date' },
status: { oneOf: [ 1, 2, 3 ] },
message: { type: string, minLength: 5 }
});
Eache rule is declared passing it options
. This options
depend on each of
the rules (ie: for the required
rule options
is just a boolean, for the
oneOf
its an array, for custom
its a function and so on.
required
validator.create({
message: { required: true }
});
equal
validator.create({
type: { equal: 'user' }
});
regexp
validator.create({
birthday: { regexp: /^\d{2}\/\d{2}\/\d{4}$/ }
});
oneOf
validator.create({
colour: { oneOf: [ 'red', 'green', 'blue' ] }
});
type
. Types: boolean
, number
, string
, date
, array
, email
,
model
, collection
, url
and domain
.validator.create({
balance: { type: 'number' }
});
minLength
. Can be used with strings or arrays.validator.create({
firstname: { type: 'string', minLength: 3 }
});
maxLength
. Can be used with strings or arrays.validator.create({
firstname: { type: 'string', maxLength: 20, minLength: 2 }
});
recurse
. Can be used to do submodel validation.validator.create({
submodel: { type: 'model', recurse: true }
});
var MyModel = Backbone.Model.extend({
validate: validator.create({
phone: {
custom: function (value) {
// This function will be called with the value that needs to be
// validated. If you want validation to fail simply return a string with
// the error message. If nothing is returned validation for this
// attribute is consider to have passed.
}
}
})
});
backbone-validator comes with default error messages that can be overriden.
validator.create({
field: { regexp: /aregex/, msg: "A custom message." }
});
FAQs
A super simple validator module for Backbone.
The npm package backbone-validator receives a total of 18 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
Tea.xyz, a crypto project aimed at rewarding open source contributions, is once again facing backlash due to an influx of spam packages flooding public package registries.
Security News
As cyber threats become more autonomous, AI-powered defenses are crucial for businesses to stay ahead of attackers who can exploit software vulnerabilities at scale.
Security News
UnitedHealth Group disclosed that the ransomware attack on Change Healthcare compromised protected health information for millions in the U.S., with estimated costs to the company expected to reach $1 billion.