thesimplevalidation 1.0.1 → 1.0.2

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.
Files changed (42) hide show
  1. package/README.md +0 -1
  2. package/dist/index.d.ts +1 -1
  3. package/dist/index.js +17 -6
  4. package/dist/index.js.map +1 -1
  5. package/dist/src/index.d.ts +8 -8
  6. package/dist/src/index.js +22 -10
  7. package/dist/src/index.js.map +1 -1
  8. package/dist/src/validation-errors-model.d.ts +3 -3
  9. package/dist/src/validation-errors-model.js +2 -2
  10. package/dist/src/validation-model.d.ts +3 -3
  11. package/dist/src/validation-model.js +2 -2
  12. package/dist/src/validation-property-result.d.ts +4 -4
  13. package/dist/src/validation-property-result.js +2 -2
  14. package/dist/src/validation-result.d.ts +8 -8
  15. package/dist/src/validation-result.js +2 -2
  16. package/dist/src/validation-scope.d.ts +19 -19
  17. package/dist/src/validation-scope.js +199 -196
  18. package/dist/src/validation-scope.js.map +1 -1
  19. package/dist/src/validator-setup.d.ts +4 -4
  20. package/dist/src/validator-setup.js +2 -2
  21. package/dist/src/validator.d.ts +8 -8
  22. package/dist/src/validator.js +68 -67
  23. package/dist/src/validator.js.map +1 -1
  24. package/dist/src/validators/boolean/index.d.ts +10 -10
  25. package/dist/src/validators/boolean/index.js +79 -76
  26. package/dist/src/validators/boolean/index.js.map +1 -1
  27. package/dist/src/validators/index.d.ts +5 -5
  28. package/dist/src/validators/index.js +13 -12
  29. package/dist/src/validators/index.js.map +1 -1
  30. package/dist/src/validators/max/index.d.ts +10 -10
  31. package/dist/src/validators/max/index.js +87 -84
  32. package/dist/src/validators/max/index.js.map +1 -1
  33. package/dist/src/validators/min/index.d.ts +10 -10
  34. package/dist/src/validators/min/index.js +88 -85
  35. package/dist/src/validators/min/index.js.map +1 -1
  36. package/dist/src/validators/required/index.d.ts +7 -7
  37. package/dist/src/validators/required/index.js +83 -80
  38. package/dist/src/validators/required/index.js.map +1 -1
  39. package/dist/src/validators/simple/index.d.ts +10 -10
  40. package/dist/src/validators/simple/index.js +75 -72
  41. package/dist/src/validators/simple/index.js.map +1 -1
  42. package/package.json +10 -13
