stringtoobjectvalidator
TypeScript icon, indicating that this package has built-in type declarations

1.0.7 • Public • Published

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

Package Sidebar

Install

npm i stringtoobjectvalidator

Weekly Downloads

1

Version

1.0.7

License

SEE LICENSE IN <LICENSE.txt>

Unpacked Size

24.1 kB

Total Files

13

Last publish

Collaborators

  • petorit