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 🙏

© 2025 – Pkg Stats / Ryan Hefner

@dmitrytavern/object-parser

v2.0.3

Published

Parsing an object without pain

Downloads

33

Readme

JavaScript Object Praser

@dmitrytavern/object-parser - is a library for object validation that takes the original object and its schema, and then checks all object properties for existence and typing, can call a custom validator and assign a default value.

The library supports typescript and has smart autocomplete for checked objects.

It has no external dependencies and weighs ~7KiB compressed without gzip and ~3KiB with.

A quick example

npm install --save @dmitrytavern/object-parser
import parser from '@dmitrytavern/object-parser'

const schema = parser.schema({
  name: String,
  age: [String, Number],
  role: parser.property({
    type: String,
    required: false,
    default: 'anonymous',
    validator: (value) => {
      if (!['anonymous', 'user', 'admin'].includes(value))
        throw new Error('Role must be "anonymous", "user" or "admin".')

      return true
    },
  }),
})

const target = { name: 'Dmitry', age: 19 }

const result = parser.parse(target, schema)
// => {
//   value: { name: 'Dmitry', age: 19, role: 'anonymous' }
//   errors: []
// }

What's going on here:

  1. Imported parser.
  2. Created the object schema where:
    • name - is required property which can be only String.
    • age - is required property which can be String or Number.
    • role - is not required property, which can be only an "anonymous", "user" or "admin" string. Value by default: "anonymous".
  3. Parsed the target object by the object schema.

The result of parsing is an object, where:

  • value - parsed object. By default is a reference on the original object.
  • errors - array of errors. Сontains parsing errors.

Installation

ObjectParser is available via npm:

npm install --save @dmitrytavern/object-parser

Browser/CDN:

<script src="https://unpkg.com/@dmitrytavern/object-parser/dist/object-parser.min.js"></script>

Usage

CommonJS:

const parser = require('@dmitrytavern/object-parser')
const { parser } = require('@dmitrytavern/object-parser')
const { parse, schema, property } = require('@dmitrytavern/object-parser')

ES6:

import parser from '@dmitrytavern/object-parser'
import { parser } from '@dmitrytavern/object-parser'
import { parse, schema, property } from '@dmitrytavern/object-parser'

Browser:

<script>
  const parser = window.objectParser
  const { parser } = window.objectParser
  const { parse, schema, property } = window.objectParser
</script>

You can use the style you prefer. For more information, see docs.

Documentation

Powerful documentation can be found at dmitrytavern.github.io/object-parser.

License

MIT - check repo files

Copyright (c) 2022-present, Dmitry Tavern