package/README.md CHANGED
@@ -5,7 +5,6 @@ The simple validation service
5
5
  [![Build Status](https://travis-ci.org/ripenko/thesimplevalidation.svg?branch=master)](https://travis-ci.org/ripenko/thesimplevalidation)
6
6
 
7
7
  ## Installation
8
-
9
8
  ```
10
9
  npm install --save thesimplevalidation
11
10
  ```
package/dist/index.d.ts CHANGED
@@ -1 +1 @@
1
- export * from "./src";
1
+ export * from "./src";
package/dist/index.js CHANGED
@@ -1,7 +1,18 @@
1
- "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- __export(require("./src"));
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
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ __exportStar(require("./src"), exports);
7
18
  //# sourceMappingURL=index.js.map
package/dist/index.js.map CHANGED
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;AAAA,2BAAsB"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;AAAA,wCAAsB"}
@@ -1,8 +1,8 @@
1
- export { ValidationScope } from "./validation-scope";
2
- export { ValidationErrorsModel } from "./validation-errors-model";
3
- export { ValidationModel } from "./validation-model";
4
- export { IValidationPropertyResult } from "./validation-property-result";
5
- export { IValidationResult } from "./validation-result";
6
- export { Validator } from "./validator";
7
- export { IValidatorSetup } from "./validator-setup";
8
- export * from "./validators";
1
+ export { ValidationScope } from "./validation-scope";
2
+ export { ValidationErrorsModel } from "./validation-errors-model";
3
+ export { ValidationModel } from "./validation-model";
4
+ export { IValidationPropertyResult } from "./validation-property-result";
5
+ export { IValidationResult } from "./validation-result";
6
+ export { Validator } from "./validator";
7
+ export { IValidatorSetup } from "./validator-setup";
8
+ export * from "./validators";
package/dist/src/index.js CHANGED
@@ -1,11 +1,23 @@
1
- "use strict";
2
- function __export(m) {
3
- for (var p in m) if (!exports.hasOwnProperty(p)) exports[p] = m[p];
4
- }
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- var validation_scope_1 = require("./validation-scope");
7
- exports.ValidationScope = validation_scope_1.ValidationScope;
8
- var validator_1 = require("./validator");
9
- exports.Validator = validator_1.Validator;
10
- __export(require("./validators"));
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
+ };
16
+ Object.defineProperty(exports, "__esModule", { value: true });
17
+ exports.Validator = exports.ValidationScope = void 0;
18
+ var validation_scope_1 = require("./validation-scope");
19
+ Object.defineProperty(exports, "ValidationScope", { enumerable: true, get: function () { return validation_scope_1.ValidationScope; } });
20
+ var validator_1 = require("./validator");
21
+ Object.defineProperty(exports, "Validator", { enumerable: true, get: function () { return validator_1.Validator; } });
22
+ __exportStar(require("./validators"), exports);
11
23
  //# sourceMappingURL=index.js.map
