Socket
Socket
Sign inDemoInstall

muxrpc-validation

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

muxrpc-validation - npm Package Compare versions

Comparing version 1.0.5 to 2.0.0

244

index.js
var zerr = require('zerr')
var pull = require('pull-stream')
var errs =
module.exports.errors = {
MissingParam: zerr('Usage', 'Param % is required'),
Type: zerr('Type', 'Param % must by of type %')
}
module.exports = function (addedValidators) {
var api = {}
var validators = {}
// base validator registry
var validators = {
number: function (param, n) {
var asNum = +param
if (isNaN(asNum) || asNum != param)
return 'type'
},
string: function (param, n) {
var asString = ''+param
if (asString != param)
return 'type'
},
boolean: function (param, n) {
if (typeof param != 'boolean')
return 'type'
},
object: function (param, n) {
if (typeof param != 'object' || !param)
return 'type'
},
array: function (param, n) {
if (!Array.isArray(param))
return 'type'
},
function: function (param, n) {
if (typeof param != 'function')
return 'type'
// validator control
api.get = function (name) {
return validators[name]
}
}
api.set = function (name, fn) {
if (name && typeof name == 'object')
for (var k in name)
api.set(k, name[k])
else
validators[name] = fn
}
// validator control
module.exports.get = function (name) {
return validators[name]
}
module.exports.set = function (name, fn) {
if (name && typeof name == 'object')
for (var k in name)
module.exports.set(k, name[k])
else
validators[name] = fn
}
// set validator registry
api.set({
number: function (param, n) {
var asNum = +param
if (isNaN(asNum) || asNum != param)
return 'type'
},
string: function (param, n) {
var asString = ''+param
if (asString != param)
return 'type'
},
boolean: function (param, n) {
if (typeof param != 'boolean')
return 'type'
},
object: function (param, n) {
if (typeof param != 'object' || !param)
return 'type'
},
array: function (param, n) {
if (!Array.isArray(param))
return 'type'
},
function: function (param, n) {
if (typeof param != 'function')
return 'type'
}
})
api.set(addedValidators)
// rpc method wrappers
module.exports.sync = function (fn) {
var spec = Array.prototype.slice.call(arguments, 1)
return function () {
var args = Array.prototype.slice.call(arguments)
// rpc method wrappers
api.sync = function (fn) {
var spec = Array.prototype.slice.call(arguments, 1)
return function () {
var args = Array.prototype.slice.call(arguments)
// run validation
var err = validate(args, spec)
if (err) throw err
// run validation
var err = validate(args, spec)
if (err) throw err
// run sync fn
return apply(this, fn, args)
// run sync fn
return apply(this, fn, args)
}
}
}
module.exports.sink =
module.exports.async = function (fn) {
var spec = Array.prototype.slice.call(arguments, 1)
return function () {
var args = Array.prototype.slice.call(arguments)
var hasCb = (typeof args[args.length - 1] == 'function')
api.sink =
api.async = function (fn) {
var spec = Array.prototype.slice.call(arguments, 1)
return function () {
var args = Array.prototype.slice.call(arguments)
var hasCb = (typeof args[args.length - 1] == 'function')
// get cb
var cb = (hasCb)
? args[args.length - 1]
: function (err) { if (err) { throw err; } }
// get cb
var cb = (hasCb)
? args[args.length - 1]
: function (err) { if (err) { throw err; } }
// run validation
var err = validate((hasCb) ? args.slice(0,args.length-1) : args, spec)
if (err) return cb(err)
// run validation
var err = validate((hasCb) ? args.slice(0,args.length-1) : args, spec)
if (err) return cb(err)
// run async fn
return apply(this, fn, args)
// run async fn
return apply(this, fn, args)
}
}
}
module.exports.source = function (fn) {
var spec = Array.prototype.slice.call(arguments, 1)
return function () {
var args = Array.prototype.slice.call(arguments)
api.source = function (fn) {
var spec = Array.prototype.slice.call(arguments, 1)
return function () {
var args = Array.prototype.slice.call(arguments)
// run validation
var err = validate(args, spec)
if (err) return pull.error(err)
// run validation
var err = validate(args, spec)
if (err) return pull.error(err)
// run stream fn
return apply(this, fn, args)
// run stream fn
return apply(this, fn, args)
}
}
}
// run validation against a spec
function validate (args, spec) {
var err
// run validation against a spec
function validate (args, spec) {
var err
// multiple specs?
if (Array.isArray(spec[0])) {
for (var i=0; i < spec.length; i++) {
err = validate(args, spec[i])
if (!err)
return false // spec passed
// multiple specs?
if (Array.isArray(spec[0])) {
for (var i=0; i < spec.length; i++) {
err = validate(args, spec[i])
if (!err)
return false // spec passed
}
return err // give the last error
}
return err // give the last error
}
// iterate the spec
for (var i=0; i < spec.length; i++) {
var types = parse(spec[i])
// iterate the spec
for (var i=0; i < spec.length; i++) {
var types = parse(spec[i])
for (var j=0; j < types.length; j++) {
var type = types[j]
for (var j=0; j < types.length; j++) {
var type = types[j]
// falsey?
if (!args[i]) {
err = (type.optional) ? false : errs.MissingParam(''+i)
break
}
// falsey?
if (!args[i]) {
err = (type.optional) ? false : errs.MissingParam(''+i)
break
}
// get & run validator
var validator = validators[type.name]
if (!validator)
throw new Error('Validator not found: ' + type.name)
err = validator(args[i], ''+i)
// get & run validator
var validator = validators[type.name]
if (!validator)
throw new Error('Validator not found: ' + type.name)
err = validator.call(api, args[i], ''+i)
// did the validator pass? break out of this type
if (!err)
break
// did the validator pass? break out of this type
if (!err)
break
// error aliases
if (err == 'type')
err = errs.Type(''+i, type.name)
// error aliases
if (err == 'type')
err = errs.Type(''+i, type.name)
}
// none of the types passed? return the error
if (err)
return err
}
return false
}
// none of the types passed? return the error
if (err)
return err
}
return false
return api
}
var errs =
module.exports.errors = {
MissingParam: zerr('Usage', 'Param % is required'),
Type: zerr('Type', 'Param % must by of type %')
}
// parse spec token

@@ -150,0 +158,0 @@ function parse (token) {

{
"name": "muxrpc-validation",
"version": "1.0.5",
"version": "2.0.0",
"description": "Validation library for muxrpc apis",

@@ -5,0 +5,0 @@ "main": "index.js",

@@ -6,4 +6,11 @@ # muxrpc validation

```js
var valid = require('muxrpc-validation')
// create validator library
var valid = require('muxrpc-validation')({
queryOpts: function (v) {
if (v.reverse && typeof v.reverse != 'boolean')
return new TypeError('opts.reverse must be a bool')
}
})
// api manifest
var manifest = {

@@ -16,3 +23,3 @@ usage: 'sync',

// wrap the functions in the validators
// api definition - wrap the functions in the validators
var api = {

@@ -25,9 +32,2 @@ usage: valid.sync(usage, 'string|boolean'), // multiple types

// register special validators
valid.set('queryOpts', function (v) {
if (v.reverse && typeof v.reverse != 'boolean')
return new TypeError('opts.reverse must be a bool')
})
// function defs:
function usage (cmd) {

@@ -34,0 +34,0 @@ // ...

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc