npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

rest-payload-validator

v1.0.6

Published

Easy and powerful payload validator for your nodejs projects

Downloads

11

Readme

Rest Payload Validator

npm

Easy and powerful payload validator for your nodejs projects. This module works server-side and client-side

Installation

Import this module on your project

Using NPM

npm install --save rest-payload-validator

Using Yarn

yarn add rest-payload-validator

Usage

Basic validator structure

/* Build a new instance of validator */

// Using build
Validator.build({
  values: {},   // Input data
  rules: {},    // Rules to validate
  messages: {}  // Custom error messages
})

// Using builder
Validator.builder()
  .values({})   // Input data
  .rules({})    // Rules to validate
  .messages({}) // Custom error messages
.build()


/* Check validation result */

// Using boolean condition
if(validator_instance.alright()){
  // ...
}
else {
  console.log(validator_instance.errors)
}

// Using fallbacks
.alright((data) => {})
.failed((errors) => {})

Example: Object validation

Validator.build({
  values: {
    name: "abcd",
    age: 21,
    email: "[email protected]"
  },
  rules: {
    'name': "required|string",
    'age': "required|integer",
    'email': "required|email"
  },
  messages: {
    'name.string': "Custom message for 'name.string'",
    'name.required': "Custom message for 'name.required'",
    'age.string': "Custom message for 'age.string'",
    'age.required': "Custom message for 'age.required'",
    'email.string': "Custom message for 'email.string'",
    'email.required': "Custom message for 'email.required'",
    // or
    'name': {
      'string': "Custom message for 'name.string'",
      'required': "Custom message for 'name.required'",
    },
    'age': {
      'string': "Custom message for 'age.string'",
      'required': "Custom message for 'age.required'",
    },
    'email': {
      'string': "Custom message for 'email.string'",
      'required': "Custom message for 'email.required'",
    }
  }
})
.alright((data) => {
  // ...
})
.failed((errors) => {
  // ...
})

Example: Objects into object

Validator.build({
  values: {
    user: {
      address: {
        street: "abc",
        zip_code: 123
      }
    }
  },
  rules: {
    'user': {
      'address': {
        'street': "string",
        'zip_code': "integer"
      }
    }
  },
  messages: {
    'user': {
      'address': {
        'street.string': "Custom message for 'street.string'",
        'zip_code.integer': "Custom message for 'zip_code.integer'",
        // or
        'street': {
          'string': "Custom message for 'street.string'"
        },
        'zip_code': {
          'integer': "Custom message for 'zip_code.integer'"
        }
      }
    }
  }
})
.alright((data) => {
  // ...
})
.failed((errors) => {
  // ...
})

Example: Lists, Collections and Entries

/* Array of values (List)*/

Validator.build({
  values: {
    list: [
      1, 2, 3, 4
    ]
  },
  rules: {
    'list': ["integer"]
  },
  messages: {
    'list.array': "Custom message for 'list.array'",
    'list.*': {
      'integer': "Custom message for '{n}.integer'"
    },
    // or
    'list': {
      'array': "Custom message for 'list.array'",
      'integer': "Custom message for '{n}.integer'"
    }
  }
})
.alright((data) => {
  // ...
})
.failed((errors) => {
  // ...
})
/* Array of objects (Collection) */

Validator.build({
  values: {
    collection: [
      {
        text: "123",
        number: 123
      },
      {
        text: "456",
        number: 456
      }
    ]
  },
  rules: {
    'collection': [{
      'text': "string|min:6|max:256",
      'number': "integer"
    }]
  },
  messages: {
    'collection.*': {
      'text.string': "Custom message for 'text.string'",
      'text.min': "Custom message for 'text.min'",
      'text.max': "Custom message for 'text.max'",
      'number.string': "Custom message for 'number.string'"
      // or
      'text': {
        'string': "Custom message for 'text.string'",
        'min': "Custom message for 'text.min'",
        'max': "Custom message for 'text.max'"
      },
      'number': {
        'string': "Custom message for 'number.string'"
      }
    }
  }
})
.alright((data) => {
  // ...
})
.failed((errors) => {
  // ...
})
/* Array of Arrays (Entries) */

Validator.build({
  values: {
    entries: [
      [ 1, 2, 3, 4 ],
      [ 'a', 'b', 'c', 'd' ]
    ]
  },
  rules: {
    'entries': [
      ["integer"],
      ["string"]
    ]
  },
  messages: {
    'entries'{
      '0': {
        '*': {
          'integer': "Custom message"
        }
      },
      '1': {
        '*': {
          'string': "Custom message"
        }
      }
      // or
      '0.*': {
        'integer': "Custom message"
      },
      '1.*': {
        'string': "Custom message"
      }
    }
  }
})
.alright((data) => {
  // ...
})
.failed((errors) => {
  // ...
})

