knight-validation 2.0.3 → 2.0.4

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,6 @@
1
+ export declare class DotNotification {
2
+ properties: string[];
3
+ constructor(dotNotification: string);
4
+ exists(obj: any): boolean;
5
+ get(obj: any): any;
6
+ }
@@ -0,0 +1,35 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.DotNotification = void 0;
4
+ class DotNotification {
5
+ constructor(dotNotification) {
6
+ this.properties = dotNotification.split('.');
7
+ }
8
+ exists(obj) {
9
+ let exists = false;
10
+ for (let property of this.properties) {
11
+ if (typeof obj == 'object' && obj !== null && obj[property] !== undefined) {
12
+ exists = true;
13
+ obj = obj[property];
14
+ }
15
+ else {
16
+ exists = false;
17
+ break;
18
+ }
19
+ }
20
+ return exists;
21
+ }
22
+ get(obj) {
23
+ for (let property of this.properties) {
24
+ if (typeof obj == 'object' && obj !== null) {
25
+ obj = obj[property];
26
+ }
27
+ else {
28
+ obj = undefined;
29
+ break;
30
+ }
31
+ }
32
+ return obj;
33
+ }
34
+ }
35
+ exports.DotNotification = DotNotification;
@@ -11,6 +11,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Validator = void 0;
13
13
  const Constraint_1 = require("./Constraint");
14
+ const DotNotification_1 = require("./DotNotification");
14
15
  const QuickConstraint_1 = require("./QuickConstraint");
15
16
  class Validator {
16
17
  constructor(options) {
@@ -73,7 +74,8 @@ class Validator {
73
74
  }
74
75
  let atLeastOnePropertyExists = false;
75
76
  for (let property of entry.properties) {
76
- if (object[property] !== undefined) {
77
+ let dotNotification = new DotNotification_1.DotNotification(property);
78
+ if (dotNotification.exists(object)) {
77
79
  atLeastOnePropertyExists = true;
78
80
  break;
79
81
  }
@@ -85,7 +87,8 @@ class Validator {
85
87
  let misfit;
86
88
  if (entry.properties.length == 1) {
87
89
  let property = entry.properties[0];
88
- let value = object[property];
90
+ let dotNotification = new DotNotification_1.DotNotification(property);
91
+ let value = dotNotification.get(object);
89
92
  misfit = yield entry.constraint.validate(value);
90
93
  }
91
94
  else {
@@ -105,8 +108,9 @@ class Validator {
105
108
  throw new Error('Using another validator only works for one property');
106
109
  }
107
110
  let property = entry.properties[0];
108
- let value = object[property];
109
- if (typeof value != 'object' || value === null) {
111
+ let dotNotification = new DotNotification_1.DotNotification(property);
112
+ let value = dotNotification.get(object);
113
+ if (value === undefined) {
110
114
  continue;
111
115
  }
112
116
  if (value instanceof Array) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knight-validation",
3
- "version": "2.0.3",
3
+ "version": "2.0.4",
4
4
  "description": "A validation lib",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",