imbrn / v8n
- понедельник, 16 июля 2018 г. в 13:10:02
JavaScript
☑️ JavaScript fluent validation library.
The ultimate JavaScript validation library you've ever needed.
Dead simple fluent API. Customizable. Reusable.
Usage - Installation - Documentation
v8n()
.number()
.between(0, 100)
.even()
.not.equal(32)
.test(74); // true# Using npm
npm install v8n
# or yarn
yarn add v8nscript tag:<!-- From unpkg -->
<script src="https://unpkg.com/v8n/dist/v8n.min.js"></script>
<!-- or from jsdelivr -->
<script src="https://cdn.jsdelivr.net/npm/v8n/dist/v8n.min.js"></script>We use the function test to perform boolean based validations:
import v8n from "v8n";
const isValid = v8n()
.not.null()
.string()
.first("H")
.last("o")
.test("Hello");
isValid; // trueWe can also use the check function to perform exception based validations. This is going to throw an exception when the validation fails:
import v8n from "v8n";
try {
v8n()
.number()
.between(10, 20)
.check(25);
} catch (ex) {
console.log(ex.rule.name); // "between"
console.log(ex.rule.args); // [10, 20]
console.log(ex.value); // 25
console.log(ex.cause); // Rule failed!
}The exception thrown by the
checkfunction contains useful information about the rule which caused the validation fail, and it also has information about the validation process. Look and the ValidationException docs section to learn more about it.
There are a lot of useful standard rules for you to use already implemented in the core. Look at the API section of this document.
You can also implement your own rules, and share them between your projects, or even with the community.
To create custom validation rules, you just need to call the v8n.extend
function passing an object with your custom rules:
import v8n from "v8n";
v8n.extend({
myCustomRule: function(expected) {
return value => value === expected;
}
});And now you can use your custom rule in a validation as you do with standard rules:
v8n()
.string()
.myCustomRule("Hello")
.test("Olá"); // falseTo learn more about custom rules and how to implement them, look at the v8n#extend documentation section.
not modifierThe not modifier can be used to invert a validation rule meaning. Suppose we
have a validation like that:
v8n()
.includes("World")
.test("Hello World!"); // trueHere, we're declaring a validation to check if the validated value includes a
"World" string. And the test returns true.
But we could want a validation with the inverse meaning. With the not
modifier, we can do that:
v8n()
.not.includes("World")
.test("Hello World!"); // falseNow, we have a validation to check if the value does not include a "World"
string. And the test returns false.
The
notmodifier inverts the meaning only of the nextrule, the rule declared right after it. So for each rule you want to invert its meaning, you should use thenotmodifier before it.To learn more about the
notmodifier, look at its documentation.
Although we have a lot of validation libraries, almost all of them are about input fields validation. That's great sometimes, but we often need something independent of the way we're going to use it.
We usually need some kind of in-code validation, so that we can use that same validation in an input field, in a function call, in the server logic, whatever. Actually almost everytime, we need the same validation, that same logic, even between different projects.
That's all about the v8n validation library. This is not another input field
validation library.
This is a powerful engine for validation creation, reuse, and in-code validation execution.
With the v8n we can write our validation strategies and reuse them whenever we
need. Actually, we can reuse validation from other people, and in a really
simple way.
The v8n library has a fluent chainable API. This help us to easily create
validation objects.
v8n()
.not.null()
.between(100, 200)
.even()
.not.between(40, 60);To reuse a validation strategy, you just need to declare it, export it in someway, and import it from your code:
myValidation.js
import v8n from "v8n";
// Export the validation object
export default v8n()
.array()
.not.empty()
.minLength(3)
.maxLength(10)
.includes("Hello");myApp.js
// Import the validation object somewhere
import myValidation from "./myValidation";
myValidation.test(["Hello", "World", "!"]); // true
myValidation.check(["Hello", "Hi", "How is it going?"]); // No exception thrown!You can write custom validation rules and reuse them in other projects. You can also use rules from other people.
To export validation rules, create a .js file contain a call to the v8n static
extend function with your custom rules declared in a object, and then
export this file someway. So you can import this file from another source code,
and your custom validation rules will be available like the standard ones:
myCustomRules.js
import v8n from "v8n";
v8n.extend({
// "one" is a custom rule
one() {
return value => value == 1;
},
// "two" is another custom rule
two() {
return value => value == 2;
}
});And in another file or even another project, import the file with the custom rules and use them like you do with standard rules:
import v8n from "v8n";
import "myExternalProject/myCustomRules.js";
v8n()
.number()
.one()
.test(1); // true
v8n()
.string()
.two()
.test("2"); // trueYou can mix custom and standard rules as you want.
Function used to produce a Validation object. The Validation object is used to configure a validation strategy and perform the validation tests.
Returns Validation
Extends the available rules with developer specified custom rules.
Custom rules:
Custom rules are rules functions defined by the developer.
A rule function works exactly the same way as a standard rule, and it can be called as a member function in a validation object instance.
The validation engine will inject custom rules into validation object instances when needed.
Custom rule structure:
A custom rule is a function that returns another function. The custom rule
function can take parameters for its own configuration, and should return a
function which takes only a value as parameter. This value must be
validated by this function and return true for valid value and false for
invalid value.
The new added rules can be used like any standard rule when building validations.
To understand how validations works, see Validation and rules sections.
newRules object an object containing named custom rule functionsfunction myCustomRule(expected) {
return value => value === expected;
}
// Adding a custom rule
v8n.extend({
myCustomRule
});
// Using the custom rule in validation
v8n()
.string()
.myCustomRule("Awesome") // Used like any other rule
.test("Awesome"); // trueRepresents an instance of a validation object. This is created by the entry point function v8n.
rules:
A validation strategy is defined by calling rules functions on the
validation object. Each call to a rule function will add that rule to the
validation strategy chain and return the validation object instance for
chaining rules functions calls together.
All the rules functions that are available for use by a Validation
object instance are actually declared in the rules object. Those
rules functions are injected into each Validation object instance.
Look at the rules object to see all the available rules.
The not modifier
To invert a rule meaning, the modifier not must be
invoked before the rule function call. It will invert the next rule
call meaning.
Validating
There are two ways to perform a validation: the functions test and check.
When the test function is used, a validation based on a boolean return value is performed.
When the check function is used, a validation based on exception throw is performed.
Look at these functions documentation to know more about them.
v8n() // Creates a validation object instance
.not.null() // Inverting the `null` rule call to `not null`
.minLength(3) // Chaining `rules` to the validation strategy
.test("some value"); // Executes the validation test functionConstructor function which produces a rule object.
This constructor should not be used directly. It's used by the validation engine when needed.
Rule object:
A rule object is composed by a name; a validation function, which will be performed against the validated value in the validation process; an arguments list, which is used by the validation function; and an invert property, which defines if the rule has to be inverted in its meaning.
To know more about the
invertproperty, look a the not modifier documentation section.Look at Validation to know more about the validation process.
name string rule function namefn function validation function executed by the ruleargs Array arguments list for the validation functioninvert boolean indicates if the rule has its meaning invertedGroup of functionalities that can be performed on a validation object.
This object should not be used directly. All of its functionalities will be injected into the Validation object instance when a validation is performed.
To know more about the validation process, look at Validation docs.
Performs boolean based validation.
When this function is executed it performs the validation process and
returns a boolean result.
value any the value to be validatedReturns boolean true for valid and false for invalid
Performs exception based validation.
When this function is executed it performs the validation process and throws a ValidationException when the value is not valid.
The exception thrown by this validation function contains a reference to the performed Rule.
value any the value to be validated
Throws ValidationException exception thrown when the validation fails
Constructor function used to produce an object which contains information about a validation exception.
Validation exception object:
A validation exception object is thrown by the check function when the validation fails.
It contains information about the Rule which was been performed during the fail, the value been validated and the cause of the thrown exception.
rule Rule performing when the exception was thrownvalue any been validated when the exception was throwncause any cause of the thrown exceptionGroup of modifiers to be used along with rules to compose a validation
strategy.
This object should not be used directly. All of its functionalities will be injected into the validation object during the validation process.
Look at Validation and rules to know more about the validation process.
Modifier for inverting of a rule meaning.
It's used before a rule function call and will invert that rule
meaning, making it to expect the opposite result.
// This call will make the `equal` rule to be inverted, so that it now
// expect the validated value to be everything but "three".
v8n()
.not.equal("three");Group of standard rules that can be used to build a validation strategy.
This object should not be used directly. Instead, its functions will be injected as
rulefunctions instance members of each Validation object instance.
See more about how to use validation rules at Validation.
Also, each rule can have its meaning inverted by using the
not modifier before it.
Rule function for regular expression based validation.
A regular expression validation is used to check if the validated value matches an specified pattern.
pattern RegExp the regular expression patternv8n()
.pattern(/[a-z]+/)
.test("hello"); // true
v8n()
.pattern(/[0-9]/)
.test("hello"); // falseRule function for equality validation.
It's used to check if the validated value is coercive the same as the specified expected value.
It works with any data type
It uses the double equal (==) operator for comparison. For comparison without coercion of types, use the exact rule.
expected any the expected valuev8n()
.equal(10)
.test("10"); // true
v8n()
.equal("Hello")
.test("Another"); // falseRule function for equality validation.
It's used to check if the validated value is exact the same as the specified expected value.
It works with any data type
It uses the triple equal (===) operator for comparison. For comparison with coercion of types, use the equal rule.
expected any the expected valuev8n()
.exact(10)
.test("10"); // false
v8n()
.exact("Hello")
.test("Hello"); // trueRule function for "string" type validation.
This is used to check if the validated value is of type string.
v8n()
.string()
.test("Hello"); // true
v8n()
.string()
.test(123); // falseRule function for "number" type validation.
This is used to check if the validated value is of type "number".
v8n()
.number()
.test(123); // true
v8n()
.number()
.test("Hello"); // falseRule function for "boolean" type validation.
This is used to check if the validated value is of type "boolean".
v8n()
.boolean()
.test(22); // false
v8n()
.boolean()
.test(false); // trueRule function for undefined value validation.
This is used to check if the validated value is undefined.
v8n()
.undefined()
.test("something"); // false
v8n()
.undefined()
.test(undefined); // true
v8n()
.undefined()
.test(); // trueRule function for null value validation.
This is used to check if the validated value is null.
v8n()
.null()
.test(123); // false
v8n()
.null()
.test(null); // trueRule function for array value validation.
This is used to check if the validated value is an array.
v8n()
.array()
.test("hello"); // false
v8n()
.array()
.test([1, 2, 3]); // trueRule function for lowercase string validation.
It's used to check if the validated value is a complete lowercase string. An empty string does not match.
v8n()
.lowercase()
.test("hello"); // true
v8n()
.lowercase()
.test("Hello"); // falseRule function for uppercase string validation.
It's used to check if the validated value is a complete uppercase string. An empty string does not match.
v8n()
.uppercase()
.test("HELLO"); // true
v8n()
.uppercase()
.test("Hello"); // falseRule function for vowel-only string validation.
It's used to check if the validated value is a vowel-only string. An empty string does not match.
Note: Only vowels of the "words" characters set defined by the JavaScript language are valid: http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6
v8n()
.vowel()
.test("UE"); // true
v8n()
.vowel()
.test("Me"); // falseRule function for consonant-only string validation.
It's used to check if the validated value is a consonant-only string. An empty string does not match.
Note: Only consonants of the "words" characters set defined by the JavaScript language are valid: http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6
v8n()
.consonant()
.test("vn"); // true
v8n()
.consonant()
.test("me"); // falseRule function for first item validation.
It's used to check if the first item of the validated value matches the specified item.
It can be used with strings and arrays.
item any the expected first item// With strings
v8n()
.first("H")
.test("Hello"); // true
v8n()
.first("A")
.test("Hello"); // false
// With arrays
v8n()
.first("One")
.test(["One", "Two", "Three"]); // true
v8n()
.first(10)
.test([0, 10, 20]); // falseRule function for last item validation.
It's used to check if the last item of the validated value matches the specified item.
It can be used with string and arrays.
item any the expected last itemv8n()
.last("o")
.test("Hello"); // true
v8n()
.last(3)
.test([1, 2, 3, 4]); // falseRule function for emptiness validation.
It's used to check if the validated value is empty.
It works with strings, arrays and any kind of object that contains a
lengthproperty.
v8n()
.empty()
.test(""); // true
v8n()
.empty()
.test([1, 2]); // falseRule function for length validation.
It's used to check if the validated value length is between the specified length (inclusive).
When only the first parameter is passed, the length must be exact as this parameter.
It works with strings, arrays and any kind of object that contains a
lengthproperty.
v8n()
.length(3, 5)
.test([1, 2, 3, 4]); // true
v8n()
.length(3)
.test([1, 2, 3, 4]); // falseRule function for minimum length validation.
It's used to check if the validated value length is at least as the specified minimum length.
It works with strings, arrays and any kind of object that have a
lengthproperty.
min number the minimum expected lengthv8n()
.minLength(3)
.test([1, 2, 3, 4]); // true
v8n()
.minLength(3)
.test([1, 2]); // falseRule function for maximum length validation.
It's used to check if the validated value length is at most as the specified maximum length.
It works with strings, arrays and any kind of object that have a
lengthproperty.
max number the maximum expected lengthv8n()
.maxLength(3)
.test([1, 2]); // true
v8n()
.maxLength(3)
.test([1, 2, 3, 4]); // falseRule function for negative number validation.
It's used to check if the validated value is a negative number.
v8n()
.negative()
.test(-1); // true
v8n()
.negative()
.test(0); // falseRule function for positive number validation.
It's used to check if the validated value is a positive number, including zero.
v8n()
.positive()
.test(1); // true
v8n()
.position()
.test(-1); // falseRule function for range validation.
It's used to check if the validated value is between (inclusive) the specified range.
It works only with numbers.
It's a synonym of the range rule.
v8n()
.between(1, 3)
.test(2); // true
v8n()
.between(1, 3)
.test(4); // falseRule function for range validation.
It's used to check if the validated value is between (inclusive) the specified range.
It works only with numbers.
It's a synonym of the between rule.
v8n()
.range(1, 3)
.test(2); // true
v8n()
.range(1, 3)
.test(4); // falseRule function for upper bound validation.
It's used to check if the validated value is less than the specified upper bound value.
It works only with numbers.
bound number the upper bound (not inclusive)v8n()
.lessThan(10)
.test(9); // true
v8n()
.lessThan(10)
.test(10); // falseRule function for upper bound validation.
It's used to check if the validated value is less than or equal to the specified upper bound value.
It works only with numbers.
bound number the upper bound (inclusive)v8n()
.lessThanOrEqual(10)
.test(10); // true
v8n()
.lessThanOrEqual(10)
.test(11); // falseRule function for lower bound validation.
It's used to check if the validated value is greater than the specified lower bound value.
It works only with numbers.
bound number the lower bound (not inclusive)v8n()
.greaterThan(10)
.test(11); // true
v8n()
.greaterThan(10)
.test(10); // falseRule function for lower bound validation.
It's used to check if the validated value is greater than or equal to the specified lower bound value.
It works only with numbers.
bound number the lower bound (inclusive)v8n()
.greaterThanOrEqual(10)
.test(10); // true
v8n()
.greaterThanOrEqual(10)
.test(9); // falseRule function for even number validation.
It's used to check if the validated value is even (divisible by 2).
v8n()
.even()
.test(40); // true
v8n()
.even()
.test(21); // falseRule function for odd number validation.
It's used to check if the validated value is odd (not divisible by 2).
v8n()
.odd()
.test(20); // false
v8n()
.odd()
.test(9); // trueRule function for inclusion validation.
It's used to check if the validated value contains the specified item.
It works for strings and arrays.
expected any the expected item to be foundv8n()
.includes(2)
.test([1, 2, 3]); // true
v8n()
.includes("a")
.test("Hello"); // falseFunction used to produce a Validation object. The Validation object is used to configure a validation strategy and perform the validation tests.
Returns Validation
Extends the available rules with developer specified custom rules.
Custom rules:
Custom rules are rules functions defined by the developer.
A rule function works exactly the same way as a standard rule, and it can be called as a member function in a validation object instance.
The validation engine will inject custom rules into validation object instances when needed.
Custom rule structure:
A custom rule is a function that returns another function. The custom rule
function can take parameters for its own configuration, and should return a
function which takes only a value as parameter. This value must be
validated by this function and return true for valid value and false for
invalid value.
The new added rules can be used like any standard rule when building validations.
To understand how validations works, see Validation and rules sections.
newRules object an object containing named custom rule functionsfunction myCustomRule(expected) {
return value => value === expected;
}
// Adding a custom rule
v8n.extend({
myCustomRule
});
// Using the custom rule in validation
v8n()
.string()
.myCustomRule("Awesome") // Used like any other rule
.test("Awesome"); // trueRepresents an instance of a validation object. This is created by the entry point function v8n.
rules:
A validation strategy is defined by calling rules functions on the
validation object. Each call to a rule function will add that rule to the
validation strategy chain and return the validation object instance for
chaining rules functions calls together.
All the rules functions that are available for use by a Validation
object instance are actually declared in the rules object. Those
rules functions are injected into each Validation object instance.
Look at the rules object to see all the available rules.
The not modifier
To invert a rule meaning, the modifier not must be
invoked before the rule function call. It will invert the next rule
call meaning.
Validating
There are two ways to perform a validation: the functions test and check.
When the test function is used, a validation based on a boolean return value is performed.
When the check function is used, a validation based on exception throw is performed.
Look at these functions documentation to know more about them.
v8n() // Creates a validation object instance
.not.null() // Inverting the `null` rule call to `not null`
.minLength(3) // Chaining `rules` to the validation strategy
.test("some value"); // Executes the validation test functionConstructor function which produces a rule object.
This constructor should not be used directly. It's used by the validation engine when needed.
Rule object:
A rule object is composed by a name; a validation function, which will be performed against the validated value in the validation process; an arguments list, which is used by the validation function; and an invert property, which defines if the rule has to be inverted in its meaning.
To know more about the
invertproperty, look a the not modifier documentation section.Look at Validation to know more about the validation process.
name string rule function namefn function validation function executed by the ruleargs Array arguments list for the validation functioninvert boolean indicates if the rule has its meaning invertedGroup of functionalities that can be performed on a validation object.
This object should not be used directly. All of its functionalities will be injected into the Validation object instance when a validation is performed.
To know more about the validation process, look at Validation docs.
Performs boolean based validation.
When this function is executed it performs the validation process and
returns a boolean result.
value any the value to be validatedReturns boolean true for valid and false for invalid
Performs exception based validation.
When this function is executed it performs the validation process and throws a ValidationException when the value is not valid.
The exception thrown by this validation function contains a reference to the performed Rule.
value any the value to be validated
Throws ValidationException exception thrown when the validation fails
Constructor function used to produce an object which contains information about a validation exception.
Validation exception object:
A validation exception object is thrown by the check function when the validation fails.
It contains information about the Rule which was been performed during the fail, the value been validated and the cause of the thrown exception.
rule Rule performing when the exception was thrownvalue any been validated when the exception was throwncause any cause of the thrown exceptionGroup of modifiers to be used along with rules to compose a validation
strategy.
This object should not be used directly. All of its functionalities will be injected into the validation object during the validation process.
Look at Validation and rules to know more about the validation process.
Modifier for inverting of a rule meaning.
It's used before a rule function call and will invert that rule
meaning, making it to expect the opposite result.
// This call will make the `equal` rule to be inverted, so that it now
// expect the validated value to be everything but "three".
v8n().not.equal("three");Group of standard rules that can be used to build a validation strategy.
This object should not be used directly. Instead, its functions will be injected as
rulefunctions instance members of each Validation object instance.
See more about how to use validation rules at Validation.
Also, each rule can have its meaning inverted by using the
not modifier before it.
Rule function for regular expression based validation.
A regular expression validation is used to check if the validated value matches an specified pattern.
pattern RegExp the regular expression patternv8n()
.pattern(/[a-z]+/)
.test("hello"); // true
v8n()
.pattern(/[0-9]/)
.test("hello"); // falseRule function for equality validation.
It's used to check if the validated value is coercive the same as the specified expected value.
It works with any data type
It uses the double equal (==) operator for comparison. For comparison without coercion of types, use the exact rule.
expected any the expected valuev8n()
.equal(10)
.test("10"); // true
v8n()
.equal("Hello")
.test("Another"); // falseRule function for equality validation.
It's used to check if the validated value is exact the same as the specified expected value.
It works with any data type
It uses the triple equal (===) operator for comparison. For comparison with coercion of types, use the equal rule.
expected any the expected valuev8n()
.exact(10)
.test("10"); // false
v8n()
.exact("Hello")
.test("Hello"); // trueRule function for "string" type validation.
This is used to check if the validated value is of type string.
v8n()
.string()
.test("Hello"); // true
v8n()
.string()
.test(123); // falseRule function for "number" type validation.
This is used to check if the validated value is of type "number".
v8n()
.number()
.test(123); // true
v8n()
.number()
.test("Hello"); // falseRule function for "boolean" type validation.
This is used to check if the validated value is of type "boolean".
v8n()
.boolean()
.test(22); // false
v8n()
.boolean()
.test(false); // trueRule function for undefined value validation.
This is used to check if the validated value is undefined.
v8n()
.undefined()
.test("something"); // false
v8n()
.undefined()
.test(undefined); // true
v8n()
.undefined()
.test(); // trueRule function for null value validation.
This is used to check if the validated value is null.
v8n()
.null()
.test(123); // false
v8n()
.null()
.test(null); // trueRule function for array value validation.
This is used to check if the validated value is an array.
v8n()
.array()
.test("hello"); // false
v8n()
.array()
.test([1, 2, 3]); // trueRule function for lowercase string validation.
It's used to check if the validated value is a complete lowercase string. An empty string does not match.
v8n()
.lowercase()
.test("hello"); // true
v8n()
.lowercase()
.test("Hello"); // falseRule function for uppercase string validation.
It's used to check if the validated value is a complete uppercase string. An empty string does not match.
v8n()
.uppercase()
.test("HELLO"); // true
v8n()
.uppercase()
.test("Hello"); // falseRule function for vowel-only string validation.
It's used to check if the validated value is a vowel-only string. An empty string does not match.
Note: Only vowels of the "words" characters set defined by the JavaScript language are valid: http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6
v8n()
.vowel()
.test("UE"); // true
v8n()
.vowel()
.test("Me"); // falseRule function for consonant-only string validation.
It's used to check if the validated value is a consonant-only string. An empty string does not match.
Note: Only consonants of the "words" characters set defined by the JavaScript language are valid: http://www.ecma-international.org/ecma-262/5.1/#sec-15.10.2.6
v8n()
.consonant()
.test("vn"); // true
v8n()
.consonant()
.test("me"); // falseRule function for first item validation.
It's used to check if the first item of the validated value matches the specified item.
It can be used with strings and arrays.
item any the expected first item// With strings
v8n()
.first("H")
.test("Hello"); // true
v8n()
.first("A")
.test("Hello"); // false
// With arrays
v8n()
.first("One")
.test(["One", "Two", "Three"]); // true
v8n()
.first(10)
.test([0, 10, 20]); // falseRule function for last item validation.
It's used to check if the last item of the validated value matches the specified item.
It can be used with string and arrays.
item any the expected last itemv8n()
.last("o")
.test("Hello"); // true
v8n()
.last(3)
.test([1, 2, 3, 4]); // falseRule function for emptiness validation.
It's used to check if the validated value is empty.
It works with strings, arrays and any kind of object that contains a
lengthproperty.
v8n()
.empty()
.test(""); // true
v8n()
.empty()
.test([1, 2]); // falseRule function for length validation.
It's used to check if the validated value length is between the specified length (inclusive).
When only the first parameter is passed, the length must be exact as this parameter.
It works with strings, arrays and any kind of object that contains a
lengthproperty.
v8n()
.length(3, 5)
.test([1, 2, 3, 4]); // true
v8n()
.length(3)
.test([1, 2, 3, 4]); // falseRule function for minimum length validation.
It's used to check if the validated value length is at least as the specified minimum length.
It works with strings, arrays and any kind of object that have a
lengthproperty.
min number the minimum expected lengthv8n()
.minLength(3)
.test([1, 2, 3, 4]); // true
v8n()
.minLength(3)
.test([1, 2]); // falseRule function for maximum length validation.
It's used to check if the validated value length is at most as the specified maximum length.
It works with strings, arrays and any kind of object that have a
lengthproperty.
max number the maximum expected lengthv8n()
.maxLength(3)
.test([1, 2]); // true
v8n()
.maxLength(3)
.test([1, 2, 3, 4]); // falseRule function for negative number validation.
It's used to check if the validated value is a negative number.
v8n()
.negative()
.test(-1); // true
v8n()
.negative()
.test(0); // falseRule function for positive number validation.
It's used to check if the validated value is a positive number, including zero.
v8n()
.positive()
.test(1); // true
v8n()
.position()
.test(-1); // falseRule function for range validation.
It's used to check if the validated value is between (inclusive) the specified range.
It works only with numbers.
It's a synonym of the range rule.
v8n()
.between(1, 3)
.test(2); // true
v8n()
.between(1, 3)
.test(4); // falseRule function for range validation.
It's used to check if the validated value is between (inclusive) the specified range.
It works only with numbers.
It's a synonym of the between rule.
v8n()
.range(1, 3)
.test(2); // true
v8n()
.range(1, 3)
.test(4); // falseRule function for upper bound validation.
It's used to check if the validated value is less than the specified upper bound value.
It works only with numbers.
bound number the upper bound (not inclusive)v8n()
.lessThan(10)
.test(9); // true
v8n()
.lessThan(10)
.test(10); // falseRule function for upper bound validation.
It's used to check if the validated value is less than or equal to the specified upper bound value.
It works only with numbers.
bound number the upper bound (inclusive)v8n()
.lessThanOrEqual(10)
.test(10); // true
v8n()
.lessThanOrEqual(10)
.test(11); // falseRule function for lower bound validation.
It's used to check if the validated value is greater than the specified lower bound value.
It works only with numbers.
bound number the lower bound (not inclusive)v8n()
.greaterThan(10)
.test(11); // true
v8n()
.greaterThan(10)
.test(10); // falseRule function for lower bound validation.
It's used to check if the validated value is greater than or equal to the specified lower bound value.
It works only with numbers.
bound number the lower bound (inclusive)v8n()
.greaterThanOrEqual(10)
.test(10); // true
v8n()
.greaterThanOrEqual(10)
.test(9); // falseRule function for even number validation.
It's used to check if the validated value is even (divisible by 2).
v8n()
.even()
.test(40); // true
v8n()
.even()
.test(21); // falseRule function for odd number validation.
It's used to check if the validated value is odd (not divisible by 2).
v8n()
.odd()
.test(20); // false
v8n()
.odd()
.test(9); // trueRule function for inclusion validation.
It's used to check if the validated value contains the specified item.
It works for strings and arrays.
expected any the expected item to be foundv8n()
.includes(2)
.test([1, 2, 3]); // true
v8n()
.includes("a")
.test("Hello"); // false