knight-validation 2.0.0 → 2.0.1

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.
package/README.md CHANGED
@@ -6,7 +6,7 @@
6
6
 
7
7
  ## Overview
8
8
 
9
- ### Constraints for single fields
9
+ ### Constraints for single properties
10
10
 
11
11
  ```typescript
12
12
  import { Required, TypeOf, Unique, Validator } from 'knight-validation'
@@ -25,7 +25,7 @@ class UserValidator extends Validator {
25
25
  }
26
26
  ```
27
27
 
28
- ### Constraints for multiple fields
28
+ ### Constraints for multiple properties
29
29
 
30
30
  ```typescript
31
31
  import { Validator } from 'knight-validation'
@@ -41,12 +41,12 @@ class UserValidator extends Validator {
41
41
  ### Available constraints
42
42
 
43
43
  ```typescript
44
- // Check if a field is absent
44
+ // Check if a property is absent
45
45
  new Absent
46
46
 
47
- // Check a field's value equals one of the three given strings
47
+ // Check a property's value equals one of the three given strings
48
48
  new Enum('Moni', 'Lisa', 'Anna')
49
- // Check if a field's value equals one of the three given numbers
49
+ // Check if a property's value equals one of the three given numbers
50
50
  new Enum(1, 3, 7)
51
51
 
