
Company News
/Security News
Socket Selected for OpenAI's Cybersecurity Grant Program
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.
node-filter
Advanced tools
var filter = require('filter')
// Synchronous
try {
filter.validate('hello', 'string', {'min': 10})
console.log('Valid')
} catch (e) {
console.log('Invalid', e.message) // → "Invalid", "too short"
}
// Asynchronous
filter.validate('hello', 'string', {'min': 10}, function(err, value) {
if (!err) {
console.log('Valid')
} else {
console.log('Invalid', err.message) // → "Invalid", "too short"
}
})
// Synchronous
try {
console.log(filter.sanitize('hello', 'string', {'min': 10})) // → "hello "
} catch (e) {
console.log('Failed')
}
// Asynchronous
filter.sanitize('hello', 'string', {'min': 10}, function(err, value, original) {
if (err) {
console.log('Failed')
} else {
console.log(value) // → "hello "
}
})
filter.help('string')
will output:
Help for filter string:
| Standard string validations
| Options:
| * min: Minimum length (default value = 0)
| * max: Maximum length (default value = null)
| * pattern: Regexp that should be matched (default value = null)
| * replace: Replacement for given pattern, used for sanitization only (default value = null)
This filter just does nothing. It always validates, and sanitization always returns original value.
This filter takes no option.
This filter will be used to validate and cleanup strings.
Available options:
TODO
TODO
TODO
The simpliest way is to declare your filter as a module:
module.exports = {
description: "Description of your filter, used by help()", // Optional
validate: function(value, options) { /* Throws an error if value is not valid. Optionnally returns sanitized value */ },
sanitize: function(value, options) { /* Returns sanitized value */ }, // Optional, if not present 'validate' will be used
options: {
my_option: { description: "Description of this option, used by help()", default: default_value },
…
}
}
Suppose you declared your filter this way:
{
validate: function() { return true; /* this filter does not make validations */ },
sanitize: function(value, options) { return value + options.added },
options: { added: { default: 0 } }
}
It will be used this way:
var filter = require('filter')
console.log(filter.sanitize(3, {added: 4})) // → 7
Note:
You can add your filter directly:
filter.add('my_filter', { … })
If you wrote it as a module, you can use the module's name:
filter.add('my_filter', './my_filter_module')
But you could prefer to let the filters declare themselves:
enabled_filters in current working directory.{ name: filter }.For example, you could have this structure:
./
+- app.js
+- enabled_filters.js → module.exports = { my_filter: './my_custom_filter' }
+- my_custom_filter.js → module.exports = { … }
This way, no need to call filter.add(…), it's automatically done when module filter is loaded.
FAQs
Validation and sanitization API inspired from PHP's filters
The npm package node-filter receives a total of 2 weekly downloads. As such, node-filter popularity was classified as not popular.
We found that node-filter demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 0 open source maintainers 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.

Company News
/Security News
Socket is an initial recipient of OpenAI's Cybersecurity Grant Program, which commits $10M in API credits to defenders securing open source software.

Security News
Socket CEO Feross Aboukhadijeh joins 10 Minutes or Less, a podcast by Ali Rohde, to discuss the recent surge in open source supply chain attacks.

Research
/Security News
Campaign of 108 extensions harvests identities, steals sessions, and adds backdoors to browsers, all tied to the same C2 infrastructure.