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

stringtoobjectvalidator

v1.0.7

Published

A library for validating objects using custom validation strings.

Downloads

20

Readme

Object Validator

Object Validator is a lightweight npm package that allows you to validate an object against a validation string. This library provides a simple and efficient way to check whether an object conforms to a specified set of rules.

Installation

You can easily install Object Validator using npm:

npm i stringtoobjectvalidator

Usage

To use Object Validator in your JavaScript code, you need to require it:

import { validateObjectAgainstString } from 'stringtoobjectvalidator';

Validate an Object

You can use the validate function to check whether an object adheres to a validation string. Here's how to do it:

const expression = '(name === "John" && age > 18)';
const object = { name: 'John', age: 20 };

const isValid = validateObjectAgainstString(expression, object);
console.log(isValid); // Output: true

Validation String Format

When constructing a test expression for validation, you can follow a set of rules to ensure the expression is well-formed and covers various cases. Here are some rules for constructing a test expression:

  1. Use of Spaces: Use spaces to separate elements within the expression for readability. For example, age > 2 is preferred over age>2. This makes the expression more human-readable.

  2. Comparison Operators: Use valid comparison operators to create conditions. Common comparison operators include ===, ==, !==, !=, >, <, >=, and <=. Ensure that you use them correctly according to the data types involved in the comparison.

  3. Logical Operators: Employ logical operators to combine conditions. Common logical operators include && (logical AND) and || (logical OR). These operators allow you to express complex conditions by connecting simpler ones.

  4. Bracket Enclosures: Use parentheses (brackets) to group conditions when necessary. Parentheses help define the order of evaluation and can be used to control the logical flow of the expression. For example, (age > 2 || name === katlego) ensures that the two conditions within the parentheses are evaluated together.

  5. Negation: You can use the ! operator to negate a condition. For example, !(age < 2) will check if age is not less than 2.

  6. Nested Expressions: Create complex expressions by nesting conditions within each other using logical operators and parentheses. For example, (age > 2 || (name === katlego && gender === male)) combines both logical OR and AND operations in a nested expression.

  7. Consistency: Be consistent with your coding style. If you start with one convention for spaces, operators, and parentheses, stick to it throughout your expressions.

  8. Testing Different Cases: Construct expressions that test a variety of cases, including valid and invalid scenarios, to ensure the function is thoroughly tested.

  9. Clear and Readable Expressions: Write expressions that are clear and easy to understand. Make sure your variable names, operators, and conditions are named appropriately to improve code maintainability.

Here's an example of a well-constructed test expression following these rules:

(age > 2 || (name === katlego && gender === male)) && !(isStudent === true || age >= 30)

Following these rules will help you create test expressions that are both functional and comprehensible.

Contributing

We welcome contributions to Object Validator! If you encounter any issues, have feature requests, or want to improve the code, please open an issue