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

sanitation

v1.0.10

Published

Iterates over nested JSON objects to fetch the value and performs operation like sanity validation of data along with blank value and mandatory fields check

Downloads

61

Readme

sanitation

This is a simple sanity validator which can be used across various Node.JS applications to iterate over the "nested JSON objects structure" and perform operations like validation of incoming API data or any JSON data to return the expected object filtering out the junk data from the object, along with that has also the ability to extract the last folder from the given folder path.

Listed below some of the key features

  • Very simple and flexible to use.
  • Configurable JSON structure along with defining the expected data type for each field actual value.
  • Configurable mandatory fields and blank value check for optional fields.
  • Can be used for any RESTful API.
  • Can extract the value of the nested object based on string path and the separator defined.

Table of Contents

  • Install
  • Dependencies
  • Use
  • Methods
    • paramsValidator - Validates the actual object across the expected object to remove the junk data and filter out the actual data along with the data type check for each field actual value
    • objValByStr - Fetch the value of the nested object based on string path separated by an identifier
    • lastFolderFromPath - Extract the last folder from the path string
    • isPositiveInteger - Check non zero positive integer value from the string
    • isPositiveIntegerArray - Check non zero positive integer value from the string in an array
    • isValidEmail - Check valid email
    • isValidDate - Check valid calendar date along with the specified date format and covert to expected date format if required

Install

npm install sanitation

Dependencies

"dependencies": {
    "moment": "2.17.1"
};

Use

let sanity = require('sanitation');

let expectedObj = {
    "test" : {
        "schema" : {
            "blank_value" : false,
            "elements" : {
                "key1" : {
                    "key2" : [
                        {
                            "key3" : "",
                            "key4" : 0
                        }
                    ],
                    "key5" : "",
                    "key6" : []
                }
            },
            "mandatory_elements" : ["key1", "key2", "key3", "key4", "key5"]
        }
    }
};

Methods

paramsValidator

/**
* Validate the actual object across the expected object to remove the junk data and filter out the actual data along with the data type check for each field actual value
**/

let actualObj = {
    "key1" : {
        "key2" : [
            {
                "key3" : "value1",
                "key4" : value2
            }
        ],
        "key5" : "value3",
        "key7" : ["value4", "value5"]
    }
};

let validation = sanity.paramsValidator(actualObj, expectedObj.test.schema.elements, expectedObj.test.schema.mandatory_elements, expectedObj.test.schema.blank_value);

if(!validation.success) {
    /**
    * Define your handler for error scenario
    * validation.response.errorMsg will contain the actual error string
    * validation.response.errorKey will contain the error keys
    **/
} else if (Object.keys(validation.elements).length === 0) {
    /**
    * Define your handler for no valid data in the actual object in case all the fields in the expected object are optional
    **/
} else {
    /**
    * validation.elements will contain the filtered data compared with the expected object from the actual object
    **/
}

objValByStr

/**
* Fetch the value of the nested object based on string path separated by dot
**/

let nestedObj = {
    "key1" : {
        "key2" : {
            "key3" : {
                "key4" : {
                    "key5" : {
                        "key6" : {
                            "key7" : "value"
                        }
                    }
                }
           }
        }
    }
};

let nestedObjVal = sanity.objValByStr("key1.key2.key3.key4.key5.key6.key7", ".", nestedObj);

if(!nestedObjVal) {
    /**
    * Check if the keys in the string pattern or the other parameters passed are correct and in proper order
    **/
} else if(typeof(nestedObjVal) === "object") {
    /**
    * Define your handler for error scenario
    **/
} else {
    /**
    * Actual value of the nested object key will be available here
    **/
}

lastFolderFromPath

/**
* Extract the last folder from the path string
**/

let folderPath = "/usr/share/test"

sanity.lastFolderFromPath(folderPath);

isPositiveInteger

/**
* Check non zero positive integer value from the string
**/

let intVal = "1";

sanity.isPositiveInteger(intVal);

isPositiveIntegerArray

/**
* Check non zero positive integer value from the string in an array
**/

let intValArray = ["1", "2"];

sanity.isPositiveIntegerArray(intValArray);

isValidEmail

/**
* Check valid email
**/

let email = "[email protected]";

sanity.isValidEmail(email);

isValidDate

/**
* Check valid calendar date along with DD/MM/YYYY date format and convert to YYYY-MM-DD as the output, third parameter is optional
**/

let date = "12/12/2016";

let validDate = sanity.isValidDate(date, "DD/MM/YYYY", "YYYY-MM-DD");

if(!validDate) {
    /**
    * Define your handler for error scenario
    **/
} else {
    /**
    * Define your handler for success scenario, return value will be either the formatted date or boolean based on the third parameter passed or not
    **/
}