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.
You can easily install Object Validator using npm:
npm i stringtoobjectvalidator
To use Object Validator in your JavaScript code, you need to require it:
import { validateObjectAgainstString } from 'stringtoobjectvalidator';
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
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:
-
Use of Spaces: Use spaces to separate elements within the expression for readability. For example,
age > 2
is preferred overage>2
. This makes the expression more human-readable. -
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. -
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. -
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. -
Negation: You can use the
!
operator to negate a condition. For example,!(age < 2)
will check ifage
is not less than 2. -
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. -
Consistency: Be consistent with your coding style. If you start with one convention for spaces, operators, and parentheses, stick to it throughout your expressions.
-
Testing Different Cases: Construct expressions that test a variety of cases, including valid and invalid scenarios, to ensure the function is thoroughly tested.
-
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.
We welcome contributions to Object Validator! If you encounter any issues, have feature requests, or want to improve the code, please open an issue