request-stripdeprecated

Validate all types of objects, be it input data, node request data or event state

Usage no npm install needed!

<script type="module">
  import requestStrip from 'https://cdn.skypack.dev/request-strip';
</script>

README

request-strip

npm version

This is a module that helps validate object data against a developer defined schema, borrowing heavily from mongoose schema as well as Auth0 additionalSignupField options.

Installation

npm install --save request-strip

Extra Documentation

Validation

Considering field options

To determine if your input object, or user-supplied data is valid, you need to test it against a predetermined schema. Let's start off by creating a schema for a simple account creation screen.

However before we start lets consider what type of validation we need on our fields.

Username

  • Minimum length of 7 characters
  • It should be a required field
  • Default value of 'Anonymous'

schema equivalent

username: {
  minLength: 7,
  required: true,
  default: 'Anonymous'
}

First Name

  • It should be required
  • Minimum we should have 2 characters

schema equivalent

firstName: { required: true, minLength: 2 }

Password

  • minimum of 8 characters
  • it should be required
  • alpha, special character, and numeric representation

schema equivalent

password: { required: true, regex: /^(?=.[a-z])(?=.[A-Z])(?=.[0-9])(?=.[$@$!%*?&]){8,}/ }

Last Name

Last names is optional

Creating the schema

Now that we have a simple idea of an account creation schema, we can add them together and test them against our incoming data.

First we will create the accountSchema object.

accountSchema

const accountSchema = {
  username: {
    minLength: 7,
    required: true,
    default: 'Anonymous'
  },
  firstName: { required: true, minLength: 2 },
  password: { required: true, regex: /^(?=.[a-z])(?=.[A-Z])(?=.[0-9])(?=.[$@$!%*?&]){8,}/ }
};

Now we can create some mock data to test the validation.

mockInput

const mockInput = {
  username: 'Bob',
  firstName: 'Bob',
  lastName: 'Smith',
  password: 'Candy123!'
}

Finally, we do the validation.

validation

const { validate } = require('request-strip');

const tested = validate(mockInput, accountSchema);

if( tested.valid ) {
  // we have valid data!!!
}
else {
  console.log(JSON.stringify(tested, null, 2);
}

The above example fails because our input data does not match what we were expecting within our schema.

The console.log output explains what happened.

output

{
  "errors": [
    "username requires a minimum of 7 characters",
    "password does not match required format"
  ],
  "errorMap": {
    "username": "username requires a minimum of 7 characters",
    "password": "password does not match required format"
  },
  "valid": false,
  "json": {
    "username": "Bob",
    "firstName": "Bob",
    "password": "Candy123!"
  },
  "string": "{\"username\":\"Bob\",\"firstName\":\"Bob\",\"password\":\"Candy123!\"}"
}

Looks like our username and password didn't meet the schema requirements we provided.

We can now surface the errors to the user easily by key value.

errorMap

...
const tested = validate(mockInput, accountSchema);

// set input types or return request with errors found
console.log(tested.errorMap.username);
console.log(tested.errorMap.password);