1.0.10 • Published 5 years ago

express-input-validation v1.0.10

Weekly downloads
-
License
ISC
Repository
-
Last release
5 years ago

express-input-validation

npm version

An express.js middleware for input validation.

logo-image

Securing your APIs by validating the inputs.

express-input-validation is simple yet powerful node module that makes your API robust and discard any request with invalid input.

Installation

npm install express-input-validation

OR

yarn add express-input-validation

Features

you can ask express-input-validation to get value from any of these(following list) using from key in rules array,

  • Header (header)
  • Query (query)
  • Body (body)
  • Params (param)

and can pass any custom validation function using validation_function in the rules array. Also you can pass a message in rules for particular rule or it'll be default send a invalid ${key_name} as message.

Example

Following are the example for validating using express-input-validation, here I have included all four type of inputs that most commonly an API can have:

    {
        "from": "query",
        "key": "emailId",
        validation_function: value => value.indexOf("@") > -1,
        "datatype": "number",
        "is_optional": true
    }

In rules array the above one can be one of the objects, here I've showed a custom validation_function without message, this ll return Invalid emailId incase the email id is not found or if it doesn't have an @ in the value string.

Now new rule can be defined as is_optional, where if its declared as optional, only if the field value found then it'll be validated otherwise it'll be ignored.

Datatype-check (optional)

In the rule you can also specify a datatype in datatype key to check for specific datatype of the value. datatypes can be checked below.

DatatypeDescriptionAlternative key
numberchecks if its a valid numbernum, int, integer
stringchecks if typeof is string and first character is not a numericstr
floatchecks if its a number and has a decimal value-
objectchecks if a objectobj
alphanumericchecks if value has atleast one character and number-
only-alphachecks if value has only alphabets-

Note

  • key, from are required keys in object in all of the rule you define.
  • a rule without a validation_function is considered to be only required field, i.e., express-input-validation only checks if there is a field in specified from key in the object.
  • Datatype is still experimential.

Roadmap

Raodmap

Next-Release

  • Documentation for Getting Started
  • More DataType checks and datetime checks(minor).
  • Code Optimization.
  • Build passing badge after creating test cases.

License

MIT License

1.0.10

5 years ago

1.0.9

5 years ago

1.0.8

5 years ago

1.0.7

5 years ago

1.0.6

5 years ago

1.0.5

5 years ago

1.0.4

5 years ago

1.0.3

5 years ago

1.0.2

5 years ago

1.0.1

5 years ago

1.0.0

5 years ago