@springworks/input-validator
Advanced tools
Comparing version 4.0.0 to 4.0.1
{ | ||
"name": "@springworks/input-validator", | ||
"version": "4.0.0", | ||
"version": "4.0.1", | ||
"description": "Module to help validate input parameters.", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
# Node.js Input Validator module | ||
Simple Node.js module to validate and filter input parameters. | ||
Simple Node.js module to validate input parameters. | ||
@@ -8,4 +8,2 @@ [![Build Status](https://travis-ci.org/Springworks/node-input-validator.png?branch=master)](https://travis-ci.org/Springworks/node-validator) | ||
## Usage | ||
``` | ||
@@ -15,76 +13,20 @@ npm install --save @springworks/input-validator | ||
Include module and use validation functions: | ||
```js | ||
var input_validator = require('@springworks/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: | ||
```js | ||
// 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); | ||
``` | ||
### `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. | ||
```js | ||
var schema = Joi.object().keys({ | ||
'username': Joi.string().required(), | ||
'password': Joi.string().required() | ||
}), | ||
validated; | ||
validated = Joi.validate({ | ||
'username': 'foo', | ||
'password': 'bar' | ||
}, schema); | ||
Validates `obj` against a `joi` schema. It will throw an error if validation fails. | ||
/* | ||
Validated is now equal to: | ||
{ | ||
'username': 'foo', | ||
'password': 'bar' | ||
} | ||
*/ | ||
``` | ||
### ~~`isMissingParams(params, required)`~~(deprecated) | ||
Checks if any of the required params are missing. | ||
```js | ||
// 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); | ||
``` | ||
import { joi, validateSchema } from '@springworks/input-validator'; | ||
### ~~`missingParams(params, required)`~~(deprecated) | ||
Checks if any of the required params are missing. Returns any missing params in an array. | ||
```js | ||
// 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); | ||
``` | ||
const schema = joi.object().keys({ | ||
username: joi.string().required(), | ||
password: joi.string().required() | ||
}); | ||
### ~~`invalidTypeParams(params, types)`~~(deprecated) | ||
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. | ||
```js | ||
// 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); | ||
const validated = validateSchema({ | ||
'username': 'foo', | ||
'password': 'bar' | ||
}, schema); | ||
``` | ||
## Tests | ||
Run `npm test` to run complete unit tests with *Istanbul* code coverage. |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
0
8996
31