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

@menadevs/objectron

v0.1.14

Published

Compares a set of match rules contained with an object to determine if the latter conforms to the matching rules

Downloads

386

Readme

Objectron

Compare an object to a generic model to test equality and extract matches

This module provides you with the means to define a tester object containing a set of match rules that will be used against a payload object. The match() method within the module will return whether the payload object has satisfied all the rules and will return the set of matches.

Demo

The best way to really understand this module is to play with some examples. Go through some of our usage examples and test them in our interactive demo page:

Installation

Windows, macOS, Linux -- requires node v10.13.x or above

$ npm install --save @menadevs/objectron

Run tests (optional)

$ npm test

Use cases

  1. You can use this module as a basic JSON schema validator
  2. Can also be used in unit testing for broader assertions
  3. Can be used to extract values from a complex API response. We are actively using it in the maintenance of our community slack bot (https://github.com/mena-devs/bosta)
  4. Can be used at the core of a JSON linter

Check our FAQs for more insights

Usage examples

Keep in mind that the tester object can be a subset of the payload. You don't have to write explicit rules for all the properties of the payload object.

1. Simple match (Success)

The tester object will match with the payload provided

const match = require('@menadevs/objectron');

const payload = {
  'type': 'message',
  'text': 'text',
}

const tester = {
  'type': 'message',
  'text': 'text',
}

const result = match(payload, tester);

console.log(result)

# Output
> {
    match: true,
    total: 2,
    matches: { type: 'message', text: 'text' },
    groups: {}
  }

2. Simple match (Fail)

The tester object will not match with the payload provided

const match = require('@menadevs/objectron');

const payload = {
  'type': 'message',
  'text': 'text',
}

const tester = {
  'another_key': 'different value',
}

const result = match(payload, tester);

console.log(result)

# Output
> { 
    match: false, 
    total: 0, 
    matches: {}, 
    groups: {} 
  }

3. Simple match with RegEx (Success)

You can use regular expressions to build generic tester objects

const match = require('@menadevs/objectron');

const payload = {
  'type': 'message',
  'text': 'invite Smith',
}

const tester = {
  'type': 'message',
  'text': /invite (\S+)/,
}

const result = match(payload, tester);

console.log(result)

# Output
> {
    match: true,
    total: 2,
    matches: { type: 'message', text: 'invite Smith' },
    groups: {}
}

4. Match with RegEx and named groups (Success)

You can use regular expression named groups to capture matches separately

const match = require('@menadevs/objectron');

const payload = {
  'type': 'message',
  'text': 'invite (Smith) ([email protected]) (CompanyX) (Engineer)',
}

const tester = {
  'type': 'message',
  'text': /invite \((?<name>\S+)\) \((?<email>\S+)\) \((?<company>\S+)\) \((?<role>\S+)\)/,
}

const result = match(payload, tester);

console.log(result)

# Output
> {
    match: true,
    total: 2,
    matches: {
      type: 'message',
      text: 'invite (Smith) ([email protected]) (CompanyX) (Engineer)'
    },
    groups: {
      name: 'Smith',
      email: '[email protected]',
      company: 'CompanyX',
      role: 'Engineer'
    }
}

5. Match with nested Objects, Arrays and RegExp (Success)

You can create complex tester objects with an indefinite nesting depth

const match = require('@menadevs/objectron');

const payload = {
    'type': 'message',
    'level1': [
        {
            'level2': [
                {
                    'text': 'invite (Smith) ([email protected]) (CompanyX) (Engineer)'
                }
            ]
        },
        {
            'text': 'secondary object'
        }
    ]
}

const tester = {
  'level1': [
      {
          'level2': [
              {
                  'text': /invite \((?<name>\S+)\) \((?<email>\S+)\) \((?<company>\S+)\) \((?<role>\S+)\)/,
              }
          ]
      }
  ]
}

const result = match(payload, tester);

console.dir(result, {depth: null});

# Output
> {
    match: true,
    total: 1,
    matches: {
      level1: [
        {
          level2: [
            {
              text: 'invite (Smith) ([email protected]) (CompanyX) (Engineer)'
            }
          ]
        }
      ]
    },
    groups: {
      name: 'Smith',
      email: '[email protected]',
      company: 'CompanyX',
      role: 'Engineer'
    }
  }

6. Match with closures (Success)

You can use closures for testing

const match = require('@menadevs/objectron');

const payload = {
  'type': 'message',
  'text': 'text',
  'int': 1,
  'bool': true,
  'float': 1.1,
  'items': [1, 1, 1, 1],
}

const tester = {
  'type': (val) => val === 'message',
  'text': (val) => val.length == 4,
  'int': (val) => val + 1 == 2,
  'bool': (val) => !!!!!!!!val,
  'float': (val) => val - 1.1 == 0,
  'items': (val) => val.length == 4,
}

const result = match(payload, tester);

console.dir(result, {depth: null});

# Output
> {
    match: true,
    total: 0,
    matches: { type: {}, text: {}, int: {}, bool: {}, float: {}, items: {} },
    groups: {}
}

7. Using closures as wildcard (Success)

You can utilize closures to return an entire sub-object or sub-array from specific keys in a payload. This can be useful in cases where you want to return all values under a key without the need for further matching.

const match = require('@menadevs/objectron');

const payload = {
  request: {
    status: 200,
    headers: [
      {
        "name": "User-Agent",
        "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)"
      },
      {
        "name": "Referer",
        "value": "https://www.somethingabcxyz.kfc/"
      }
    ]
  }
}

const tester = {
  request: {
    status: 200,
    headers: (val) => val
  }
};

const result = match(payload, tester);

# Output
> {
    match: true,
    total: 2,
    matches: {
      request: {
        status: 200,
        headers: [
          {
            "name": "User-Agent",
            "value": "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_3)"
          },
          {
            "name": "Referer",
            "value": "https://www.somethingabcxyz.kfc/"
          }
        ]
      }
    },
    groups: {}
}

FAQs

1. What's the difference between Objectron and any other Schema Validator?

Objectron has a simple interface, it's a very small module (~60 LOCs), and most importantly it allows the extraction of the data that matches the rules not merely validating it.

2. Has this been used in production?

No, but we're planning to use it.

3. Why did you build this?

Why not? :)

4. I have a great idea for a new feature!

Fantastic, we'd love to hear more about it. Please create an issue and we will look into it as soon as we're able to.

5. I have a question not in this list

Please create an issue and we will look into it as soon as we're able to.

Meta

Contributing

This project does not require a Contributor License Agreement.

Release Process

Release checklist and process is documented in Release.md