Revalidate

const values = {
  variable1: 1,
  variable2: 2
}

const validation = Validator.build({
  values,
  rules: {
    'variable1': "string",
    'variable2': "string"
  }
})

// First check without errors
if(validation.alright()){
  return;
}

// Change values
if('variable1.string' in validation.errors)
  values.variable1 = "text-value1"

if('variable2.string' in validation.errors)
  values.variable2 = "text-value2"

// Revalidate
if(validation.alright()){
  return;
}

Messages

You can override the default error message using the a custom field message.

Basic message

{
  'field.rule1': "Custom message",
  'field.rule2': "Custom message"
}

Grouped by field name

{
  'field': {
    'rule1': "Custom message",
    'rule2': "Custom message"
  }
}

Message for all items in an array

/* List of values */

{
  'field': {
    '*': {
      'rule1': "Custom message",
      'rule2': "Custom message"
    }
  }
  // or
  'field.*'{
    'rule1': "Custom message",
    'rule2': "Custom message"
  }
}
/* Collection of objects */

{
  'field': {
    '*': {
      'field1': {
        'rule1': "Custom message",
        'rule2': "Custom message"
      },
      'field2': {
        'rule1': "Custom message",
        'rule2': "Custom message"
      }
    }
  },
  // or
  'field.*': {
    'field1': {
      'rule1': "Custom message",
      'rule2': "Custom message"
    },
    'field2': {
      'rule1': "Custom message",
      'rule2': "Custom message"
    }
  }
}
{
  'entries'{
    '*': {
      '*': {
        'integer': "Custom message"
      }
    },
  }
  // or
  'entries.*'{
    '*': {
      'integer': "Custom message"
    }
  }
}

Message for some specific items in an array

/* List of values */

{
  'field': {
    '0': {
      'rule1': "Custom message",
      'rule2': "Custom message"
    },
    '1': {
      'rule1': "Custom message",
      'rule2': "Custom message"
    }
  }
}
/* Collection of objects */

{
  'field': {
    '0': {
      'field1': {
        'rule1': "Custom message",
        'rule2': "Custom message"
      },
      'field2': {
        'rule1': "Custom message",
        'rule2': "Custom message"
      }
    },
    '1': {
      'field1': {
        'rule1': "Custom message",
        'rule2': "Custom message"
      },
      'field2': {
        'rule1': "Custom message",
        'rule2': "Custom message"
      }
    }
  }
}
/* Entries */
{
  'entries'{
    '0': {
      '*': {
        'integer': "Custom message"
      }
    },
    '1': {
      '*': {
        'string': "Custom message"
      }
    }
    // or
    '0.*': {
      'integer': "Custom message"
    },
    '1.*': {
      'string': "Custom message"
    }
  }
}

Global messages

Global messages replace all messages in the same or lower scope. Local messages have higher priority.

{
  'rule1': "Custom message"
  'rule2': "Custom message (will not appear)",

  'field': {
    'rule2': "Custom message (override the global)"
  }
}

Default supported validations

Rule | Parameter | Description -------------|-------------|------------ required | N/A | Check if the field is defined or is not a empty string. integer | N/A | Check if the field is a valid integer value float | N/A | Check if the field is a valid float value number | N/A | Check if the field is a valid number value string | N/A | Check if the field is a valid string value array | N/A | Check if the field is an array. Defining rules as 'rule': [] validates as array by default. object | N/A | Check if the field is an object. Defining rules as 'rule': {} validates as object by default. email | N/A | Check if the field is a valid email value min | integer | This validation have 2 modes. When in a string field, check if the length of the text is greater than @param. In a numeric field, check if the value is greater than @param. max | integer | This validation have 2 modes. When in a string field, check if the text length is less than @param. In a numeric field, check if the value is less than @param.

Extra validations

Rule | Parameter | Description -------------|-------------|------------ cpf | N/A | Check if the field is a valid cpf (Brazilian Natural Persons Register) value.

Custom validation

You can implement your own validations. Validation passes when no error message is returned.

/* Custom validation template */

Validator.custom('custom_rule', (key, value, param) => {
  if(/* condition */)
    return /* Error message */;
  /* Valid result */
})

Validator.build({
  values: {
    field: 123
  },
  rules: {
    'field': "custom_rule"
  }
})

Maintainer