Security News
Fluent Assertions Faces Backlash After Abandoning Open Source Licensing
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
validate-promise
Advanced tools
Promised based validation library
npm install validate-promise
Validates an object against a specified validation contract
import validate, {int} from 'validation-promise';
var contract = [
{
key: 'age', // index to validate in data
promises: [{
rule: int,
arg: (value, row) => 5
}], // array of validations
msg: (value, row, arg) => value + ' not an int',
}
];
var data = {
age: '11'
};
validate(contract, data)
.then(() => {
// The validations have passed...
})
.catch(error => {
// Validations failed - error is an object keyed on data keys, and containing an array of error messages.
});
contract = [
{
key: 'age',
promises: [
{
rule: after,
arg: () => '3 Jan 2016'
}
],
msg: (value, row, arg) => value + ' not after 3 Jan 2016'
}];
Determines if a date value is earlier than the supplied argument. The rule's arg should return a string compatible with Date.parse()
contract = [
{
key: 'age',
promises: [{
rule: before,
arg: () => '3 Jan 2016'
}
],
msg: (value, row, arg) => value + ' not before 3 Jan 2016'
}];
Determines if a date value is later than the supplied argument. The rule's arg should return a string compatible with Date.parse()
contract = [
{
key: 'age',
promises: [
{
rule: blacklist,
arg: () => ['17', 'abc', 'foo']
}
],
msg: (value, row, arg) => value + ' not allowed'
}];
contract = [
{
key: 'age',
promises: [
{
rule: equals,
arg: () => '17'
}
],
msg: (value, row, arg) => value + ' is not 17'
}];
Validate that the supplied value is is loosely equal to the argument.
import {float} from 'validate-promise';
contract = [
{
key: 'age',
promises: [
{
rule: float,
arg: () => ({min: 18, max: 55})
}
],
msg: (value, row, arg) => value + ' not a float'
}]
Tests if value can be coerced to a float. Optionally you can supply a min/max object from the arg function. If supplied the float must fall within this range to be valid.
contract = [
{
key: 'sales',
promises: [
{
rule: greaterthan,
arg: () => 0
}
],
msg: (value, row, arg) => 'Sales must be greater than 0'
}]
Tests a value is greater than the supplied argument. Alternatively you can compare two arbitrary values with the following contract:
contract = [
{
key: 'sales',
promises: [
{
rule: greaterthan,
arg: (value, row) => ({compare: 0, value: 10})
}
],
msg: (value, row, arg) => 'Sales must be greater than 0'
}]
This tests if value (10) is greater than the compare (0) value.
contract2 = [
{
key: 'age',
promises: [
{
rule: int,
arg: () => ({min: 18, max: 55})
}
],
msg: (value, row, arg) => value + ' not an int'
}]
Tests if value can be coerced to an integer. Optionally you can supply a min/max object from the arg function. If supplied the integer must fall within this range to be valid.
contract = [
{
key: 'age',
promises: [
{
rule: lessthan,
arg: () => 18
}
],
msg: (value, row, arg) => 'age less than 18'
}];
Test a value is less than the supplied argument. Alternatively you can compare two arbitrary values with the following contract:
contract = [
{
key: 'age',
promises: [
{
rule: lessthan,
arg: (value, row) => ({compare: 0, value: 10})
}
],
msg: (value, row, arg) => 'Age must be greater than 0'
}]
This tests if value (10) is less than than the compare (0) value.
contract = [
{
key: 'name',
promises: [
{
rule: required,
}
],
msg: () => 'Name is required'
}
];
contract = [
{
key: 'age',
promises: [
{
rule: whitelist,
arg: () => ['17', 'abc', 'foo']
}
],
msg: (value, row, arg) => value + ' not allowed'
}];
Validate that the supplied value is contained within the argument whitelist.
FAQs
Promised based validation library
The npm package validate-promise receives a total of 354 weekly downloads. As such, validate-promise popularity was classified as not popular.
We found that validate-promise demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 5 open source maintainers collaborating on the project.
Did you know?
Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.
Security News
Fluent Assertions is facing backlash after dropping the Apache license for a commercial model, leaving users blindsided and questioning contributor rights.
Research
Security News
Socket researchers uncover the risks of a malicious Python package targeting Discord developers.
Security News
The UK is proposing a bold ban on ransomware payments by public entities to disrupt cybercrime, protect critical services, and lead global cybersecurity efforts.