
Product
Rust Support in Socket Is Now Generally Available
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.
Small declarative rules framework designed primarily for use when validating incoming data, such as JSON coming into services.
You create an object to declare the rules/invariants you want to apply (something akin to a schema). A fluent interface makes it easy to specify the invariants for each property:
#####JavaScript
var nameRules = {
first : mustBe().populated().string({ minLength: 5, maxLength: 20}),
second : mustBe().populated().string({ minLength: 5, maxLength: 20}),
}
var personRules = {
name: nameRules,
weight: mustBe().populated().numeric({min : 0, max: 130}),
dateOfBirth: mustBe().date({ before: now.subtract("years", 1) })
}
You can also do inline validation:
var doSomeStuff = function(name, age) {
ensure(name).populated().string();
ensure(age, "age").integer();
}
#####CoffeeScript
# This schema is not showing how to validate a real address, its just an example that makes it easy to test the framework
addressRules = {
streetOne: mustBe().populated()
streetTwo: -> @.populated().string( minLength: 10, maxLength : 50 ) [2]
streetThree: -> @.populated().string( minLength : 10, maxLength: 50)
town: -> @.populated()
postCode: -> @.populated().matchFor(/.../)
}
As shown you can access this fluent interface using twp approaces:
#####Triggering validation You trigger validation using:
result = rules.apply(person, personRules)
The returned object has the per-property details of any validation failures, e.g.:
{
name: {
first: {
message: 'The value must be populated.',
type: 'not_populated',
value: ''
},
second: {
message: 'The value must be populated.',
type: 'not_populated',
value: undefined }
},
weight: {
message: 'The value must be populated.',
type: 'not_populated',
value: undefined
}
}
Note in this case both the first name (e.g. person.name.first) and second name (person.name.second) needed to be populated, along with the weight.
The project comes with examples in the examples directory:
node examples/person
coffee examples/address
Note that if you are using sublime you can get the alignment shown in the person example using the sublime text alignment package.
The framework comes with several validators, to understand them further you may want to run the examples.
populated - Checks the value is not null, undefined, "", or an empty array.arraynumeric - Optionally you can also pass in object with min and/or max valuesmatchFor - You can pass in an object with pattern and optionally flags, alternatively you can pass in the RegExp object to use.date - Optionally you can specify that the date must be before and/or after specified dates. To make this easier you use now.add or now.subtract to specify the dates to use for before/after.string - Optionally you can pass in minLength and/or maxLength.First install mocha:
npm install mocha -g
Run the tests using npm test or:
mocha -R spec spec/ -w -G --recursive -b
FAQs
Validate objects against predefined rules (node.js).
The npm package rules receives a total of 192 weekly downloads. As such, rules popularity was classified as not popular.
We found that rules 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.

Product
Socket’s Rust and Cargo support is now generally available, providing dependency analysis and supply chain visibility for Rust projects.

Security News
Chrome 144 introduces the Temporal API, a modern approach to date and time handling designed to fix long-standing issues with JavaScript’s Date object.

Research
Five coordinated Chrome extensions enable session hijacking and block security controls across enterprise HR and ERP platforms.