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

obj-valid

v0.7.0

Published

This package can check is your object has correct structure without using typescript. As you know after transpilation interfaces disappear. If you want to check object structure on production or in clean javascript you can use this package.

Downloads

10

Readme

obj-valid

This package can check is your object has correct structure without using typescript. As you know after transpilation interfaces disappear. If you want to check object structure on production or in plain javascript you can use this package.

Please send me feedback or maybe you have any idea to add new functionality to this package.

Installation

Run npm i obj-valid or download it directly from git repository https://github.com/Iicytower/obj-valid

Why you should use it?

This package is very fast, simple to use, it has only one dependency to package without dependencies. You can write schema by yourself or createSchema function can create it for you. If you have any idea how to improve it or what to add, send me an e-mail.

Usage

You have three ways to use it:

1. You can use functions.

import { createSchema, checkSchema } from "obj-valid";
// or
const { createSchema, checkSchema } = require("obj-valid");

const schema = createSchema(objectFromWhichTheSchemaWillBeCreated);

if (!checkSchema(schema, objectToCheck).score) {
  return new Error()
}
  // Your awesome code

2. You can use class

import { Schema } from "obj-valid";
// or
const { Schema } = require("obj-valid");

const schema = new Schema(objectFromWhichTheSchemaWillBeCreated);

if (!schema.check(objectToCheck).score) {
  return new Error()
}
  // Your awesome code

also you can out into constructor your own schema:


const schema = new Schema(yourOwnSchema, true);

if (!schema.check(objectToCheck).score) {
  return new Error()
}
  // Your awesome code

If second argument is true(default false) schema object will be created directly from object in first argument,

3. You can add methods to object prototype. This method is not recomended!

import { configPrototype } from "obj-valid";
// or
const { configPrototype } = require("obj-valid");

configPrototype(); // this function adds methods to object prototype
const schema = objectFromWhichTheSchemaWillBeCreated.createSchema();

if (!objectToCheck.checkSchema(schema).score) {
  return new Error()
}
  // Your awesome code

If you use this method you can't use "createSchema" and "checkSchema" as property name.

Example

import  { Schema  as ObjStructure } from "obj-valid";

const earth = {
  hello: "Earth",
  age: 4543000000,
}

const venus = {
  hello: "Venus",
  age: 4503000000,
}

const world = {
  hello: "world",
}

const pluton = {
  hello: "pluton",
}

const jupiter = {
  bye: "Jupiter",
  age: 4503000000, 
}

const earthSchema = new ObjStructure(earth);
const worldSchema = new ObjStructure(world);

console.log(earthSchema.check(venus).score) // log true
console.log(earthSchema.check(world).score); // log false
console.log(earthSchema.check(jupiter).score); // log false
console.log(worldSchema.check(jupiter).score); // log false
console.log(worldSchema.check(earth).score); // log false
console.log(worldSchema.check(venus).score); // log false
console.log(worldSchema.check(pluton).score); // log true

You can write schema by yourself. Check how schema looks on example object:

import { checkSchema } from "obj-valid";

const schema = {
  hello: "string",
  number: "number",
  nu: "null",
  und: "undefined",
  bool: "boolean",
  obj: {
    ex: "string",
    nu: "number",
    func: "function",
    arr: {
      type: "array",
      value: "number",
    },
  },
  arr: {
    type: "array",
    value: "number",
  },
  tab: {
    type: "array",
    value: "string",
  },
  reg: "regexp",
  multidimensionalArr: {
    type: "array",
    value: {
      type: "array",
      value: "string",
    },
  },
  objArr: {
    type: "array",
    value: {
      nu: "null",
      simple: "string",
      arr: {
        type: "array",
        value: "number",
      },
      insObjArr: {
        type: "array",
        value: {
          name: "string",
          age: "number",
        },
      },
    },
  },
  objArrObj: {
    nu: "null",
    simple: "string",
    arr: {
      type: "array",
      value: "number",
    },
    insObjArr: {
      type: "array",
      value: {
        name: "string",
        age: "number",
      },
    },
  },
  func: "function",
}

const obj = {
  hello: "world",
  number: 1,
  nu: null,
  und: undefined,
  bool: true,
  obj: {
    ex: "ample",
    nu: 4,
    func: () => "tion",
    arr: [1, 2, 3],
  },
  arr: [1, 2, 3],
  tab: ['1', '2', '3',],
  reg: /./gm,
  multidimensionalArr:
  [['1', '2'], ['3', '4']],
  objArr: [
    {
      nu: null,
      simple: "type",
      arr: [4,5,6],
      insObjArr: [{
        name: 'Daisy',
        age: 18,
      }]
    },
  ],
  objArrObj: {
    nu: null,
    simple: "type",
    arr: [4,5,6],
    insObjArr: [{
      name: 'Daisy',
      age: 18,
    }]
  },
  func: () => 'value',
}
console.log(checkSchema(schema, check).score) // log true

Not required properties

In schema object you can add property notRequireProperties. It should Be an array of strings. Properties whose names are contained in the array are not required.

Example:

  const schema = {
    notRequireProperties: ['name'],
    name: 'string',
    age: 'number',
    choclate: 'string',
  };
  const obj = {
    age: 80,
    choclate: 'only milk'
  }

  console.log(checkSchema(schema, obj).score); //log true

You can use this feature in nested objects.

Additional remarks

  • Remember you can use:
import { Schema as MyName } from "obj-valid";
// or
const MyName = require("obj-valid").Schema;
  • The same functions are always performed underneath regardless of usage option

  • checkSchema return an object:

{
  score: true, // this property say is object fits schema
  wrongProperties: [], // this is an array contain strings with not matching properties
}
  • There is possible to type any in arrays. If in based object is an empty array, all values will be correct. The other way around. If in the object to be checked is an empty array all values will be correct.

  • Package returns an object that contain following properties:

{
  configPrototype,
  Schema,
  checkSchema,
  createSchema,
}