knight-validation 2.0.6 → 3.0.0

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
@@ -8,6 +8,8 @@
8
8
 
9
9
  ### Constraints for single properties
10
10
 
11
+ Add constraints to single properties.
12
+
11
13
  ```typescript
12
14
  import { Required, TypeOf, Unique, Validator } from 'knight-validation'
13
15
 
@@ -27,20 +29,39 @@ class UserValidator extends Validator {
27
29
 
28
30
  ### Constraints for multiple properties
29
31
 
32
+ You can also use any constraint on multiple properties at once.
33
+
30
34
  ```typescript
31
- import { Validator } from 'knight-validation'
35
+ import { Required, Validator } from 'knight-validation'
32
36
 
33
37
  class UserValidator extends Validator {
34
- constructor(userDb: UserDb) {
38
+ constructor() {
35
39
  super()
36
-
37
- this.add(['firstName', 'lastName'], 'Different', async (user: User) => {
38
- if (user.firstName == user.lastName) {
39
- // You do not need to set the constraint name nor the properties on the misfit.
40
- // Those will be set automatically.
41
- return new Misfit
42
- }
43
- })
40
+
41
+ this.add(['fistName', 'lastName', 'email'], new Required)
42
+ }
43
+ }
44
+ ```
45
+
46
+ The given constraint is being applied to every given property. If one property is not fitting the constraint, the whole constraint will be regarded as not fulfilled.
47
+
48
+ Every constraint offers a set of parameters with which you can fine tune its behavior.
49
+
50
+ ```typescript
51
+ import { Required, Validator } from 'knight-validation'
52
+
53
+ class UserValidator extends Validator {
54
+ constructor() {
55
+ super()
56
+
57
+ // Exactly one property is required
58
+ this.add(['fistName', 'lastName', 'email'], new Required({ exactFits: 1 }))
59
+
60
+ // At least two properties are required
61
+ this.add(['fistName', 'lastName', 'email'], new Required({ minFits: 2 }))
62
+
63
+ // At most two properties are required
64
+ this.add(['fistName', 'lastName', 'email'], new Required({ maxFits: 2 }))
44
65
  }
45
66
  }
46
67
  ```
@@ -54,7 +75,7 @@ new Absent
54
75
  // Check if a number is greater and/or lesser than a given number
55
76
  new Bounds({ greaterThan: 0, lesserThan: 10 })
56
77
  // Check if a number is greater and/or lesser than or equal a given number
57
- new Bounds({ greaterThanEqual: 0, lesserThanqEqual: 10 })
78
+ new Bounds({ greaterThanEqual: 0, lesserThanEqual: 10 })
58
79
 
59
80
  // Check a property's value equals one of the three given strings
60
81
  new Enum('Moni', 'Lisa', 'Anna')
@@ -66,16 +87,16 @@ new Exists(async (user: User) => {
66
87
  // Return true if your exists condition is met
67
88
  })
68
89
 
69
- // Check if the length of a string has a minmum and/or maximum length
90
+ // Check if the length of a string or array has a minmum and/or maximum length
70
91
  new Length({ min: 5, max: 10})
71
- // Check if the length of a string has exactly a specific length
92
+ // Check if the length of a string or array has exactly a specific length
72
93
  new Length({ exact: 5 })
73
94
 
74
- // Check if a property is there
95
+ // Check that a property is not undefined
75
96
  new Required
76
97
 
77
- // Check if a property's value is of the given JavaScript type
78
- new TypeOf('number')
98
+ // Check if a property's value is one of the given JavaScript type
99
+ new TypeOf('number', null)
79
100
  // Check if a property's value is an instance if the given class
80
101
  new TypeOf(Date)
81
102
 
@@ -99,7 +120,7 @@ let misfits = validator.validate(user)
99
120
  misfits.length == 1
100
121
  ```
101
122
 
102
- A misfit contains the following informations by default.
123
+ A misfit contains the following information.
103
124
 
104
125
  ```typescript
105
126
  // The name of the constraint that resulted in the misfit
@@ -115,9 +136,24 @@ misfit.values
115
136
  misift.message == 'The property email is required.'
116
137
  ```
117
138
 
139
+ The optional `message` needs to be provided by your application, it is not part of this package.
140
+
141
+ Every misfit has a `values` property which holds an object containing contextual information specific to the constraint that created the misfit. Every misfit that is provided in this package, extends the interface `ConstraintMisfitValues` to define the appearance of the `values` object.
142
+
143
+ ```typescript
144
+ interface ConstraintMisfitValues {
145
+ minFits?: number
146
+ maxFits?: number
147
+ exactFits?: number
148
+ misfits?: Misfit[]
149
+ }
150
+ ```
151
+
152
+ Its properties come into play if the misfit was applied to multiple properties. The `misfits` property references an array of misfits that were found when the constraint was checked on each property individually.
153
+
118
154
  #### Check only what is there
119
155
 
120
- You can validate only what is there. This means any constraint becomes optional.
156
+ You can validate only what is there. This means, every constraint is only applied when the value of a property is not `undefined`. This is useful if you want to work with partial objects for updating purposes.
121
157
 
122
158
  ```typescript
123
159
  let user = new User
@@ -130,6 +166,8 @@ misfits.length == 0 // There are no misfits even though the email property is re
130
166
 
131
167
  ### Constraints that are only checked if a condition is met
132
168
 
169
+ You can provide a condition which is evaluated before the constraint is being checked. If the condition is not met, the constraint will not be checked.
170
+
133
171
  ```typescript
134
172
  import { Required, Validator } from 'knight-validation'
135
173
 
@@ -181,15 +219,47 @@ validator.add(['firstName', 'lastName'], 'Different', async (user: User) => {
181
219
  })
182
220
  ```
183
221
 
184
- In the case the misfit involves exactly one property, its corresponding validation function receives the value of that one property as a parameter. In the case the misfit involves more than one property, the corresponding validation function receives the whole object that is being validated, so that all of the involved properties can be accessed.
222
+ In the case the misfit involves exactly one property, its corresponding validation function receives the value of that one property as a parameter. In the case the misfit involves more than one property, the corresponding validation function receives the whole object that is being validated plus the properties that are to be involved in the check. That way, the individual properties can be accessed and involved in the check.
185
223
 
186
224
  ### Custom constraints as classes
187
225
 
188
226
  If you want to reuse a constraint over and over again, create a new class for it.
189
227
 
228
+ ```typescript
229
+ import { Constraint, ConstraintMisfitValues } from 'knight-validation'
230
+
231
+ // You need to extend from the default ConstraintMisfitValues since it provides
232
+ // the properties being used when the constraint is being applied to multiple properties
233
+ export interface OnlyGermanMailsMisfitValues extends ConstraintMisfitValues {
234
+ actualTld?: string
235
+ }
236
+
237
+ export class OnlyGermanMailsConstraint extends Constraint<string, OnlyGermanMailsMisfitValues> {
238
+
239
+ validate(email: string): Promise<Misfit<OnlyGermanMailsMisfitValues>|null> {
240
+ if (! email.endsWith('.de')) {
241
+ let misfit = new Misfit<OnlyGermanMailsMisfitValues>
242
+
243
+ misfit.values = {
244
+ actualTld: email.split('.').slice(-1)[0]
245
+ }
246
+
247
+ // You do not need to set the constraint name nor the property on the misfit.
248
+ // Those will be set automatically.
249
+
250
+ return new Misfit
251
+ }
252
+ }
253
+ }
254
+ ```
255
+
256
+ If you want to define specific behavior for the case that the constraint is being used to validate multiple properties instead of only one, then overwrite the method `validateMultipleProperties`.
257
+
190
258
  ```typescript
191
259
  import { Constraint } from 'knight-validation'
192
260
 
261
+ // You do not need to extend the default ConstraintMisfitValues since we are overriding the
262
+ // method that defines the behavior when the misfit is being used on multiple properties
193
263
  export interface DifferentMisfitValues {
194
264
  firstName: string
195
265
  lastName: string
@@ -197,22 +267,21 @@ export interface DifferentMisfitValues {
197
267
 
198
268
  export class Different extends Constraint<User, DifferentMisfitValues> {
199
269
 
200
- // Override the abstract method validate
201
- validate(user: User): Promise<Misfit<DifferentMisfitValues>|null> {
270
+ validateMultipleProperties(object: any, properties: string[]): Promise<Misfit<DifferentMisfitValues>|null> {
202
271
  if (user.firstName == user.lastName) {
203
272
  let misfit = new Misfit<DifferentMisfitValues>
204
273
 
205
- // You do not need to set the constraint name nor the properties on the misfit.
206
- // Those will be set automatically.
207
-
208
274
  misfit.values = {
209
275
  firstName: user.firstName,
210
276
  lastName: user.lastName
211
277
  }
212
278
 
279
+ // You do not need to set the constraint name nor the properties on the misfit.
280
+ // Those will be set automatically.
281
+
213
282
  return misfit
214
283
  }
215
284
  }
216
285
  ```
217
286
 
218
- You need to pay attention to the parameter of the validate method. In the case your constraint is assigned to a single property, that parameter will have the value of the mentioned property. In the case your constraint is assigned to an array of properties, that parameter will have the complete object as its value which ought to contain the mentioned properties.
287
+ In the `Constraint` super class, the method `validateMultipleProperties` is being implemented with a useful default behavior. It iterates through every property and uses the `validate` method to check each property individually, while also offering the parameters `exactFits`, `minFits` and `maxFits` to adjust its behavior.
package/lib/index.d.ts CHANGED
@@ -7,7 +7,5 @@ export * from './lib/constraints/Length';
7
7
  export * from './lib/constraints/Required';
8
8
  export * from './lib/constraints/TypeOf';
9
9
  export * from './lib/constraints/Unique';
10
- export * from './lib/Misfit';
11
- export * from './lib/MisfitsError';
12
- export * from './lib/QuickConstraint';
10
+ export * from './lib/constraints/QuickConstraint';
13
11
  export * from './lib/Validator';
package/lib/index.js CHANGED
@@ -23,7 +23,5 @@ __exportStar(require("./lib/constraints/Length"), exports);
23
23
  __exportStar(require("./lib/constraints/Required"), exports);
24
24
  __exportStar(require("./lib/constraints/TypeOf"), exports);
25
25
  __exportStar(require("./lib/constraints/Unique"), exports);
26
- __exportStar(require("./lib/Misfit"), exports);
27
- __exportStar(require("./lib/MisfitsError"), exports);
28
- __exportStar(require("./lib/QuickConstraint"), exports);
26
+ __exportStar(require("./lib/constraints/QuickConstraint"), exports);
29
27
  __exportStar(require("./lib/Validator"), exports);
@@ -1,5 +1,15 @@
1
- import { Misfit } from './Misfit';
2
- export declare abstract class Constraint<T = any, MisfitValuesType = any> {
1
+ import { Misfit } from 'knight-misfit';
2
+ export interface ConstraintMisfitValues {
3
+ minFits?: number;
4
+ maxFits?: number;
5
+ exactFits?: number;
6
+ misfits?: Misfit[];
7
+ }
8
+ export declare abstract class Constraint<T = any, MisfitValuesType = ConstraintMisfitValues> {
3
9
  name: string;
4
- abstract validate(value: T): Promise<Misfit<MisfitValuesType> | null>;
10
+ minFits?: number;
11
+ maxFits?: number;
12
+ exactFits?: number;
13
+ validate(value: T): Promise<Misfit<MisfitValuesType> | null>;
14
+ validateMultipleProperties(object: any, properties: string[]): Promise<Misfit<MisfitValuesType> | null>;
5
15
  }
@@ -1,9 +1,88 @@
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
+ };
2
11
  Object.defineProperty(exports, "__esModule", { value: true });
3
12
  exports.Constraint = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
14
+ const DotNotation_1 = require("./DotNotation");
4
15
  class Constraint {
5
16
  constructor() {
6
17
  this.name = this.constructor.name;
7
18
  }
19
+ validate(value) {
20
+ return __awaiter(this, void 0, void 0, function* () {
21
+ return null;
22
+ });
23
+ }
24
+ validateMultipleProperties(object, properties) {
25
+ return __awaiter(this, void 0, void 0, function* () {
26
+ let misfits = [];
27
+ for (let property of properties) {
28
+ let dotNotification = new DotNotation_1.DotNotation(property);
29
+ let value = dotNotification.get(object);
30
+ let misfit = yield this.validate(value);
31
+ if (misfit != null) {
32
+ misfits.push(misfit);
33
+ }
34
+ }
35
+ let fits = properties.length - misfits.length;
36
+ if (this.exactFits != undefined) {
37
+ if (fits != this.exactFits) {
38
+ return new knight_misfit_1.Misfit(this.name + 'ExactFits', undefined, {
39
+ atLeastFits: this.minFits,
40
+ atMostFits: this.maxFits,
41
+ exactFits: this.exactFits,
42
+ misfits: misfits
43
+ });
44
+ }
45
+ }
46
+ else if (this.minFits != undefined && this.maxFits != undefined) {
47
+ if (fits < this.minFits || fits > this.maxFits) {
48
+ return new knight_misfit_1.Misfit(this.name + 'MinAndMaxFits', undefined, {
49
+ atLeastFits: this.minFits,
50
+ atMostFits: this.maxFits,
51
+ exactFits: this.exactFits,
52
+ misfits: misfits
53
+ });
54
+ }
55
+ }
56
+ else if (this.minFits != undefined) {
57
+ if (fits < this.minFits) {
58
+ return new knight_misfit_1.Misfit(this.name + 'MinFits', undefined, {
59
+ atLeastFits: this.minFits,
60
+ atMostFits: this.maxFits,
61
+ exactFits: this.exactFits,
62
+ misfits: misfits
63
+ });
64
+ }
65
+ }
66
+ else if (this.maxFits != undefined) {
67
+ if (fits > this.maxFits) {
68
+ return new knight_misfit_1.Misfit(this.name + 'MaxFits', undefined, {
69
+ atLeastFits: this.minFits,
70
+ atMostFits: this.maxFits,
71
+ exactFits: this.exactFits,
72
+ misfits: misfits
73
+ });
74
+ }
75
+ }
76
+ else if (misfits.length > 0) {
77
+ return new knight_misfit_1.Misfit(this.name, undefined, {
78
+ atLeastFits: this.minFits,
79
+ atMostFits: this.maxFits,
80
+ exactFits: this.exactFits,
81
+ misfits: misfits
82
+ });
83
+ }
84
+ return null;
85
+ });
86
+ }
8
87
  }
9
88
  exports.Constraint = Constraint;
@@ -1,4 +1,4 @@
1
- export declare class DotNotification {
1
+ export declare class DotNotation {
2
2
  properties: string[];
3
3
  constructor(dotNotification: string);
4
4
  exists(obj: any): boolean;
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.DotNotification = void 0;
4
- class DotNotification {
3
+ exports.DotNotation = void 0;
4
+ class DotNotation {
5
5
  constructor(dotNotification) {
6
6
  this.properties = dotNotification.split('.');
7
7
  }
@@ -32,4 +32,4 @@ class DotNotification {
32
32
  return obj;
33
33
  }
34
34
  }
35
- exports.DotNotification = DotNotification;
35
+ exports.DotNotation = DotNotation;
@@ -1,5 +1,5 @@
1
+ import { Misfit } from 'knight-misfit';
1
2
  import { Constraint } from './Constraint';
2
- import { Misfit } from './Misfit';
3
3
  export interface ValidatorOptions {
4
4
  checkOnlyWhatIsThere?: boolean;
5
5
  }
@@ -15,7 +15,8 @@ export declare class Validator<T = any> {
15
15
  constructor(options?: ValidatorOptions);
16
16
  add(property: string, constraint: Constraint<T>, condition?: (object: T) => Promise<boolean>): void;
17
17
  add(property: string, constraintName: string, validate: (value: any) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
18
- add(properties: string[], constraintName: string, validate: (object: T) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
18
+ add(properties: string[], constraint: Constraint<T>, condition?: (object: T) => Promise<boolean>): void;
19
+ add(properties: string[], constraintName: string, validate: (object: T, properties: string[]) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
19
20
  add(property: string, validator: Validator<any>, condition?: (object: T) => Promise<boolean>): void;
20
21
  add(validator: Validator<any>): void;
21
22
  validate(object: T, options?: ValidatorOptions): Promise<Misfit[]>;
@@ -11,8 +11,8 @@ 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");
15
- const QuickConstraint_1 = require("./QuickConstraint");
14
+ const DotNotation_1 = require("./DotNotation");
15
+ const QuickConstraint_1 = require("./constraints/QuickConstraint");
16
16
  class Validator {
17
17
  constructor(options) {
18
18
  this.entries = [];
@@ -31,7 +31,12 @@ class Validator {
31
31
  let validator = undefined;
32
32
  let condition;
33
33
  if (typeof args[1] == 'string') {
34
- constraint = new QuickConstraint_1.QuickConstraint(args[1], args[2]);
34
+ if (typeof args[0] == 'string') {
35
+ constraint = new QuickConstraint_1.QuickConstraint(args[1], args[2], undefined);
36
+ }
37
+ else {
38
+ constraint = new QuickConstraint_1.QuickConstraint(args[1], undefined, args[2]);
39
+ }
35
40
  condition = args.length > 3 ? args[3] : undefined;
36
41
  }
37
42
  else if (args[1] instanceof Constraint_1.Constraint) {
@@ -74,7 +79,7 @@ class Validator {
74
79
  }
75
80
  let atLeastOnePropertyExists = false;
76
81
  for (let property of entry.properties) {
77
- let dotNotification = new DotNotification_1.DotNotification(property);
82
+ let dotNotification = new DotNotation_1.DotNotation(property);
78
83
  if (dotNotification.exists(object)) {
79
84
  atLeastOnePropertyExists = true;
80
85
  break;
@@ -87,12 +92,12 @@ class Validator {
87
92
  let misfit;
88
93
  if (entry.properties.length == 1) {
89
94
  let property = entry.properties[0];
90
- let dotNotification = new DotNotification_1.DotNotification(property);
95
+ let dotNotification = new DotNotation_1.DotNotation(property);
91
96
  let value = dotNotification.get(object);
92
97
  misfit = yield entry.constraint.validate(value);
93
98
  }
94
99
  else {
95
- misfit = yield entry.constraint.validate(object);
100
+ misfit = yield entry.constraint.validateMultipleProperties(object, entry.properties);
96
101
  }
97
102
  if (misfit) {
98
103
  if (misfit.constraint === undefined) {
@@ -108,7 +113,7 @@ class Validator {
108
113
  throw new Error('Using another validator only works for one property');
109
114
  }
110
115
  let property = entry.properties[0];
111
- let dotNotification = new DotNotification_1.DotNotification(property);
116
+ let dotNotification = new DotNotation_1.DotNotation(property);
112
117
  let value = dotNotification.get(object);
113
118
  if (value === undefined) {
114
119
  continue;
@@ -1,6 +1,6 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export interface AbsentMisfitValues {
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export interface AbsentMisfitValues extends ConstraintMisfitValues {
4
4
  actual: any;
5
5
  }
6
6
  export declare class Absent extends Constraint<any, AbsentMisfitValues> {
@@ -10,12 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Absent = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class Absent extends Constraint_1.Constraint {
16
16
  validate(value) {
17
17
  return __awaiter(this, void 0, void 0, function* () {
18
- return value !== undefined ? new Misfit_1.Misfit(this.name, { actual: value }) : null;
18
+ return value !== undefined ? new knight_misfit_1.Misfit(this.name, undefined, { actual: value }) : null;
19
19
  });
20
20
  }
21
21
  }
@@ -1,6 +1,6 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export interface BoundsMisfitValues {
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export interface BoundsMisfitValues extends ConstraintMisfitValues {
4
4
  actual: any;
5
5
  greaterThan?: number;
6
6
  greaterThanEqual?: number;
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Bounds = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class Bounds extends Constraint_1.Constraint {
16
16
  constructor(constraints) {
17
17
  super();
@@ -38,7 +38,7 @@ class Bounds extends Constraint_1.Constraint {
38
38
  });
39
39
  }
40
40
  _createMisfit(actual) {
41
- return new Misfit_1.Misfit(this.name, {
41
+ return new knight_misfit_1.Misfit(this.name, undefined, {
42
42
  actual: actual,
43
43
  greaterThan: this.greaterThan,
44
44
  greaterThanEqual: this.greaterThanEqual,
@@ -1,6 +1,6 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export interface EnumMisfitValues {
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export interface EnumMisfitValues extends ConstraintMisfitValues {
4
4
  actual: any;
5
5
  values: any[];
6
6
  }
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Enum = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class Enum extends Constraint_1.Constraint {
16
16
  constructor(...values) {
17
17
  super();
@@ -38,7 +38,7 @@ class Enum extends Constraint_1.Constraint {
38
38
  return null;
39
39
  }
40
40
  if (this.values.indexOf(value) == -1) {
41
- return new Misfit_1.Misfit(this.name, {
41
+ return new knight_misfit_1.Misfit(this.name, undefined, {
42
42
  actual: value,
43
43
  values: this.values
44
44
  });
@@ -1,6 +1,6 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export interface ExistsMisfitValues {
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export interface ExistsMisfitValues extends ConstraintMisfitValues {
4
4
  notExistingValue: any;
5
5
  }
6
6
  export declare class Exists<T> extends Constraint<T, ExistsMisfitValues> {
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Exists = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class Exists extends Constraint_1.Constraint {
16
16
  constructor(doesExist) {
17
17
  super();
@@ -21,7 +21,7 @@ class Exists extends Constraint_1.Constraint {
21
21
  return __awaiter(this, void 0, void 0, function* () {
22
22
  if (value !== undefined) {
23
23
  if (!(yield this.doesExist(value))) {
24
- return new Misfit_1.Misfit(this.name, {
24
+ return new knight_misfit_1.Misfit(this.name, undefined, {
25
25
  notExistingValue: value
26
26
  });
27
27
  }
@@ -1,6 +1,6 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export interface LengthMisfitValues {
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export interface LengthMisfitValues extends ConstraintMisfitValues {
4
4
  actual: any;
5
5
  min?: number;
6
6
  max?: number;
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Length = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class Length extends Constraint_1.Constraint {
16
16
  constructor(constraints) {
17
17
  super();
@@ -35,7 +35,7 @@ class Length extends Constraint_1.Constraint {
35
35
  });
36
36
  }
37
37
  _createMisfit(actual) {
38
- return new Misfit_1.Misfit(this.name, {
38
+ return new knight_misfit_1.Misfit(this.name, undefined, {
39
39
  actual: actual,
40
40
  exact: this.exact,
41
41
  max: this.max,
@@ -0,0 +1,10 @@
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint } from '../Constraint';
3
+ export declare class QuickConstraint<T> extends Constraint<T, any> {
4
+ name: string;
5
+ validateFn?: (obj: T) => Promise<Misfit | null>;
6
+ validateMultiplePropertiesFn?: (obj: T, properties: string[]) => Promise<Misfit | null>;
7
+ constructor(name: string, validateFn?: (obj: T) => Promise<Misfit | null>, validateMultiplePropertiesFn?: (obj: T, properties: string[]) => Promise<Misfit | null>);
8
+ validate(obj: T): Promise<Misfit | null>;
9
+ validateMultipleProperties(obj: T, properties: string[]): Promise<Misfit | null>;
10
+ }
@@ -10,17 +10,32 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.QuickConstraint = void 0;
13
- const Constraint_1 = require("./Constraint");
13
+ const Constraint_1 = require("../Constraint");
14
14
  class QuickConstraint extends Constraint_1.Constraint {
15
- constructor(name, validateFn) {
15
+ constructor(name, validateFn, validateMultiplePropertiesFn) {
16
16
  super();
17
17
  this.name = name;
18
18
  this.validateFn = validateFn;
19
+ this.validateMultiplePropertiesFn = validateMultiplePropertiesFn;
19
20
  }
20
21
  validate(obj) {
21
22
  return __awaiter(this, void 0, void 0, function* () {
23
+ if (this.validateFn == undefined) {
24
+ throw new Error('Could not call method because the needed this.validateFn function was not set!');
25
+ }
22
26
  return this.validateFn(obj);
23
27
  });
24
28
  }
29
+ validateMultipleProperties(obj, properties) {
30
+ const _super = Object.create(null, {
31
+ validateMultipleProperties: { get: () => super.validateMultipleProperties }
32
+ });
33
+ return __awaiter(this, void 0, void 0, function* () {
34
+ if (this.validateMultiplePropertiesFn == undefined) {
35
+ return _super.validateMultipleProperties.call(this, obj, properties);
36
+ }
37
+ return this.validateMultiplePropertiesFn(obj, properties);
38
+ });
39
+ }
25
40
  }
26
41
  exports.QuickConstraint = QuickConstraint;
@@ -1,5 +1,5 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export declare class Required extends Constraint<any, void> {
4
- validate(value: any): Promise<Misfit<void> | null>;
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export declare class Required extends Constraint {
4
+ validate(value: any): Promise<Misfit<ConstraintMisfitValues> | null>;
5
5
  }
@@ -10,12 +10,12 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Required = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class Required extends Constraint_1.Constraint {
16
16
  validate(value) {
17
17
  return __awaiter(this, void 0, void 0, function* () {
18
- return value === undefined ? new Misfit_1.Misfit(this.name) : null;
18
+ return value === undefined ? new knight_misfit_1.Misfit(this.name) : null;
19
19
  });
20
20
  }
21
21
  }
@@ -1,6 +1,6 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export interface TypeOfMisfitValues {
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export interface TypeOfMisfitValues extends ConstraintMisfitValues {
4
4
  types: (string | null)[];
5
5
  actual: string | null;
6
6
  }
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.TypeOf = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class TypeOf extends Constraint_1.Constraint {
16
16
  constructor(...valueTypes) {
17
17
  super();
@@ -47,11 +47,10 @@ class TypeOf extends Constraint_1.Constraint {
47
47
  else {
48
48
  actualType = typeof value;
49
49
  }
50
- let misfit = new Misfit_1.Misfit(this.name);
51
- misfit.values = {
50
+ let misfit = new knight_misfit_1.Misfit(this.name, undefined, {
52
51
  actual: actualType,
53
52
  types: []
54
- };
53
+ });
55
54
  for (let valueType of this.valueTypes) {
56
55
  if (typeof valueType == 'string') {
57
56
  misfit.values.types.push(valueType);
@@ -1,6 +1,6 @@
1
- import { Constraint } from '../Constraint';
2
- import { Misfit } from '../Misfit';
3
- export interface UniqueMisfitValues {
1
+ import { Misfit } from 'knight-misfit';
2
+ import { Constraint, ConstraintMisfitValues } from '../Constraint';
3
+ export interface UniqueMisfitValues extends ConstraintMisfitValues {
4
4
  notUniqueValue: any;
5
5
  }
6
6
  export declare class Unique<T> extends Constraint<T, UniqueMisfitValues> {
@@ -10,8 +10,8 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  Object.defineProperty(exports, "__esModule", { value: true });
12
12
  exports.Unique = void 0;
13
+ const knight_misfit_1 = require("knight-misfit");
13
14
  const Constraint_1 = require("../Constraint");
14
- const Misfit_1 = require("../Misfit");
15
15
  class Unique extends Constraint_1.Constraint {
16
16
  constructor(isUnique) {
17
17
  super();
@@ -23,7 +23,7 @@ class Unique extends Constraint_1.Constraint {
23
23
  return null;
24
24
  }
25
25
  if (!(yield this.isUnique(value))) {
26
- return new Misfit_1.Misfit(this.name, { notUniqueValue: value });
26
+ return new knight_misfit_1.Misfit(this.name, undefined, { notUniqueValue: value });
27
27
  }
28
28
  return null;
29
29
  });
package/package.json CHANGED
@@ -1,35 +1,38 @@
1
1
  {
2
2
  "name": "knight-validation",
3
- "version": "2.0.6",
3
+ "version": "3.0.0",
4
4
  "description": "A validation lib",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",
7
7
  "files": [
8
8
  "lib/**/*"
9
9
  ],
10
- "devDependencies": {
11
- "@types/chai": "^4.3.3",
12
- "@types/mocha": "^9.1.1",
13
- "@types/node": "^13.5.0",
14
- "chai": "^4.3.6",
15
- "mocha": "^10.0.0",
16
- "ts-node": "^10.9.1",
17
- "typescript": "^4.8.2"
18
- },
19
10
  "scripts": {
20
11
  "test": "mocha -r ts-node/register \"test/**/*.test.ts\"",
21
12
  "build": "rm -rf lib && tsc"
22
13
  },
23
14
  "repository": {
24
15
  "type": "git",
25
- "url": "git+https://github.com/c0deritter/knight-validation.git"
16
+ "url": "git+https://github.com/coderitter/knight-validation.git"
26
17
  },
27
18
  "keywords": [
28
19
  ""
29
20
  ],
30
21
  "author": "Coderitter GmbH",
31
22
  "bugs": {
32
- "url": "https://github.com/c0deritter/knight-validation/issues"
23
+ "url": "https://github.com/coderitter/knight-validation/issues"
24
+ },
25
+ "homepage": "https://github.com/coderitter/knight-validation#readme",
26
+ "devDependencies": {
27
+ "@types/chai": "^5.2.2",
28
+ "@types/mocha": "^10.0.10",
29
+ "@types/node": "^24.0.4",
30
+ "chai": "^5.2.0",
31
+ "mocha": "^11.7.1",
32
+ "ts-node": "^10.9.2",
33
+ "typescript": "^5.8.3"
33
34
  },
34
- "homepage": "https://github.com/c0deritter/knight-validation#readme"
35
+ "dependencies": {
36
+ "knight-misfit": "^1.0.0"
37
+ }
35
38
  }
@@ -1,11 +0,0 @@
1
- export declare class Misfit<ValuesType = any> {
2
- constraint: string;
3
- properties: string[];
4
- values?: ValuesType;
5
- message?: string;
6
- constructor(constraint?: string, values?: ValuesType);
7
- setProperties(property: string | string[]): void;
8
- addPrefix(prefix: string): void;
9
- isSinglePropery(): boolean;
10
- isMultipleProperties(): boolean;
11
- }
package/lib/lib/Misfit.js DELETED
@@ -1,30 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.Misfit = void 0;
4
- class Misfit {
5
- constructor(constraint, values) {
6
- this.properties = [];
7
- this.constraint = constraint;
8
- this.values = values;
9
- }
10
- setProperties(property) {
11
- if (typeof property == 'string') {
12
- this.properties = [property];
13
- }
14
- else if (property instanceof Array) {
15
- this.properties = property;
16
- }
17
- }
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,5 +0,0 @@
1
- import { Misfit } from './Misfit';
2
- export declare class MisfitsError extends Error {
3
- misfits: Misfit[];
4
- constructor(misfits: Misfit | Misfit[]);
5
- }
@@ -1,10 +0,0 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
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];
8
- }
9
- }
10
- exports.MisfitsError = MisfitsError;
@@ -1,8 +0,0 @@
1
- import { Constraint } from './Constraint';
2
- import { Misfit } from './Misfit';
3
- export declare class QuickConstraint<T> extends Constraint<T, any> {
4
- name: string;
5
- validateFn: (obj: T) => Promise<Misfit | null>;
6
- constructor(name: string, validateFn: (obj: T) => Promise<Misfit | null>);
7
- validate(obj: T): Promise<Misfit | null>;
8
- }