Skip to content

Commit 6448dc9

Browse files
committed
wip
1 parent 7cbc328 commit 6448dc9

File tree

6 files changed

+98
-27
lines changed

6 files changed

+98
-27
lines changed

CHANGELOG.md

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,17 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.0.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [4.6.0]
9+
10+
### Fixed
11+
- [issue/71](https://github.com/bitnbytesio/node-input-validator/issues/71), [PR](https://github.com/bitnbytesio/node-input-validator/pull/82)
12+
- performance improvement
13+
14+
## [4.5.1]
15+
16+
### Fixed
17+
- fix vulnerable deps
18+
819
## [4.3.3]
920

1021
### Fixed

lib/validator.js

Lines changed: 41 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,21 @@ class Validator {
6060

6161
// parse validations collection, this should be after all assignments
6262
this.parseRules(validationsRules);
63+
64+
// @beta feature
65+
const state = {};
66+
this.inputsAsPerRules = { state, data: state };
67+
this.collectDeclaredInputs = false;
68+
}
69+
70+
/**
71+
* @beta
72+
* added in v4.6.
73+
* get inputs as per declared rules
74+
* @returns
75+
*/
76+
data() {
77+
return this.inputsAsPerRules.data;
6378
}
6479

6580
/**
@@ -329,6 +344,10 @@ class Validator {
329344
return;
330345
}
331346

347+
// get value
348+
validation.value = this.parseValue(attr);
349+
const isEmpty = empty.reallyEmpty(validation.value);
350+
332351
// collection of async rules
333352
const rulesPromise = [];
334353
const rulesPromiseProps = [];
@@ -337,9 +356,6 @@ class Validator {
337356
for (let r = 0; r < rulesLen; r++) {
338357
const { rule, args } = attrRules[r];
339358

340-
// get value
341-
validation.value = this.parseValue(attr);
342-
343359
// return if nullable or value is null
344360
if (rule === 'nullable' || (validation.nullable === true && validation.value === null)) {
345361
continue;
@@ -350,7 +366,7 @@ class Validator {
350366
}
351367

352368
// if value is really empty, skip validation
353-
if (!validation.required && empty.reallyEmpty(validation.value)) {
369+
if (!validation.required && isEmpty) {
354370
continue;
355371
}
356372

@@ -379,7 +395,8 @@ class Validator {
379395
if (this.breakWhenFailed) {
380396
// add error
381397
this.addError(attr, rule, message);
382-
return;
398+
break;
399+
// return;
383400
}
384401
this.appendError(attr, rule, message);
385402
}
@@ -409,6 +426,25 @@ class Validator {
409426
return true;
410427
});
411428
}
429+
// @beta feature
430+
let state = this.inputsAsPerRules.data;
431+
// eslint-disable-next-line no-restricted-syntax
432+
for (const key of attr.split('.')) {
433+
if (typeof validation.value === 'object') {
434+
// @ts-ignore
435+
// eslint-disable-next-line no-multi-assign
436+
state = state[key] = Array.isArray(validation.value) ? [] : {};
437+
continue;
438+
}
439+
if (state[key]) {
440+
state = state[key];
441+
continue;
442+
}
443+
if (typeof state[key] === 'object') {
444+
state[key] = validation.value;
445+
}
446+
}
447+
// this.inputsAsPerRules.data[attr] = validation.value;
412448
}
413449

414450
async postApply(rule) {

package-lock.json

Lines changed: 17 additions & 18 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,8 +51,6 @@
5151
],
5252
"license": "ISC",
5353
"devDependencies": {
54-
"@types/mocha": "^5.2.7",
55-
"@types/moment": "^2.13.0",
5654
"@types/node": "^12.19.14",
5755
"@types/validator": "^10.11.3",
5856
"eslint": "^6.8.0",
@@ -67,8 +65,8 @@
6765
"image-size": "^0.8.3",
6866
"lodash.has": "^4.5.2",
6967
"mime-types": "^2.1.28",
70-
"moment": "^2.29.1",
68+
"moment": "^2.29.4",
7169
"read-chunk": "^3.2.0",
72-
"validator": "^13.5.2"
70+
"validator": "^13.7.0"
7371
}
7472
}

stubs/niv.mjs

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
const { Validator } = require('../lib/index.js');
2+
3+
const v = new Validator(
4+
{
5+
order: {
6+
price: 100,
7+
items: [
8+
{ id: 1, quantity: 2, _v: 2 },
9+
{ id: 2, quantity: 4, _v: 2 },
10+
],
11+
_v: 1,
12+
},
13+
extra: 1,
14+
_v: 1,
15+
},
16+
{
17+
order: 'object',
18+
'order.price': 'numeric',
19+
'order.items': 'array',
20+
'order.items.*.id': 'numeric',
21+
'order.items.*.quantity': 'numeric',
22+
},
23+
);
24+
25+
v.validate().then(passed => {
26+
console.log(passed, v.errors, v.data());
27+
});

test/inputs.js

Whitespace-only changes.

0 commit comments

Comments
 (0)