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 🙏

© 2026 – Pkg Stats / Ryan Hefner

remove-unknown-properties

v1.0.3

Published

Remove unknown properties from a given object

Downloads

34

Readme

remove-unknown-properties

Remove unknown properties from a given object. it remove unknown properties in deeply nested object. work in all javascript enviroments.

node

Coverage Status

MIT License

Installation

Install remove-unknown-properties

  #npm
  npm install remove-unknown-properties

  #yarn
  yarn add remove-unknown-properties

Usage/Examples

import { removeUnknown } from 'remove-unknown-properties'

const student = {
    name: 'adam',
    age:16,
    salary:1000, //unknown property
    address:{
        country: 'usa',
        city: 'la',
        department:'any', //unknown property
        codes:{
            postalCode:1100,
            zipCode:1100,
            barcode:"barcode" //unknown property
        }
    }
}

const schema = {
    name: String,
    age: Number,
    address:{
        country: String,
        city: String,
        codes:{
            postalCode:Number,
            zipCode:Number
        }
    }
}

/**
 * this function will remove unknown properties from student object based on * * schema.
 * it will delete salary, department, and barcode properties.
 * note:this function will mutate original object.
 */
removeUnknown(student, schema);

console.log(student);
// result will be =>
/**
 * {
 *  name: 'adam',
 *  age:16,
 *  address:{
 *    country: 'usa',
 *    city: 'la',
 *    codes:{
        postalCode:1100,
        zipCode:1100
      }
 *  }
 * }
 * /

Params

  • obj - The object to remove unknown properties from it. the obj must be native javascript object otherwise will throw an error.
  • schema - The schema of how obj should look like. the schema must be native javascript object otherwise will throw an error.
  • options - This parameter is optional. It is an object contain strict property with type boolean. when it is true the removeUnknown function will remove known properties that has different type. default is false.

Schema

There are multiple way to create schema.

  • using javascript built in types constructor.
const schema = {
    name: String,
    age: Number,
    address:{
        country: String,
        city: String,
    },
    pohonNumbers: Array
}
  • using Types.
import { Types } from 'remove-unknown-properties';

const schema = {
    name: Types.String,
    age: Types.Number,
    address:{
        country: Types.String,
        city: Types.String,
    },
    pohonNumbers: Types.Array
}
  • using defualt values.

const schema = {
    name: "",
    age: 0,
    address:{
        country: "any",
        city: "city name",
    },
    pohonNumbers: []
}

Strict

When strict is true removeUnknown will remove unknown properties and known properties from obj if it is has different type from schema.

NOTE: When set strict to true you must use Types to build your schema to get accurate result. otherwise you will lose your data.

import { removeUnknown, Types } from 'remove-unknown-properties';

const student = {
    name: 'adam',
    age:"16", //will remove becouse it string not number
    stage: "any",
    salary:1000, //unknown property
    phoneNumbers:['12345678','87654321']
}

const schema = {
    name: Types.String,
    stage: Types.String,
    age: Types.Number,
    phoneNumbers:Types.Array
}

removeUnknown(student, schema, {strict: true});
// result will be =>
/**
 * {
 *  name: 'adam',
 *  stage:'any',
 *  phoneNumbers: ['12345678','87654321']
 * }
 * /

Usecases

There are a lot of use cases one of theme to use it with node js and express to remove unknown and harmful data from request body.

  • first create reusable function that take schema and return express middleware
//file removeUnknownMiddleware.js
import { removeUnknown } from 'remove-unknown-properties';

export function removeUnknownMiddleware(schema) {
  return (req, res, next) => {
    removeUnknown(req.body, schema);
    next();
  };
}
  • next add your middleware to your route
//file user.router.js
import { removeUnknownMiddleware } from '../middleares/removeUnknownMiddleware';
import { userSchema } from '../schema/userSchema';

router.post('/add-user', removeUnknownMiddleware(userSchema), createUserController);