npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

data-defender

v1.1.0

Published

A database-agnostic simple data schema enforcer for node.js

Downloads

6

Readme

data-defender

A node.js module to let you define and enforce data constraints.

The module is database-agnostic.

The module is also ideal for evaludating incoming request paramters for servers such as HTTP.

NOTE: This module alone does NOT store data in a database and it does NOT read from any database.

How To Install

npm install data-defencer

API

.returnError()

Enable the module to return errors instead of throwing exceptions.

.create(schemaName [string])

Returns a schema object to allow you to define constraints on each data property.

It will return an error if trying to create a schema that has already been created.

Example:

var defender = require('data-defender');
var example = defender.create('example');

example.define('id', {
	type: defender.DATATYPE.UNIQUE
});
example.define('name', {
	type: defender.DATATYPE.STR,
	default: 'No Name',
	validation: myValidationFunction,
	max: 30,
	min: 4
});

.get(schemaName [string])

Returns an already defined schema object.

It will return an error if trying to get a schema that has not been created.

Example:

var example = defender.get('example');

Schema Object

It is an instance of Struct class.

This object allows you to define constraints on each data property.

.define(PropertyName [string], constraints [object])

If there is an error, it will return an error.

constraints:

{
	type: [enum],
	default: [*default value],
	validation: [*function],
	max: [*number or date],
	min: [*number or date] 
}
type [enum]

Defines the data type of the property.

This is required.

The possible values are:

var defender = require('data-defender');

// number type
defender.DATATYPE.NUM;

// string type
defender.DATATYPE.STR;

// array type
defender.DATATYPE.ARR;

// object type
defender.DATATYPE.OBJ;

// unique type
defender.DATATYPE.UNIQUE;

// date type
defender.DATATYPE.DATE;

// boolean type
defender.DATATYPE.BOOL;

// modified time type
defender.DATATYPE.MOD;

NOTE 1: UNIQUE and MOD data type's value cannot be updated.

NOTE 2: MOD data type property will be updated automatically by updating other properties.

default [*default value]

An optional value to define a default value for the property.

NOTE: By setting this value to null, you can allow null value for this property.

validation [*function]

An optional function used to validated the value for the property.

The validation function is given the value as an argument.

NOTE: Validation function can only performce synchronous operations.

Example:

var defender = require('data-defender');
var validate = function (value) {
	// only odd numbers
	return value % 2 !== 0;
};
var example = defender.create('example');
example.define('oddNum', {
	type: defender.DATATYPE.NUM,
	validation: validate,
	default: 1,
	min: 1,
	max: 99
});
max [*number or date]

An optional value to define the maximum value for the property.

min [*number or date]

An optional value to define the minimum value for the property.

.lockSchema()

Locks the definition of schema. If you call .define() after invoking this function, it will throw an excetion.

.getPropertyNames()

Returns an array of defined property names.

.load(data [*object])

It loads the data and returns a data object.

If the given values do not meet the constraints defined by the schema, it will return an error.

NOTE: If you define a property that is UNIQUE and pass nothing to .load(), it will auto-generate a unique ID as a string.

Example:

var defender = require('data-defender');
var mySchema = defender.create('mySchema');
mySchema.define('id', {
	type: defender.DATATYPE.UNIQUE
});
mySchema.define('name', {
	type: defender.DATATYPE.STR,
	max: 30,
	min: 1
});
mySchema.define('age', {
	type: defender.DATATYPE.NUM,
	min: 0
});

var dbRecord = mySchema.load(dataFromDatabase);

Data Object

An instance of Data class. This object allows you to change the property values of the data loaded according to the schema constraints defined.

Example:

var defender = require('data-defender');
var mySchema = defender.create('mySchema');
mySchema.define('id', {
	type: defender.DATATYPE.UNIQUE
});
mySchema.define('name', {
	type: defender.DATATYPE.STR,
	max: 30,
	min: 1
});
mySchema.define('age', {
	type: defender.DATATYPE.NUM,
	min: 0
});

var dbRecord = mySchema.load(dataFromDatabase);
dbRecord.update('name', 'New Name');
dbRecord.update('age', dbRecord.get('age') + 4);
.update(propertyName [string], value [mix])

Updates the property value of the name given with the value passed to the function.

.get(propertyName [string])

Returns the current value of the given property.

.toJSON()

Returns all the property along with their values as an object.

Useful when you need to store the data to database etc.

Example:

var dataToSave = myData.toJSON();
saveToDatabase(dataToSave, callback);