@@ -1 +1 @@
1
- {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;AAAA,uDAAqD;AAA5C,6CAAA,eAAe,CAAA;AAKxB,yCAAwC;AAA/B,gCAAA,SAAS,CAAA;AAGlB,kCAA6B"}
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;AAAA,uDAAqD;AAA5C,mHAAA,eAAe,OAAA;AAKxB,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAGlB,+CAA6B"}
@@ -1,3 +1,3 @@
1
- export declare type ValidationErrorsModel<TModel> = {
2
- [k in keyof TModel]: string[];
3
- };
1
+ export type ValidationErrorsModel<TModel> = {
2
+ [k in keyof TModel]: string[];
3
+ };
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=validation-errors-model.js.map
@@ -1,3 +1,3 @@
1
- export declare type ValidationModel<TModel> = {
2
- [K in keyof TModel]: boolean;
3
- };
1
+ export type ValidationModel<TModel> = {
2
+ [K in keyof TModel]: boolean;
3
+ };
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=validation-model.js.map
@@ -1,4 +1,4 @@
1
- export interface IValidationPropertyResult {
2
- isValid: boolean;
3
- errors: string[];
4
- }
1
+ export interface IValidationPropertyResult {
2
+ isValid: boolean;
3
+ errors: string[];
4
+ }
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=validation-property-result.js.map
@@ -1,8 +1,8 @@
1
- import { IValidationPropertyResult } from "./validation-property-result";
2
- export interface IValidationResult<TModel> {
3
- isValid: boolean;
4
- properties: {
5
- [K in keyof TModel]: IValidationPropertyResult;
6
- };
7
- errors: string[];
8
- }
1
+ import { IValidationPropertyResult } from "./validation-property-result";
2
+ export interface IValidationResult<TModel> {
3
+ isValid: boolean;
4
+ properties: {
5
+ [K in keyof TModel]: IValidationPropertyResult;
6
+ };
7
+ errors: string[];
8
+ }
@@ -1,3 +1,3 @@
1
- "use strict";
2
- Object.defineProperty(exports, "__esModule", { value: true });
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
3
  //# sourceMappingURL=validation-result.js.map
@@ -1,19 +1,19 @@
1
- import { IValidationPropertyResult } from "./validation-property-result";
2
- import { IValidationResult } from "./validation-result";
3
- import { Validator } from "./validator";
4
- export declare class ValidationScope<TModel extends {}> {
5
- private setup;
6
- private originalModel;
7
- private modelInfo;
8
- constructor(setup: {
9
- getModel: () => TModel;
10
- onValidationChanged?: (result: IValidationResult<TModel>) => Promise<void>;
11
- });
12
- useOriginal: (model: TModel) => ValidationScope<TModel>;
13
- useValidators: <K extends keyof TModel>(key: K, ...validators: Validator<TModel, K, import("./validator-setup").IValidatorSetup<TModel, K>>[]) => ValidationScope<TModel>;
14
- isPropertyDirty: (field: keyof TModel, key?: string) => boolean;
15
- getOriginalProperty: <K extends keyof TModel>(field: K) => TModel[K];
16
- isDirty: (...fields: (keyof TModel)[]) => boolean;
17
- isPropertyValid: <K extends keyof TModel>(field: K) => Promise<IValidationPropertyResult>;
18
- isValid: () => Promise<IValidationResult<TModel>>;
19
- }
1
+ import { IValidationPropertyResult } from "./validation-property-result";
2
+ import { IValidationResult } from "./validation-result";
3
+ import { Validator } from "./validator";
4
+ export declare class ValidationScope<TModel extends {}> {
5
+ private setup;
6
+ private originalModel;
7
+ private modelInfo;
8
+ constructor(setup: {
9
+ getModel: () => TModel;
10
+ onValidationChanged?: (result: IValidationResult<TModel>) => Promise<void>;
11
+ });
12
+ useOriginal: (model: TModel) => ValidationScope<TModel>;
13
+ useValidators: <K extends keyof TModel>(key: K, ...validators: Validator<TModel, K, import("./validator-setup").IValidatorSetup<TModel, K>>[]) => ValidationScope<TModel>;
14
+ isPropertyDirty: (field: keyof TModel, key?: string | null) => boolean;
15
+ getOriginalProperty: <K extends keyof TModel>(field: K) => TModel[K];
16
+ isDirty: (...fields: Array<keyof TModel>) => boolean;
17
+ isPropertyValid: <K extends keyof TModel>(field: K) => Promise<IValidationPropertyResult>;
18
+ isValid: () => Promise<IValidationResult<TModel>>;
19
+ }
@@ -1,197 +1,200 @@
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
- var __spreadArrays = (this && this.__spreadArrays) || function () {
39
- for (var s = 0, i = 0, il = arguments.length; i < il; i++) s += arguments[i].length;
40
- for (var r = Array(s), k = 0, i = 0; i < il; i++)
41
- for (var a = arguments[i], j = 0, jl = a.length; j < jl; j++, k++)
42
- r[k] = a[j];
43
- return r;
44
- };
45
- var __importDefault = (this && this.__importDefault) || function (mod) {
46
- return (mod && mod.__esModule) ? mod : { "default": mod };
47
- };
48
- Object.defineProperty(exports, "__esModule", { value: true });
49
- var lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
50
- var lodash_get_1 = __importDefault(require("lodash.get"));
51
- var lodash_isequal_1 = __importDefault(require("lodash.isequal"));
52
- var lodash_keys_1 = __importDefault(require("lodash.keys"));
53
- var ValidationScope = /** @class */ (function () {
54
- function ValidationScope(setup) {
55
- var _this = this;
56
- this.setup = setup;
57
- this.useOriginal = function (model) {
58
- _this.originalModel = lodash_clonedeep_1.default(model);
59
- return _this;
60
- };
61
- this.useValidators = function (key) {
62
- var validators = [];
63
- for (var _i = 1; _i < arguments.length; _i++) {
64
- validators[_i - 1] = arguments[_i];
65
- }
66
- _this.modelInfo[key] = {
67
- validators: validators
68
- };
69
- return _this;
70
- };
71
- this.isPropertyDirty = function (field, key) {
72
- if (key === void 0) { key = null; }
73
- var model = _this.setup.getModel();
74
- var originalField = _this.originalModel[field];
75
- var modelField = model[field];
76
- if (Array.isArray(originalField) && Array.isArray(modelField)) {
77
- if (originalField.length !== modelField.length)
78
- return true;
79
- for (var index = 0; index < originalField.length; index++) {
80
- if (originalField[index] === modelField[index])
81
- continue;
82
- if (key == null)
83
- return true;
84
- if (lodash_get_1.default(originalField[index], key) !== lodash_get_1.default(modelField[index], key))
85
- return true;
86
- }
87
- return false;
88
- }
89
- if (key == null)
90
- return !lodash_isequal_1.default(_this.originalModel[field], model[field]);
91
- return lodash_get_1.default(_this.originalModel[field], key) !== lodash_get_1.default(model[field], key);
92
- };
93
- this.getOriginalProperty = function (field) {
94
- return _this.originalModel[field];
95
- };
96
- this.isDirty = function () {
97
- var fields = [];
98
- for (var _i = 0; _i < arguments.length; _i++) {
99
- fields[_i] = arguments[_i];
100
- }
101
- var _loop_1 = function (property) {
102
- if (!_this.originalModel.hasOwnProperty(property))
103
- return "continue";
104
- if (fields && fields.length > 0) {
105
- if (!fields.some(function (x) { return x === property; }))
106
- return "continue";
107
- }
108
- if (_this.isPropertyDirty(property))
109
- return { value: true };
110
- };
111
- for (var property in _this.originalModel) {
112
- var state_1 = _loop_1(property);
113
- if (typeof state_1 === "object")
114
- return state_1.value;
115
- }
116
- return false;
117
- };
118
- this.isPropertyValid = function (field) { return __awaiter(_this, void 0, void 0, function () {
119
- var isValid, errors, model, value, _i, _a, validator, validatorResult;
120
- return __generator(this, function (_b) {
121
- switch (_b.label) {
122
- case 0:
123
- if (!(this.modelInfo[field] && this.modelInfo[field].validators && this.modelInfo[field].validators)) return [3 /*break*/, 5];
124
- isValid = true;
125
- errors = [];
126
- model = this.setup.getModel();
127
- value = model[field];
128
- _i = 0, _a = this.modelInfo[field].validators;
129
- _b.label = 1;
130
- case 1:
131
- if (!(_i < _a.length)) return [3 /*break*/, 4];
132
- validator = _a[_i];
133
- return [4 /*yield*/, validator.isValid(value, model, field)];
134
- case 2:
135
- validatorResult = _b.sent();
136
- isValid = isValid && validatorResult.isValid;
137
- if (!validatorResult.isValid)
138
- errors = __spreadArrays(errors, validatorResult.errors);
139
- _b.label = 3;
140
- case 3:
141
- _i++;
142
- return [3 /*break*/, 1];
143
- case 4: return [2 /*return*/, {
144
- isValid: isValid,
145
- errors: errors
146
- }];
147
- case 5: return [2 /*return*/, {
148
- isValid: true,
149
- errors: []
150
- }];
151
- }
152
- });
153
- }); };
154
- this.isValid = function () { return __awaiter(_this, void 0, void 0, function () {
155
- var properties, result, _i, properties_1, property, propertyValidationResult;
156
- return __generator(this, function (_a) {
157
- switch (_a.label) {
158
- case 0:
159
- properties = lodash_keys_1.default(this.setup.getModel());
160
- result = {
161
- isValid: true,
162
- errors: [],
163
- properties: {}
164
- };
165
- _i = 0, properties_1 = properties;
166
- _a.label = 1;
167
- case 1:
168
- if (!(_i < properties_1.length)) return [3 /*break*/, 4];
169
- property = properties_1[_i];
170
- return [4 /*yield*/, this.isPropertyValid(property)];
171
- case 2:
172
- propertyValidationResult = _a.sent();
173
- result.isValid = result.isValid && propertyValidationResult.isValid;
174
- result.properties[property] = propertyValidationResult;
175
- if (!propertyValidationResult.isValid)
176
- result.errors = __spreadArrays(result.errors, propertyValidationResult.errors);
177
- _a.label = 3;
178
- case 3:
179
- _i++;
180
- return [3 /*break*/, 1];
181
- case 4:
182
- if (!(this.setup.onValidationChanged != null)) return [3 /*break*/, 6];
183
- return [4 /*yield*/, this.setup.onValidationChanged(result)];
184
- case 5:
185
- _a.sent();
186
- _a.label = 6;
187
- case 6: return [2 /*return*/, result];
188
- }
189
- });
190
- }); };
191
- this.originalModel = lodash_clonedeep_1.default(setup.getModel());
192
- this.modelInfo = {};
193
- }
194
- return ValidationScope;
195
- }());
196
- exports.ValidationScope = ValidationScope;
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 (g && (g = 0, op[0] && (_ = 0)), _) 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
+ var __spreadArray = (this && this.__spreadArray) || function (to, from, pack) {
39
+ if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
40
+ if (ar || !(i in from)) {
41
+ if (!ar) ar = Array.prototype.slice.call(from, 0, i);
42
+ ar[i] = from[i];
43
+ }
44
+ }
45
+ return to.concat(ar || Array.prototype.slice.call(from));
46
+ };
47
+ var __importDefault = (this && this.__importDefault) || function (mod) {
48
+ return (mod && mod.__esModule) ? mod : { "default": mod };
49
+ };
50
+ Object.defineProperty(exports, "__esModule", { value: true });
51
+ exports.ValidationScope = void 0;
52
+ var lodash_clonedeep_1 = __importDefault(require("lodash.clonedeep"));
53
+ var lodash_get_1 = __importDefault(require("lodash.get"));
54
+ var lodash_isequal_1 = __importDefault(require("lodash.isequal"));
55
+ var lodash_keys_1 = __importDefault(require("lodash.keys"));
56
+ var ValidationScope = /** @class */ (function () {
57
+ function ValidationScope(setup) {
58
+ var _this = this;
59
+ this.setup = setup;
60
+ this.useOriginal = function (model) {
61
+ _this.originalModel = (0, lodash_clonedeep_1.default)(model);
62
+ return _this;
63
+ };
64
+ this.useValidators = function (key) {
65
+ var validators = [];
66
+ for (var _i = 1; _i < arguments.length; _i++) {
67
+ validators[_i - 1] = arguments[_i];
68
+ }
69
+ _this.modelInfo[key] = {
70
+ validators: validators
71
+ };
72
+ return _this;
73
+ };
74
+ this.isPropertyDirty = function (field, key) {
75
+ if (key === void 0) { key = null; }
76
+ var model = _this.setup.getModel();
77
+ var originalField = _this.originalModel[field];
78
+ var modelField = model[field];
79
+ if (Array.isArray(originalField) && Array.isArray(modelField)) {
80
+ if (originalField.length !== modelField.length)
81
+ return true;
82
+ for (var index = 0; index < originalField.length; index++) {
83
+ if (originalField[index] === modelField[index])
84
+ continue;
85
+ if (key == null)
86
+ return true;
87
+ if ((0, lodash_get_1.default)(originalField[index], key) !== (0, lodash_get_1.default)(modelField[index], key))
88
+ return true;
89
+ }
90
+ return false;
91
+ }
92
+ if (key == null)
93
+ return !(0, lodash_isequal_1.default)(_this.originalModel[field], model[field]);
94
+ return (0, lodash_get_1.default)(_this.originalModel[field], key) !== (0, lodash_get_1.default)(model[field], key);
95
+ };
96
+ this.getOriginalProperty = function (field) {
97
+ return _this.originalModel[field];
98
+ };
99
+ this.isDirty = function () {
100
+ var fields = [];
101
+ for (var _i = 0; _i < arguments.length; _i++) {
102
+ fields[_i] = arguments[_i];
103
+ }
104
+ var _loop_1 = function (property) {
105
+ if (!_this.originalModel.hasOwnProperty(property))
106
+ return "continue";
107
+ if (fields && fields.length > 0) {
108
+ if (!fields.some(function (x) { return x === property; }))
109
+ return "continue";
110
+ }
111
+ if (_this.isPropertyDirty(property))
112
+ return { value: true };
113
+ };
114
+ for (var property in _this.originalModel) {
115
+ var state_1 = _loop_1(property);
116
+ if (typeof state_1 === "object")
117
+ return state_1.value;
118
+ }
119
+ return false;
120
+ };
121
+ this.isPropertyValid = function (field) { return __awaiter(_this, void 0, void 0, function () {
122
+ var isValid, errors, model, value, _i, _a, validator, validatorResult;
123
+ return __generator(this, function (_b) {
124
+ switch (_b.label) {
125
+ case 0:
126
+ if (!(this.modelInfo[field] && this.modelInfo[field].validators && this.modelInfo[field].validators)) return [3 /*break*/, 5];
127
+ isValid = true;
128
+ errors = [];
129
+ model = this.setup.getModel();
130
+ value = model[field];
131
+ _i = 0, _a = this.modelInfo[field].validators;
132
+ _b.label = 1;
133
+ case 1:
134
+ if (!(_i < _a.length)) return [3 /*break*/, 4];
135
+ validator = _a[_i];
136
+ return [4 /*yield*/, validator.isValid(value, model, field)];
137
+ case 2:
138
+ validatorResult = _b.sent();
139
+ isValid = isValid && validatorResult.isValid;
140
+ if (!validatorResult.isValid)
141
+ errors = __spreadArray(__spreadArray([], errors, true), validatorResult.errors, true);
142
+ _b.label = 3;
143
+ case 3:
144
+ _i++;
145
+ return [3 /*break*/, 1];
146
+ case 4: return [2 /*return*/, {
147
+ isValid: isValid,
148
+ errors: errors
149
+ }];
150
+ case 5: return [2 /*return*/, {
151
+ isValid: true,
152
+ errors: []
153
+ }];
154
+ }
155
+ });
156
+ }); };
157
+ this.isValid = function () { return __awaiter(_this, void 0, void 0, function () {
158
+ var properties, result, _i, properties_1, property, propertyValidationResult;
159
+ return __generator(this, function (_a) {
160
+ switch (_a.label) {
161
+ case 0:
162
+ properties = (0, lodash_keys_1.default)(this.setup.getModel());
163
+ result = {
164
+ isValid: true,
165
+ errors: [],
166
+ properties: {}
167
+ };
168
+ _i = 0, properties_1 = properties;
169
+ _a.label = 1;
170
+ case 1:
171
+ if (!(_i < properties_1.length)) return [3 /*break*/, 4];
172
+ property = properties_1[_i];
173
+ return [4 /*yield*/, this.isPropertyValid(property)];
174
+ case 2:
175
+ propertyValidationResult = _a.sent();
176
+ result.isValid = result.isValid && propertyValidationResult.isValid;
177
+ result.properties[property] = propertyValidationResult;
178
+ if (!propertyValidationResult.isValid)
179
+ result.errors = __spreadArray(__spreadArray([], result.errors, true), propertyValidationResult.errors, true);
180
+ _a.label = 3;
181
+ case 3:
182
+ _i++;
183
+ return [3 /*break*/, 1];
184
+ case 4:
185
+ if (!(this.setup.onValidationChanged != null)) return [3 /*break*/, 6];
186
+ return [4 /*yield*/, this.setup.onValidationChanged(result)];
187
+ case 5:
188
+ _a.sent();
189
+ _a.label = 6;
190
+ case 6: return [2 /*return*/, result];
191
+ }
192
+ });
193
+ }); };
194
+ this.originalModel = (0, lodash_clonedeep_1.default)(setup.getModel());
195
+ this.modelInfo = {};
196
+ }
197
+ return ValidationScope;
198
+ }());
199
+ exports.ValidationScope = ValidationScope;
197
200
  //# sourceMappingURL=validation-scope.js.map