Security News
Combatting Alert Fatigue by Prioritizing Malicious Intent
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
object-schemata
Advanced tools
A tool for defining and using object schemas that can be used to normalize and validate object configurations.
At times we want to pass configuration object as a parameter into a function, but we still want to impose limits on that configuration object. Some of it's properties might be required, or some properties might need to be validated. And how do you go about handling errors when the configuration that was passed in doesn't fit those requirements?
This module seeks to solve those problems.
Important Note
A more comprehensive solution, Fully-Typed, has been developed that supports:
Alternatively this module works fine too.
$ npm install object-schemata
var schema = require('object-schemata');
// define a schema
var personSchema = schema({
age: {
defaultValue: 0,
help: 'This must be a non-negative number.',
transform: function(value) {
return Math.round(value);
},
validate: function(value, is) {
return !is.nan(value) && value >= 0;
}
},
name: {
help: 'This must be a non-empty string.',
required: true,
validate: function(value, is) {
return is.string(value) && value;
}
}
});
// this function takes a configuration object as a parameter
function personData(person) {
var config = personSchema.normalize(person);
return 'Name: ' + config.name + ', Age: ' + config.age;
}
// each of these lines sends a configuration to the personData function
personData({ name: 'Bob', age: 12.5 }); // Name: Bob, Age: 13
personData({ name: 'Bob' }); // Name: Bob, Age: 0
personData({}); // Throws Error with message: OSError EREQ : Missing required configuration property: name. This must be a non-empty string.
personData({ name: 'Bob', age: -5 }); // Throws Error with message: OSError EVALID : Invalid configuration value for property: age. This must be a non-negative number. Received: -5
The schema configuration consists of a property name and its associated schema configuration. The schema configuration can take multiple properties, none of which are required.
Once you have created a schema you have access to these functions below. Each of these examples are preceeded by this code:
var schema = require('object-schemata');
var personSchema = schema({
age: {
defaultValue: 0,
help: 'This must be a non-negative number.',
transform: function(value) {
return Math.round(value);
},
validate: function(value, is) {
return !is.nan(value) && value >= 0;
}
},
name: {
help: 'This must be a non-empty string.',
required: true,
validate: function(value, is) {
return is.string(value) && value;
}
}
});
Object : get ()
Get a copy of the defined schema. If you ever see the need to modify a schema, you'll want to use this function to get the current schema. Then with this object you can modify it and create a new schema. There isn't a way to modify an existing schema beyond this method due to the dangers of mutable objects.
Returns: An copy object of the current schema.
...
//get a copy of the person schema
var copy = personSchema.get();
/*
Value in copy:
{
age: {
defaultValue: 0,
help: 'This must be a non-negative number.',
transform: function(value) {
return Math.round(value);
},
validate: function(value, is) {
return !is.nan(value) && value >= 0;
}
},
name: {
help: 'This must be a non-empty string.',
required: true,
validate: function(value, is) {
return is.string(value) && value;
}
}
}
*/
Object : normalize ( configuration : Object )
Pass an object into this function and get a normalized copy back. This will run the passed in configuration through validations and transformations. Note: the normalized object will not have properties that are not defined in the schema.
Parameters:
Returns: A normalized copy of the passed in object.
...
var config = personSchema.normalize({ name: 'Bob', age: 12.5 });
/*
Value in config:
{
name: 'Bob',
age: 13
}
*/
Mixed : validate ( [ propertyName : String ], value : Mixed )
Validate a single value against a property, or validate an entire object. If validation passes true will be returned, otherwise an Error object will be returned. If you want to throw the error, make sure that you do so.
Parameters:
Returns: true if validation was passed successfuly or an Error object if validation failed.
Example 1:
...
var result = personSchema.validate({ age: 12.5 });
/*
Value in result is an Error object with this message:
OSError EREQ : Missing required configuration property: name. This must be a non-empty string.
*/
Example 2:
...
var result = personSchema.validate('age', -5);
/*
Value in result is an Error object with this message:
OSError EVALID : Invalid configuration value for property: age. This must be a non-negative number. Received: -5
*/
FAQs
A tool for defining and using object schemas that can be used to normalize and validate object configurations.
The npm package object-schemata receives a total of 97 weekly downloads. As such, object-schemata popularity was classified as not popular.
We found that object-schemata 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
In 2023, data breaches surged 78% from zero-day and supply chain attacks, but developers are still buried under alerts that are unable to prevent these threats.
Security News
Solo open source maintainers face burnout and security challenges, with 60% unpaid and 60% considering quitting.
Security News
License exceptions modify the terms of open source licenses, impacting how software can be used, modified, and distributed. Developers should be aware of the legal implications of these exceptions.