Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

@springworks/input-validator

Package Overview
Dependencies
Maintainers
1
Versions
449
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

@springworks/input-validator

Module to help validate and filter input parameters.

  • 3.0.4
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
928
increased by11.54%
Maintainers
1
Weekly downloads
 
Created
Source

Node.js Input Validator module

Simple Node.js module to validate and filter input parameters.

Build Status Coverage Status

Usage

npm install --save @springworks/input-validator

Include module and use validation functions:

var input_validator = require('input-validator');

// Filter object to only include `foo` and `bar`
var filtered = input_validator.filterParams({ foo: 1, bar: 2, baz: 3 }, ['foo', 'bar']);

API

filterParams(params, allowed)

Ensures a params object only has the allowed parameters:

// Remove all params but for `foo` or `bar`
var filtered = input_validator.filterParams({ foo: 1, bar: 2, baz: 3 }, ['foo', 'bar']);
assert(filtered.length === 2);

isMissingParams(params, required)

Checks if any of the required params are missing.

// Ensure both `foo` and `bar` are included in params object
var is_missing_params = input_validator.isMissingParams({ foo: 1 }, ['foo', 'bar']);
assert(is_missing_params === true);

missingParams(params, required)

Checks if any of the required params are missing. Returns any missing params in an array.

// Ensure both `foo` and `bar` are included in params object, and return missing params in an array
var missing_params = input_validator.missingParams({ foo: 1 }, ['foo', 'bar']);
assert(missing_params.length === 1);

isTypesValid(params, types)

Checks that the parameters have the correct datatype. Returns true if all params are of valid types.

var has_valid_params = input_validator.isTypesValid({foo: 'baz', bar: 'foz'}, {foo: String, bar: Boolean});
assert(has_valid_params === false);

invalidTypeParams(params, types)

Checks that the parameters have the correct datatype. Returns parameters that don't have the correct datatype in an array. Returns an empty array if all parameters are of the correct datatype.

// Ensure that `foo` is a String and `bar` is a Boolean.
var invalid_params = input_validator.invalidTypeParams({foo: 'baz', bar: 'foz'}, {foo: String, bar: Boolean});
assert(invalid_params.length === 1);

getType(obj)

Returns a string with the name of the datatype of obj.

assert(input_validator.getType('this is a string') === 'String');
assert(input_validator.getType(1337) === 'Number');
assert(input_validator.getType(true) === 'Boolean');
assert(input_validator.getType([]) === 'Array');
assert(input_validator.getType({}) === 'Object');

getType(obj)

Returns a string with the name of the datatype of obj.

assert(input_validator.getType('this is a string') === 'String');
assert(input_validator.getType(1337) === 'Number');
assert(input_validator.getType(true) === 'Boolean');
assert(input_validator.getType([]) === 'Array');
assert(input_validator.getType({}) === 'Object');

validateSchema(obj, schema, resource_name, options)

Validates obj against a Joi.schema. It will filter unknown keys. It will throw an error if validation fails.

var schema = Joi.object().keys({
      'username': Joi.string().required(),
      'password': Joi.string().required()
    }),
    validated;

    validated = Joi.validate({
      'username': 'foo',
      'password': 'bar'
    }, schema);

    /*
      Validated is now equal to:
      {
        'username': 'foo',
        'password': 'bar'
      }
    */

Tests

Run npm test to run complete unit tests with Istanbul code coverage.

Keywords

FAQs

Package last updated on 30 Sep 2015

Did you know?

Socket

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.

Install

Related posts

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