52
52
  new Exists(async (user: User) => {
@@ -54,12 +54,12 @@ new Exists(async (user: User) => {
54
54
  // Return true if your exists condition is met
55
55
  })
56
56
 
57
- // Check if a field is there
57
+ // Check if a property is there
58
58
  new Required
59
59
 
60
- // Check if a field's value is of the given JavaScript type
60
+ // Check if a property's value is of the given JavaScript type
61
61
  new TypeOf('number')
62
- // Check if a field's value is an instance if the given class
62
+ // Check if a property's value is an instance if the given class
63
63
  new TypeOf(Date)
64
64
 
65
65
  new Unique(async (user: User) => {
@@ -70,7 +70,7 @@ new Unique(async (user: User) => {
70
70
 
71
71
  ### Validation
72
72
 
73
- Validating an object returns an array of misfits which is empty if there are not any. The validator goes through all constraints for one field and stops the validation on the first misfit. Afterwards it goes on to the next field.
73
+ Validating an object returns an array of misfits which is empty if there are not any. The validator goes through all constraints for one property and stops the validation on the first misfit. Afterwards it goes on to the next property.
74
74
 
75
75
  ```typescript
76
76
  let user = new User
@@ -86,19 +86,19 @@ A misfit contains the following informations by default.
86
86
 
87
87
  ```typescript
88
88
  // The name of the misfit which defaults to the name of the constraint which was not met
89
- misfit.name == 'Required'
89
+ misfit.constraint == 'Required'
90
90
 
91
- // The field where the misfit occured
92
- misfit.field == 'email'
91
+ // The property where the misfit occured
92
+ misfit.property == 'email'
93
93
 
94
- // The fields where the misfit
95
- misfit.fields == ['firstName', 'lastName']
94
+ // The properties where the misfit occured
95
+ misfit.properties == ['firstName', 'lastName']
96
96
 
97
97
  // It contains any information that is useful about why checking the constraint resulted in a misfit (Optional)
98
- misfit.constraints
98
+ misfit.value
99
99
 
100
100
  // A message (Optional)
101
- misift.message == 'The field email is required.'
101
+ misift.message == 'The property email is required.'
102
102
  ```
103
103
 
104
104
  #### Check only what is there
@@ -111,7 +111,7 @@ user.email = undefined
111
111
 
112
112
  let misfits = validator.validate(user, { checkOnlyWhatIsThere: true })
113
113
 
114
- misfits.length == 0 // There are no misfits even though the email field is required
114
+ misfits.length == 0 // There are no misfits even though the email property is required
115
115
  ```
116
116
 
117
117
  ### Constraints that are only checked if a condition is met
@@ -132,11 +132,11 @@ class UserValidator extends Validator {
132
132
  #### Exclude rules
133
133
 
134
134
  ```typescript
135
- // Exclude all constraints regarding the email field
135
+ // Exclude all constraints regarding the email property
136
136
  let misfits = validator.validate(user, { exclude: ['email'] })
137
137
 
138
- // Exclude only the required constraint of the email field
139
- let misfits = validator.validate(user, { exclude: [{ field: 'email', constraint: 'Required' }] })
138
+ // Exclude only the required constraint of the email property
139
+ let misfits = validator.validate(user, { exclude: [{ properties: 'email', constraint: 'Required' }] })
140
140
  ```
141
141
 
142
142
  ### Anonymous custom constraints
@@ -150,7 +150,7 @@ validator.add(['firstName', 'lastName'], 'Different', async (user: User) => {
150
150
  if (user.firstName == user.lastName) {
151
151
  let misfit = new Misfit
152
152
 
153
- // You can skip setting a name and the field(s) for the misfit. These will be set automatically.
153
+ // You can skip setting a name and the property(s) for the misfit. These will be set automatically.
154
154
 
155
155
  // We give it some information here on what went wrong. What you put in here depends on your needs.
156
156
  misfit.constraints = {
@@ -173,32 +173,32 @@ import { Constraint } from 'knight-validation'
173
173
  class YourConstraint extends Constraint {
174
174
 
175
175
  // Override the abstract method validate
176
- async validate(obj: any, field: string|string[]): Promise<Misfit|undefined> {
176
+ async validate(obj: any, properties: string|string[]): Promise<Misfit|undefined> {
177
177
 
178
- // At first you want to check if the field is absent because in case of absense you do not want to validate because a field may be optional.
179
- if (this.isFieldAbsent(obj, field)) {
178
+ // At first you want to check if the properties are absent because in case of absense you do not want to validate because a property may be optional.
179
+ if (this.arePropertyAbsent(obj, properties)) {
180
180
  return
181
181
  }
182
182
 
183
- // Next you need to check if the field was a single or a combined one. Maybe you just implement on of the two possibilities.
184
- if (typeof field == 'string') {
185
- // In case of a single field
183
+ // Next you need to check if the properties were single or a nultiple ones. Maybe you just implement on of the two possibilities.
184
+ if (typeof properties == 'string') {
185
+ // In case of a single property
186
186
  }
187
187
  else {
188
- // In case of multiple fields
188
+ // In case of multiple properties
189
189
  }
190
190
  }
191
191
  }
192
192
  ```
193
193
 
194
- Another possibility is to use the `defaultValidation` method. It will do the check for absence and will implement the validation of combined fields.
194
+ Another possibility is to use the `defaultValidation` method. It will do the check for absence and will implement the validation of combined properties.
195
195
 
196
196
  ```typescript
197
197
  import { Constraint } from 'knight-validation'
198
198
 
199
199
  class YourConstraint extends Constraint {
200
- async validate(obj: any, field: string|string[]): Promise<Misfit|undefined> {
201
- return this.defaultValidation(obj, field, async (value: any) => {
200
+ async validate(obj: any, properties: string|string[]): Promise<Misfit|undefined> {
201
+ return this.defaultValidation(obj, properties, async (value: any) => {
202
202
  if (value == 1) { // Validate the value here
203
203
  return new Misfit
204
204
  }
package/lib/index.js CHANGED
@@ -1,28 +1,28 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
14
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
15
+ };
2
16
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var Constraint_1 = require("./lib/Constraint");
4
- Object.defineProperty(exports, "Constraint", { enumerable: true, get: function () { return Constraint_1.default; } });
5
- var Absent_1 = require("./lib/constraints/Absent");
6
- Object.defineProperty(exports, "Absent", { enumerable: true, get: function () { return Absent_1.default; } });
7
- var Enum_1 = require("./lib/constraints/Enum");
8
- Object.defineProperty(exports, "Enum", { enumerable: true, get: function () { return Enum_1.default; } });
9
- var Exists_1 = require("./lib/constraints/Exists");
10
- Object.defineProperty(exports, "Exists", { enumerable: true, get: function () { return Exists_1.default; } });
11
- var Max_1 = require("./lib/constraints/Max");
12
- Object.defineProperty(exports, "Max", { enumerable: true, get: function () { return Max_1.default; } });
13
- var QuickConstraint_1 = require("./lib/constraints/QuickConstraint");
14
- Object.defineProperty(exports, "QuickConstraint", { enumerable: true, get: function () { return QuickConstraint_1.default; } });
15
- var Required_1 = require("./lib/constraints/Required");
16
- Object.defineProperty(exports, "Required", { enumerable: true, get: function () { return Required_1.default; } });
17
- var TypeOf_1 = require("./lib/constraints/TypeOf");
18
- Object.defineProperty(exports, "TypeOf", { enumerable: true, get: function () { return TypeOf_1.default; } });
19
- var Unique_1 = require("./lib/constraints/Unique");
20
- Object.defineProperty(exports, "Unique", { enumerable: true, get: function () { return Unique_1.default; } });
21
- var fieldsEqual_1 = require("./lib/fieldsEqual");
22
- Object.defineProperty(exports, "fieldsEqual", { enumerable: true, get: function () { return fieldsEqual_1.default; } });
23
- var Misfit_1 = require("./lib/Misfit");
24
- Object.defineProperty(exports, "Misfit", { enumerable: true, get: function () { return Misfit_1.default; } });
25
- var MisfitsError_1 = require("./lib/MisfitsError");
26
- Object.defineProperty(exports, "MisfitsError", { enumerable: true, get: function () { return MisfitsError_1.default; } });
27
- var Validator_1 = require("./lib/Validator");
28
- Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return Validator_1.default; } });
17
+ __exportStar(require("./lib/constraints/Absent"), exports);
18
+ __exportStar(require("./lib/constraints/Bounds"), exports);
19
+ __exportStar(require("./lib/constraints/Enum"), exports);
20
+ __exportStar(require("./lib/constraints/Exists"), exports);
21
+ __exportStar(require("./lib/constraints/Length"), exports);
22
+ __exportStar(require("./lib/constraints/Required"), exports);
23
+ __exportStar(require("./lib/constraints/TypeOf"), exports);
24
+ __exportStar(require("./lib/constraints/Unique"), exports);
25
+ __exportStar(require("./lib/Misfit"), exports);
26
+ __exportStar(require("./lib/MisfitsError"), exports);
27
+ __exportStar(require("./lib/QuickConstraint"), exports);
28
+ __exportStar(require("./lib/Validator"), exports);
@@ -1,125 +1,9 @@
1
1
  "use strict";
2
- var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
- return new (P || (P = Promise))(function (resolve, reject) {
5
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
- step((generator = generator.apply(thisArg, _arguments || [])).next());
9
- });
10
- };
11
- var __generator = (this && this.__generator) || function (thisArg, body) {
12
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g;
13
- return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
14
- function verb(n) { return function (v) { return step([n, v]); }; }
15
- function step(op) {
16
- if (f) throw new TypeError("Generator is already executing.");
17
- while (_) try {
18
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
19
- if (y = 0, t) op = [op[0] & 2, t.value];
20
- switch (op[0]) {
21
- case 0: case 1: t = op; break;
22
- case 4: _.label++; return { value: op[1], done: false };
23
- case 5: _.label++; y = op[1]; op = [0]; continue;
24
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
25
- default:
26
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
27
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
28
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
29
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
30
- if (t[2]) _.ops.pop();
31
- _.trys.pop(); continue;
32
- }
33
- op = body.call(thisArg, _);
34
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
35
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
36
- }
37
- };
38
2
  Object.defineProperty(exports, "__esModule", { value: true });
39
- var Constraint = /** @class */ (function () {
40
- function Constraint() {
3
+ exports.Constraint = void 0;
4
+ class Constraint {
5
+ constructor() {
41
6
  this.name = this.constructor.name;
42
7
  }
43
- Constraint.prototype.defaultValidation = function (obj, field, validateValue) {
44
- return __awaiter(this, void 0, void 0, function () {
45
- var value, _i, field_1, fld, value, misfit;
46
- return __generator(this, function (_a) {
47
- switch (_a.label) {
48
- case 0:
49
- if (this.isFieldAbsent(obj, field)) {
50
- return [2 /*return*/];
51
- }
52
- if (!(typeof field == 'string')) return [3 /*break*/, 1];
53
- value = obj[field];
54
- return [2 /*return*/, validateValue(value)];
55
- case 1:
56
- if (!(field instanceof Array)) return [3 /*break*/, 6];
57
- _i = 0, field_1 = field;
58
- _a.label = 2;
59
- case 2:
60
- if (!(_i < field_1.length)) return [3 /*break*/, 5];
61
- fld = field_1[_i];
62
- value = obj[fld];
63
- return [4 /*yield*/, validateValue(value)];
64
- case 3:
65
- misfit = _a.sent();
66
- if (misfit) {
67
- return [2 /*return*/, misfit];
68
- }
69
- _a.label = 4;
70
- case 4:
71
- _i++;
72
- return [3 /*break*/, 2];
73
- case 5: return [3 /*break*/, 7];
74
- case 6: throw new Error('Parameter field was neither of type string nor instance of Array');
75
- case 7: return [2 /*return*/];
76
- }
77
- });
78
- });
79
- };
80
- Constraint.prototype.validateValue = function (value) {
81
- return __awaiter(this, void 0, void 0, function () {
82
- var misfit;
83
- return __generator(this, function (_a) {
84
- switch (_a.label) {
85
- case 0: return [4 /*yield*/, this.validate({ value: value }, 'value')];
86
- case 1:
87
- misfit = _a.sent();
88
- if (misfit) {
89
- return [2 /*return*/, misfit];
90
- }
91
- return [2 /*return*/];
92
- }
93
- });
94
- });
95
- };
96
- Constraint.prototype.isFieldAbsent = function (obj, field) {
97
- if (typeof field == 'string') {
98
- var value = obj[field];
99
- if (Constraint.absent(value)) {
100
- return true;
101
- }
102
- }
103
- else if (field instanceof Array) {
104
- for (var _i = 0, field_2 = field; _i < field_2.length; _i++) {
105
- var fld = field_2[_i];
106
- var value = obj[fld];
107
- if (Constraint.absent(value)) {
108
- return true;
109
- }
110
- }
111
- }
112
- else {
113
- throw new Error('Parameter field was neither of type string nor instance of Array');
114
- }
115
- return false;
116
- };
117
- Constraint.absent = function (value) {
118
- return value === undefined ||
119
- value === null ||
120
- value === '' ||
121
- typeof value === 'number' && isNaN(value);
122
- };
123
- return Constraint;
124
- }());
125
- exports.default = Constraint;
8
+ }
9
+ exports.Constraint = Constraint;
package/lib/lib/Misfit.js CHANGED
@@ -1,50 +1,30 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- var Absent_1 = require("./constraints/Absent");
4
- var Exists_1 = require("./constraints/Exists");
5
- var Required_1 = require("./constraints/Required");
6
- var TypeOf_1 = require("./constraints/TypeOf");
7
- var Unique_1 = require("./constraints/Unique");
8
- var fieldsEqual_1 = require("./fieldsEqual");
9
- var Misfit = /** @class */ (function () {
10
- function Misfit(constraint, field, values) {
3
+ exports.Misfit = void 0;
4
+ class Misfit {
5
+ constructor(constraint, values) {
6
+ this.properties = [];
11
7
  this.constraint = constraint;
12
- if (typeof field == 'string') {
13
- this.field = field;
8
+ this.values = values;
9
+ }
10
+ setProperties(property) {
11
+ if (typeof property == 'string') {
12
+ this.properties = [property];
14
13
  }
15
- else if (field instanceof Array) {
16
- this.fields = field;
14
+ else if (property instanceof Array) {
15
+ this.properties = property;
17
16
  }
18
- this.values = values;
19
17
  }
20
- Misfit.prototype.setMessage = function (message) {
21
- this.message = message;
22
- return this;
23
- };
24
- Misfit.prototype.isSingleField = function () {
25
- return this.field != undefined;
26
- };
27
- Misfit.prototype.isMultipleField = function () {
28
- return this.fields != undefined;
29
- };
30
- Misfit.prototype.fieldsEqual = function (fields) {
31
- return fieldsEqual_1.default(this.fields, fields);
32
- };
33
- Misfit.required = function (field, message) {
34
- return new Misfit(Required_1.default.name, field).setMessage(message);
35
- };
36
- Misfit.absent = function (field, message) {
37
- return new Misfit(Absent_1.default.name, field).setMessage(message);
38
- };
39
- Misfit.unique = function (field, message) {
40
- return new Misfit(Unique_1.default.name, field).setMessage(message);
41
- };
42
- Misfit.exists = function (field, message) {
43
- return new Misfit(Exists_1.default.name, field).setMessage(message);
44
- };
45
- Misfit.typeOf = function (field, message) {
46
- return new Misfit(TypeOf_1.default.name, field).setMessage(message);
47
- };
48
- return Misfit;
49
- }());
50
- exports.default = Misfit;
18
+ addPrefix(prefix) {
19
+ for (let i = 0; i < this.properties.length; i++) {
20
+ this.properties[i] = prefix + this.properties[i];
21
+ }
22
+ }
23
+ isSinglePropery() {
24
+ return this.properties && this.properties.length == 1;
25
+ }
26
+ isMultipleProperties() {
27
+ return this.properties && this.properties.length > 1;
28
+ }
29
+ }
30
+ exports.Misfit = Misfit;
@@ -1,25 +1,10 @@
1
1
  "use strict";
2
- var __extends = (this && this.__extends) || (function () {
3
- var extendStatics = function (d, b) {
4
- extendStatics = Object.setPrototypeOf ||
5
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
6
- function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
7
- return extendStatics(d, b);
8
- };
9
- return function (d, b) {
10
- extendStatics(d, b);
11
- function __() { this.constructor = d; }
12
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
13
- };
14
- })();
15
2
  Object.defineProperty(exports, "__esModule", { value: true });
16
- var MisfitsError = /** @class */ (function (_super) {
17
- __extends(MisfitsError, _super);
18
- function MisfitsError(misfits) {
19
- var _this = _super.call(this, 'Misfits occured ' + JSON.stringify(misfits)) || this;
20
- _this.misfits = misfits instanceof Array ? misfits : [misfits];
21
- return _this;
3
+ exports.MisfitsError = void 0;
4
+ class MisfitsError extends Error {
5
+ constructor(misfits) {
6
+ super('Misfits occured ' + JSON.stringify(misfits));
7
+ this.misfits = misfits instanceof Array ? misfits : [misfits];
22
8
  }
23
- return MisfitsError;
24
- }(Error));
25
- exports.default = MisfitsError;
9
+ }
10
+ exports.MisfitsError = MisfitsError;
@@ -0,0 +1,26 @@
1
+ "use strict";
2
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
3
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
4
+ return new (P || (P = Promise))(function (resolve, reject) {
5
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
6
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
7
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
8
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
9
+ });
10
+ };
11
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ exports.QuickConstraint = void 0;
13
+ const Constraint_1 = require("./Constraint");
14
+ class QuickConstraint extends Constraint_1.Constraint {
15
+ constructor(name, validateFn) {
16
+ super();
17
+ this.name = name;
18
+ this.validateFn = validateFn;
19
+ }
20
+ validate(obj) {
21
+ return __awaiter(this, void 0, void 0, function* () {
22
+ return this.validateFn(obj);
23
+ });
24
+ }
25
+ }
26
+ exports.QuickConstraint = QuickConstraint;