knight-validation 3.4.0 → 4.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
@@ -54,13 +54,13 @@ class UserValidator extends Validator {
54
54
  constructor() {
55
55
  super()
56
56
 
57
- // Exactly one property is required
57
+ // Exactly one property must be there
58
58
  this.add(['fistName', 'lastName', 'email'], new Required({ exactFits: 1 }))
59
59
 
60
- // At least two properties are required
60
+ // At least two properties must be there
61
61
  this.add(['fistName', 'lastName', 'email'], new Required({ minFits: 2 }))
62
62
 
63
- // At most two properties are required
63
+ // At most two properties must be there
64
64
  this.add(['fistName', 'lastName', 'email'], new Required({ maxFits: 2 }))
65
65
  }
66
66
  }
@@ -133,7 +133,7 @@ misfit.property == [ 'email' ]
133
133
  misfit.values
134
134
 
135
135
  // A misfit message (Optional)
136
- misift.message == 'The property email is required.'
136
+ misfit.message == 'The property email is required.'
137
137
  ```
138
138
 
139
139
  The optional `message` needs to be provided by your application, it is not part of this package.
@@ -4,24 +4,28 @@ export interface ValidatorOptions {
4
4
  checkOnlyWhatIsThere?: boolean;
5
5
  exclude?: string[];
6
6
  }
7
- interface ValidatorEntry<T = any> {
7
+ export interface ValidatorEntry<T = any> {
8
8
  properties: string[];
9
9
  constraint?: Constraint;
10
10
  validator?: Validator;
11
11
  condition?: (object: T) => Promise<boolean>;
12
12
  }
13
+ declare abstract class ValidatorFactory<T = any> {
14
+ abstract create(): Validator<T>;
15
+ }
13
16
  export declare class Validator<T = any> {
14
17
  options?: ValidatorOptions;
15
18
  entries: ValidatorEntry<T>[];
16
19
  constructor(options?: ValidatorOptions);
17
20
  add(constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
18
21
  add(constraintName: string, validate: (value: any) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
22
+ add(validatorFactory: ValidatorFactory): void;
19
23
  add(property: string, constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
20
24
  add(property: string, constraintName: string, validate: (value: any) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
25
+ add(property: string, validatorFactory: ValidatorFactory, condition?: (object: T) => Promise<boolean>): void;
21
26
  add(properties: string[], constraint: Constraint, condition?: (object: T) => Promise<boolean>): void;
22
27
  add(properties: string[], constraintName: string, validate: (object: T, properties: string[]) => Promise<Misfit | null>, condition?: (object: T) => Promise<boolean>): void;
23
- add(property: string, validator: Validator<any>, condition?: (object: T) => Promise<boolean>): void;
24
- add(validator: Validator): void;
28
+ add(validatorEntry: ValidatorEntry): void;
25
29
  validate(object: T, options?: ValidatorOptions): Promise<Misfit[]>;
26
30
  }
27
31
  export {};
@@ -15,40 +15,51 @@ const Constraint_1 = require("./Constraint");
15
15
  const DotNotation_1 = require("./DotNotation");
16
16
  const QuickConstraint_1 = require("./constraints/QuickConstraint");
17
17
  let log = new knight_log_1.Log('knight-validation/Validator.ts');
18
+ class ValidatorFactory {
19
+ }
18
20
  class Validator {
19
21
  constructor(options) {
20
22
  this.entries = [];
21
23
  this.options = options;
22
24
  }
23
25
  add(...args) {
24
- var _a, _b;
26
+ var _a, _b, _c;
25
27
  let l = log.mt('add');
26
28
  l.param('args', args);
27
- if (args[0] instanceof Validator) {
28
- let validator = args[0];
29
- for (let propertyConstraint of validator.entries) {
30
- this.entries.push(propertyConstraint);
29
+ let properties;
30
+ let constraint;
31
+ let validatorFactory;
32
+ let condition;
33
+ if (typeof args[0] == 'object' && args[0] !== null && 'create' in args[0] && typeof args[0].create == 'function') {
34
+ let validator = args[0].create();
35
+ if (typeof validator == 'object' && validator !== null && 'entries' in validator && Array.isArray(validator.entries)) {
36
+ for (let entry of validator.entries) {
37
+ this.add(entry);
38
+ }
39
+ }
40
+ }
41
+ else if (typeof args[0] == 'object' && args[0] !== null && 'properties' in args[0] && Array.isArray(args[0].properties)) {
42
+ properties = args[0].properties;
43
+ constraint = args[0].constraint;
44
+ validatorFactory = args[0].validator ?
45
+ { create: () => args[0].validator } : undefined;
46
+ condition = args[0].condition;
47
+ if (constraint != undefined && validatorFactory != undefined) {
48
+ throw new Error('The provided parameter is a ValidatorEntry with both a constraint and a validator defined. This is invalid! Only one may be specified at a time!');
31
49
  }
32
50
  }
33
51
  else if (args[0] instanceof Constraint_1.Constraint) {
34
- this.entries.push({
35
- properties: [],
36
- constraint: args[0],
37
- condition: args.length > 1 ? args[1] : undefined
38
- });
52
+ properties = [];
53
+ constraint = args[0],
54
+ condition = args.length > 1 ? args[1] : undefined;
39
55
  }
40
- else if (typeof args[0] == 'string' && typeof args[1] == 'function') {
41
- this.entries.push({
42
- properties: [],
43
- constraint: new QuickConstraint_1.QuickConstraint(args[0], args[1]),
44
- condition: args.length > 2 ? args[2] : undefined
45
- });
56
+ else if (typeof args[0] == 'string' && typeof args[1] == 'function' && ((_a = args[1].prototype) === null || _a === void 0 ? void 0 : _a.constructor) !== args[1]) {
57
+ properties = [];
58
+ constraint = new QuickConstraint_1.QuickConstraint(args[0], args[1]);
59
+ condition = args.length > 2 ? args[2] : undefined;
46
60
  }
47
61
  else if (typeof args[0] == 'string' || Array.isArray(args[0])) {
48
- let properties = typeof args[0] == 'string' ? [args[0]] : args[0];
49
- let constraint = undefined;
50
- let validator = undefined;
51
- let condition;
62
+ properties = typeof args[0] == 'string' ? [args[0]] : args[0];
52
63
  if (typeof args[1] == 'string') {
53
64
  if (typeof args[0] == 'string') {
54
65
  constraint = new QuickConstraint_1.QuickConstraint(args[1], args[2], undefined);
@@ -62,35 +73,41 @@ class Validator {
62
73
  constraint = args[1];
63
74
  condition = args.length > 2 ? args[2] : undefined;
64
75
  }
65
- else if (args[1] instanceof Validator) {
66
- validator = args[1];
76
+ else if (typeof args[1] == 'object' && args[1] !== null && 'create' in args[1] && typeof args[1].create == 'function') {
77
+ validatorFactory = args[1];
67
78
  condition = args.length > 2 ? args[2] : undefined;
68
79
  }
69
- else {
70
- throw new Error('Invalid parameters');
71
- }
72
- l.dev('properties', properties);
73
- l.dev('constraint', constraint);
74
- l.dev('validator', validator);
75
- l.dev('condition', condition);
76
- if (properties.length == 1 && ((_a = this.options) === null || _a === void 0 ? void 0 : _a.exclude)) {
77
- for (let property of (_b = this.options) === null || _b === void 0 ? void 0 : _b.exclude) {
78
- if (property == properties[0]) {
79
- l.dev('Not adding constraint since the property was excluded');
80
- l.returning();
81
- return;
82
- }
80
+ }
81
+ if (properties == undefined) {
82
+ l.returning();
83
+ return;
84
+ }
85
+ let propertyExcluded = false;
86
+ if (properties.length == 1 && ((_b = this.options) === null || _b === void 0 ? void 0 : _b.exclude)) {
87
+ for (let property of (_c = this.options) === null || _c === void 0 ? void 0 : _c.exclude) {
88
+ if (property == properties[0]) {
89
+ propertyExcluded = true;
90
+ l.dev('Not adding constraint since the property was excluded', {
91
+ properties: properties,
92
+ constraint: constraint,
93
+ validatorFactory: validatorFactory,
94
+ condition: condition
95
+ });
96
+ break;
83
97
  }
84
98
  }
85
- this.entries.push({
99
+ }
100
+ if (!propertyExcluded) {
101
+ let entry = {
86
102
  properties: properties,
87
103
  constraint: constraint,
88
- validator: validator,
104
+ validator: validatorFactory ? validatorFactory.create() : undefined,
89
105
  condition: condition
90
- });
91
- l.dev('Constraint was added');
92
- l.returning();
106
+ };
107
+ this.entries.push(entry);
108
+ l.dev('Constraint was added', entry);
93
109
  }
110
+ l.returning();
94
111
  }
95
112
  validate(object, options) {
96
113
  return __awaiter(this, void 0, void 0, function* () {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "knight-validation",
3
- "version": "3.4.0",
3
+ "version": "4.0.0",
4
4
  "description": "A validation lib",
5
5
  "main": "lib/index.js",
6
6
  "types": "lib/index.d.ts",