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.
active-model
Advanced tools
Readme
Data source agnostic opinionated models for Node.js with classic inheritance, basic validations and a clean API. Models only keep pre-defined properties on initialization which protect against property leakage and ensure your models remain clean however tey are instantiated.
Create and return a new model based on schema.
width: Number
Object.defineProperty
e.g. area: { get: function () {return this.width * this.height } }
Validations supported at the moment are type validation for Number
, String
, Boolean
, Date
, Object
, and Array
.
When values are not of the correct type, the model will try to cast them to the correct one, otherwise it will throw
an error.
var model = require('active-model').model;
var assert = require('assert');
var Rectangle = model({
attributes: {
height: Number,
width: Number
},
methods: {
area: function () { return this.width * this.height; }
},
properties: {
perimeter: {
get: function () {
return (this.width + this.height) * 2;
}
}
}
});
var rectangle = new Rectangle({ width: 2, height: 3, unknown: true });
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);
assert.notEqual(rectangle.unknown, true);
assert.equal(rectangle.area(), 6);
assert.equal(rectangle.perimeter, 10);
It is also possible to configure the attributes, methods, and properties individually.
var model = require('active-model').model;
var assert = require('assert');
var Rectangle = model({
height: Number,
width: { type: Number } // support object notation
});
Rectangle.methods({
area: function () { return this.width * this.height; }
});
Rectangle.properties({
perimeter: {
get: function () {
return (this.width + this.height) * 2;
}
}
});
var rectangle = new Rectangle({ width: 2, height: 3, unknown: true });
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);
assert.notEqual(rectangle.unknown, true);
assert.equal(rectangle.area(), 6);
assert.equal(rectangle.perimeter, 10);
Use a custom constructor
var model = require('active-model').model;
var assert = require('assert');
var Rectangle = model({
height: Number,
width: Number,
constructor: function (width, height) {
this.width = width;
this.height = height;
}
});
var rectangle = new Rectangle(2, 3);
assert.equal(rectangle.width, 2);
assert.equal(rectangle.height, 3);
Create a new object inherited form superclass with class methods added to object and properties added to object instances.
var augment = require('active-model').augment;
var Square = augment(Rectangle, {
constructor: function (side) {
Rectangle.call(this, side, side);
}
});
var square = new Square(3)
assert.equal(square.width, 3);
assert.equal(square.height, 3);
assert.equal(square.area(), 9);
assert.equal(square instanceof Rectangle, true);
augment
method based on jashkenas's comment in
Hacker News threadFAQs
Data source agnostic opinionated models for Node.js with classic inheritance, basic validations and a clean API
The npm package active-model receives a total of 9 weekly downloads. As such, active-model popularity was classified as not popular.
We found that active-model 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.