Skip to content

Commit

Permalink
docs improved
Browse files Browse the repository at this point in the history
  • Loading branch information
bitnbytesio committed Jul 10, 2019
1 parent 1663adf commit d5c36e5
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 1 deletion.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- nothing

## [3.4.2]

### Added

- Example of custom rule using other attributes

## [3.4.1]

### Fixed
Expand Down
35 changes: 35 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,41 @@ Validator.extend('status', async function (field, value, args) {

```

Example of using other fileds in rule

```javascript

Validator.extend('sumOfFields', async function (field, value, args) {

if (args.length !== 2) {
throw "Invalid seed for rule sumOfFields";
}

// use this.inputs to get another attributes

const anotherValue = Number(this.inputs[args[0]]);

const eq = Number(args[1]);

if ((Number(value) + anotherValue) !== eq) {
return false;
}

return true;

});

let v = new Validator(
{ num1: '50', num2: '50' },
{ num1: 'sumOfFields:num2,100|required' }
);

let matched = await v.check();

assert.equal(matched, true);

```

Some example of using database in rules

```javascript
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-input-validator",
"version": "3.4.1",
"version": "3.4.2",
"description": "validation library for nodejs, inspired by laravel.",
"main": "index.js",
"scripts": {
Expand Down
53 changes: 53 additions & 0 deletions test/customRules.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,12 +41,65 @@ Validator.extend('status', async function (field, value, args) {

});

Validator.extend('sumOfFields', async function (field, value, args) {

if (args.length !== 2) {
throw "Invalid seed for rule sumOfFields";
}

const anotherValue = Number(this.inputs[args[0]]);

const eq = Number(args[1]);

if ((Number(value) + anotherValue) !== eq) {
return false;
}

return true;

});

let r = {};

describe('Custom Rules', function () {

describe('Using custom rule even', function () {


it('sumOfFields:should return true', async () => {

let v = new Validator(
{ num1: '50', num2: '50' }, { num1: 'sumOfFields:num2,100|required' });

let matched = await v.check();

assert.equal(matched, true);

});

it('sumOfFields:should return false value is greater', async () => {

let v = new Validator(
{ num1: '50', num2: '51' }, { num1: 'sumOfFields:num2,100|required' });

let matched = await v.check();

assert.equal(matched, false);

});

it('sumOfFields:hould return false value is less', async () => {

let v = new Validator(
{ num1: '50', num2: '49' }, { num1: 'sumOfFields:num2,100|required' });

let matched = await v.check();

assert.equal(matched, false);

});


it('should return true', async () => {

let v = new Validator(
Expand Down

0 comments on commit d5c36e5

Please sign in to comment.