validno 0.2.4 → 0.2.6

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/dist/Schema.js CHANGED
@@ -1,18 +1,14 @@
1
+ import { ESchemaFields } from "./constants/schema.js";
1
2
  import validate from "./validate.js";
2
- export const defaultSchemaKeys = [
3
- "required",
4
- "type",
5
- "eachType",
6
- "rules",
7
- "title",
8
- "customMessage",
9
- "joinErrors"
10
- ];
3
+ export const defaultSchemaKeys = Object.values(ESchemaFields);
11
4
  export class Schema {
12
- constructor(inputSchema) {
13
- this.schema = inputSchema;
5
+ constructor(inputSchemaDefinition) {
6
+ if (!inputSchemaDefinition || typeof inputSchemaDefinition !== 'object') {
7
+ throw new Error("Invalid schema input");
8
+ }
9
+ this.schema = inputSchemaDefinition;
14
10
  }
15
- validate(data, keys) {
16
- return validate.call(this, this, data, keys);
11
+ validate(inputData, validationKeys) {
12
+ return validate.call(this, inputData, validationKeys);
17
13
  }
18
14
  }
@@ -9,6 +9,10 @@ class ValidnoResult {
9
9
  this.byKeys = (results === null || results === void 0 ? void 0 : results.byKeys) || {};
10
10
  this.errorsByKeys = (results === null || results === void 0 ? void 0 : results.errorsByKeys) || {};
11
11
  }
12
+ setNoData() {
13
+ this.ok = false;
14
+ this.errors = ['Отсутствует объект для проверки'];
15
+ }
12
16
  setKeyStatus(key, result) {
13
17
  this.byKeys[key] = result;
14
18
  }
@@ -68,7 +72,7 @@ class ValidnoResult {
68
72
  }
69
73
  }
70
74
  finish() {
71
- if (this.failed.length)
75
+ if (this.failed.length || this.errors.length)
72
76
  this.ok = false;
73
77
  else
74
78
  this.ok = true;
@@ -86,5 +90,8 @@ class ValidnoResult {
86
90
  errorsByKeys: this.errorsByKeys,
87
91
  };
88
92
  }
93
+ isValid() {
94
+ return this.ok === true;
95
+ }
89
96
  }
90
97
  export default ValidnoResult;
@@ -1,6 +1,11 @@
1
- export var ErrorKeywords;
2
- (function (ErrorKeywords) {
3
- ErrorKeywords["Missing"] = "missing";
4
- ErrorKeywords["Type"] = "type";
5
- ErrorKeywords["Rule"] = "rule";
6
- })(ErrorKeywords || (ErrorKeywords = {}));
1
+ export var EValidationId;
2
+ (function (EValidationId) {
3
+ EValidationId["Missing"] = "missing";
4
+ EValidationId["Type"] = "type";
5
+ EValidationId["Rule"] = "rule";
6
+ })(EValidationId || (EValidationId = {}));
7
+ export var EValidationDetails;
8
+ (function (EValidationDetails) {
9
+ EValidationDetails["OK"] = "ok";
10
+ EValidationDetails["INVALID_DATE"] = "\u0414\u0430\u0442\u0430 \u043D\u0435\u0432\u0430\u043B\u0438\u0434\u043D\u0430";
11
+ })(EValidationDetails || (EValidationDetails = {}));
@@ -0,0 +1,10 @@
1
+ export var ESchemaFields;
2
+ (function (ESchemaFields) {
3
+ ESchemaFields["Required"] = "required";
4
+ ESchemaFields["Type"] = "type";
5
+ ESchemaFields["EachType"] = "eachType";
6
+ ESchemaFields["Rules"] = "rules";
7
+ ESchemaFields["Title"] = "title";
8
+ ESchemaFields["CustomMessage"] = "customMessage";
9
+ ESchemaFields["JoinErrors"] = "joinErrors";
10
+ })(ESchemaFields || (ESchemaFields = {}));
package/dist/dev.js CHANGED
@@ -1,51 +1,30 @@
1
1
  import { Schema } from "./Schema.js";
2
- const validateConfig = (cfg) => {
3
- const cfgSchema = new Schema({
4
- str1: {
5
- type: String,
2
+ const testSchema = new Schema({
3
+ parent: {
4
+ collection: {
5
+ type: [String, Array],
6
6
  required: true
7
- },
8
- str2: {
9
- deep1: {
10
- type: String,
11
- required: true,
12
- }
13
7
  }
14
- });
15
- const res = cfgSchema.validate(cfg);
16
- console.log(res);
17
- };
18
- const uniTestService = {
19
- str1: 'xxxx'
20
- };
21
- validateConfig(uniTestService);
22
- console.log(uniTestService);
23
- console.log(uniTestService);
24
- const obj = {
25
- top: {
26
- mid1: {
27
- low1a: false,
28
- low1b: false
29
- },
30
- mid2: {
31
- low2a: false
32
- }
33
- },
34
- top2: false
35
- };
36
- const setNestedValue = (linkedObj, keysLevels, curLevel, valueToSet) => {
37
- const lvKey = keysLevels[curLevel];
38
- if (curLevel < keysLevels.length - 1) {
39
- if (!linkedObj[lvKey] || typeof linkedObj[lvKey] !== 'object') {
40
- linkedObj[lvKey] = {};
41
- }
42
- return setNestedValue(linkedObj[lvKey], keysLevels, curLevel + 1, valueToSet);
43
8
  }
44
- else {
45
- linkedObj[lvKey] = valueToSet;
9
+ });
10
+ const testObj = {
11
+ parent: {
12
+ collection: ['xxxx']
13
+ }
14
+ };
15
+ const testObj2 = {
16
+ parent: {
17
+ collection: false
46
18
  }
47
19
  };
48
- const setDeepValue = (obj, keysChain, valueToSet) => {
49
- const keysLevels = keysChain.split('.');
50
- setNestedValue(obj, keysLevels, 0, valueToSet);
20
+ const testObj3 = {
21
+ parent: {
22
+ collection: 'str'
23
+ }
51
24
  };
25
+ const res = testSchema.validate(testObj);
26
+ console.log(res.ok === true ? '#1 ✅' : '#1 ❌');
27
+ const res2 = testSchema.validate(testObj2);
28
+ console.log(res2.ok === false ? '#2 ✅' : '#2 ❌');
29
+ const res3 = testSchema.validate(testObj3);
30
+ console.log(res3.ok === true ? '#3 ✅' : '#3 ❌');
package/dist/index.js CHANGED
@@ -1,2 +1,4 @@
1
1
  import { Schema } from "./Schema.js";
2
+ import _validations from "./utils/validations.js";
3
+ export const validations = _validations;
2
4
  export default Schema;
@@ -0,0 +1,11 @@
1
+ import { EValidationDetails } from "../constants/details.js";
2
+ const _validateType = {
3
+ getResult: (key, passed, details = EValidationDetails.OK) => {
4
+ return {
5
+ key: key,
6
+ passed: passed,
7
+ details: details,
8
+ };
9
+ }
10
+ };
11
+ export default _validateType;
@@ -32,7 +32,7 @@ _validations.isNullOrUndefined = (value) => {
32
32
  return value === undefined || value === null;
33
33
  };
34
34
  _validations.isEmail = (value) => {
35
- const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
35
+ const emailRegex = /^(?!.*\.\.)[^\s@]+@[^\s@]+\.[^\s@]+$/;
36
36
  return emailRegex.test(value);
37
37
  };
38
38
  _validations.isDateYYYYMMDD = (value) => {
package/dist/validate.js CHANGED
@@ -1,114 +1,142 @@
1
- import checkType from "./checkType.js";
1
+ import checkType from "./validateType.js";
2
2
  import _errors from "./utils/errors.js";
3
- import checkRules from "./checkRules.js";
3
+ import checkRules from "./validateRules.js";
4
4
  import _helpers from "./utils/helpers.js";
5
- import { ErrorKeywords } from "./constants/details.js";
5
+ import { EValidationId } from "./constants/details.js";
6
6
  import ValidnoResult from "./ValidnoResult.js";
7
- function generateMsg(input) {
8
- let { key, deepKey, data, reqs } = input;
9
- const keyForMsg = deepKey || key;
10
- const keyTitle = 'title' in reqs ? reqs.title : keyForMsg;
11
- if (!reqs.customMessage)
12
- return _errors.getMissingError(keyForMsg);
13
- const errMsg = reqs.customMessage({
14
- keyword: ErrorKeywords.Missing,
7
+ function handleMissingKey(schema, input) {
8
+ const { key, nestedKey, data, reqs } = input;
9
+ const messageKey = nestedKey || key;
10
+ const messageTitle = reqs.title || messageKey;
11
+ if (!reqs.customMessage) {
12
+ return _errors.getMissingError(messageKey);
13
+ }
14
+ const errorMessage = reqs.customMessage({
15
+ keyword: EValidationId.Missing,
15
16
  value: data[key],
16
- key: keyForMsg,
17
- title: keyTitle,
18
- reqs: reqs,
19
- schema: this.schema
17
+ key: messageKey,
18
+ title: messageTitle,
19
+ reqs,
20
+ schema,
20
21
  });
21
- return errMsg;
22
+ return errorMessage;
22
23
  }
23
- function handleDeepKey(input) {
24
- const { results, key, deepKey, data, reqs } = input;
25
- const nesctedKeys = Object.keys(reqs);
24
+ function validateNestedKey(input) {
25
+ const { results, key, nestedKey, data, reqs } = input;
26
+ const nestedKeys = Object.keys(reqs);
26
27
  const nestedResults = [];
27
- let i = 0;
28
- while (i < nesctedKeys.length) {
29
- const nestedKey = nesctedKeys[i];
28
+ for (const itemKey of nestedKeys) {
30
29
  const deepParams = {
31
- key: nestedKey,
30
+ key: itemKey,
32
31
  data: data[key],
33
- reqs: reqs[nestedKey],
34
- deepKey: `${deepKey}.${nestedKey}`
32
+ reqs: reqs[itemKey],
33
+ nestedKey: `${nestedKey}.${itemKey}`
35
34
  };
36
- const deepResults = handleKey.call(this, deepParams);
35
+ const deepResults = validateKey.call(this, deepParams);
37
36
  nestedResults.push(deepResults.ok);
38
37
  results.merge(deepResults);
39
- i++;
40
38
  }
41
- results.fixParentByChilds(deepKey, nestedResults);
39
+ results.fixParentByChilds(nestedKey, nestedResults);
42
40
  return results;
43
41
  }
44
- export function handleKey(input) {
45
- let { results, key, deepKey, data, reqs } = input;
42
+ function validateKey(input) {
43
+ let { results, key, nestedKey, data, reqs } = input;
44
+ if (data === undefined) {
45
+ const noDataResult = new ValidnoResult();
46
+ noDataResult.setNoData();
47
+ noDataResult.finish();
48
+ return noDataResult;
49
+ }
46
50
  if (!results)
47
51
  results = new ValidnoResult();
48
- if (!deepKey)
49
- deepKey = key;
50
- const hasNested = _helpers.checkIsNested(reqs);
52
+ if (!nestedKey)
53
+ nestedKey = key;
51
54
  const hasMissing = _helpers.hasMissing(input);
52
- const missedCheck = [];
53
- const typeChecked = [];
54
- const rulesChecked = [];
55
55
  if (_helpers.checkNestedIsMissing(reqs, data)) {
56
- results.setMissing(deepKey);
57
- return results;
56
+ return handleMissingNestedKey(results, nestedKey);
58
57
  }
59
- if (hasNested) {
60
- return handleDeepKey.call(this, { results, key, data, reqs, deepKey });
58
+ if (_helpers.checkIsNested(reqs)) {
59
+ return validateNestedKey.call(this, { results, key, data, reqs, nestedKey });
61
60
  }
61
+ return validateKeyDetails.call(this, {
62
+ results,
63
+ key,
64
+ nestedKey,
65
+ data,
66
+ reqs,
67
+ hasMissing,
68
+ });
69
+ }
70
+ function handleMissingNestedKey(results, nestedKey) {
71
+ results.setMissing(nestedKey);
72
+ return results;
73
+ }
74
+ function validateKeyDetails(params) {
75
+ const { results, key, nestedKey, data, reqs, hasMissing } = params;
76
+ const missedCheck = [];
77
+ const typeChecked = [];
78
+ const rulesChecked = [];
62
79
  if (hasMissing) {
63
- let errMsg = generateMsg.call(this, input);
64
- missedCheck.push(false);
65
- results.setMissing(deepKey, errMsg);
66
- return results;
80
+ return handleMissingKeyValidation(this.schema, { results, key, nestedKey, data, reqs }, missedCheck);
67
81
  }
68
- const typeCheck = checkType(key, data[key], reqs, deepKey);
69
- typeCheck.forEach(res => {
70
- if (res.passed === false) {
71
- typeChecked.push(res.passed);
72
- results.errors.push(res.details);
82
+ checkValueType(results, key, data[key], reqs, nestedKey, typeChecked);
83
+ checkRulesForKey.call(this, results, nestedKey, data[key], reqs, data, rulesChecked);
84
+ return finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked);
85
+ }
86
+ function handleMissingKeyValidation(schema, params, missedCheck) {
87
+ const { results, key, nestedKey, data, reqs } = params;
88
+ const errMsg = handleMissingKey(schema, { key, nestedKey, data, reqs });
89
+ missedCheck.push(false);
90
+ results.setMissing(nestedKey, errMsg);
91
+ return results;
92
+ }
93
+ function checkValueType(results, key, value, reqs, nestedKey, typeChecked) {
94
+ const typeCheck = checkType(key, value, reqs, nestedKey);
95
+ typeCheck.forEach((res) => {
96
+ if (!res.passed) {
97
+ typeChecked.push(false);
98
+ results.errors.push(res.details || '');
73
99
  }
74
100
  });
75
- const ruleCheck = checkRules.call(this, deepKey, data[key], reqs, data);
101
+ }
102
+ function checkRulesForKey(results, nestedKey, value, reqs, data, rulesChecked) {
103
+ const ruleCheck = checkRules.call(this, nestedKey, value, reqs, data);
76
104
  if (!ruleCheck.ok) {
77
105
  rulesChecked.push(false);
78
106
  ruleCheck.details.forEach((el) => {
79
- if (deepKey in results.errorsByKeys)
80
- results.errorsByKeys[deepKey] = [];
107
+ if (!(nestedKey in results.errorsByKeys))
108
+ results.errorsByKeys[nestedKey] = [];
81
109
  results.errors.push(el);
82
- results.errorsByKeys[deepKey] = ['1'];
110
+ results.errorsByKeys[nestedKey] = ['1'];
83
111
  });
84
112
  }
113
+ }
114
+ function finalizeValidation(results, nestedKey, missedCheck, typeChecked, rulesChecked) {
85
115
  if (missedCheck.length)
86
- results.setMissing(deepKey);
87
- const isPassed = (!typeChecked.length && !rulesChecked.length && !missedCheck.length);
116
+ results.setMissing(nestedKey);
117
+ const isPassed = !typeChecked.length && !rulesChecked.length && !missedCheck.length;
88
118
  if (!isPassed) {
89
- results.setFailed(deepKey);
90
- results.errorsByKeys[deepKey] = [
91
- ...results.errors
92
- ];
119
+ results.setFailed(nestedKey);
120
+ results.errorsByKeys[nestedKey] = [...results.errors];
93
121
  }
94
122
  else {
95
- results.setPassed(deepKey);
123
+ results.setPassed(nestedKey);
96
124
  }
97
125
  return results.finish();
98
126
  }
99
- function validate(schema, data, keysToCheck) {
127
+ function validateSchema(data, keysToCheck) {
100
128
  const output = new ValidnoResult();
101
129
  const hasKeysToCheck = _helpers.areKeysLimited(keysToCheck);
102
- const schemaKeys = Object.entries(schema.schema);
130
+ const schemaKeys = Object.entries(this.schema);
103
131
  for (const [key, reqs] of schemaKeys) {
104
132
  const toBeValidated = _helpers.needValidation(key, hasKeysToCheck, keysToCheck);
105
133
  if (!toBeValidated)
106
134
  continue;
107
- const keyResult = handleKey.call(this, { key, data, reqs });
135
+ const keyResult = validateKey.call(this, { key, data, reqs });
108
136
  output.merge(keyResult);
109
137
  }
110
138
  output.finish();
111
139
  return output;
112
140
  }
113
141
  ;
114
- export default validate;
142
+ export default validateSchema;
@@ -110,7 +110,7 @@ const rulesFunctions = {
110
110
  return output;
111
111
  }
112
112
  };
113
- function checkRules(key, value, requirements, inputObj) {
113
+ function validateRules(key, value, requirements, inputObj) {
114
114
  const result = {
115
115
  ok: true,
116
116
  details: []
@@ -156,4 +156,4 @@ function checkRules(key, value, requirements, inputObj) {
156
156
  return result;
157
157
  }
158
158
  ;
159
- export default checkRules;
159
+ export default validateRules;
@@ -0,0 +1,124 @@
1
+ import { EValidationDetails, EValidationId } from "./constants/details.js";
2
+ import _validations from "./utils/validations.js";
3
+ import _errors from "./utils/errors.js";
4
+ import _validateType from "./utils/validateType.js";
5
+ const validateUnionType = (key, value, requirements, keyName = key) => {
6
+ const typeList = Array.isArray(requirements.type)
7
+ ? requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el))
8
+ : [];
9
+ const results = [];
10
+ for (let i = 0; i < typeList.length; i++) {
11
+ const requirementsRe = Object.assign(Object.assign({}, requirements), { type: requirements.type[i] });
12
+ const result = validateType(key, value, requirementsRe);
13
+ results.push(result[0].passed);
14
+ if (results[i] === true)
15
+ return _validateType.getResult(keyName, true);
16
+ }
17
+ const isPassed = results.some((r) => r === true);
18
+ const result = _validateType.getResult(keyName, isPassed, isPassed ? null : _errors.getErrorDetails(keyName, typeList.join('/'), value));
19
+ return result;
20
+ };
21
+ const validateType = (key, value, requirements, keyName = key) => {
22
+ var _a;
23
+ const isNotNull = value !== null;
24
+ const keyTitle = 'title' in requirements ? requirements.title : keyName;
25
+ const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
26
+ if (value === undefined && requirements.required) {
27
+ return [_validateType.getResult(keyName, false, _errors.getMissingError(keyName))];
28
+ }
29
+ if (Array.isArray(requirements.type)) {
30
+ return [validateUnionType(key, value, requirements)];
31
+ }
32
+ if (value === undefined && requirements.required !== true) {
33
+ return [_validateType.getResult(keyName, true)];
34
+ }
35
+ const customErrDetails = hasCustomMessage ?
36
+ requirements.customMessage({
37
+ keyword: EValidationId.Type,
38
+ value: value,
39
+ key: keyName,
40
+ title: keyTitle,
41
+ reqs: requirements,
42
+ schema: null
43
+ }) :
44
+ null;
45
+ const baseErrDetails = _errors.getErrorDetails(keyName, requirements.type, value);
46
+ const getDetails = (isOK, errorText) => isOK ?
47
+ EValidationDetails.OK :
48
+ errorText || customErrDetails || baseErrDetails;
49
+ const typeBySchema = requirements.type;
50
+ const result = [];
51
+ switch (typeBySchema) {
52
+ case 'any': {
53
+ result.push(_validateType.getResult(keyName, true, getDetails(true)));
54
+ break;
55
+ }
56
+ case Number: {
57
+ const isNumber = isNotNull && value.constructor === Number;
58
+ result.push(_validateType.getResult(keyName, isNumber, getDetails(isNumber)));
59
+ break;
60
+ }
61
+ case String: {
62
+ const isString = isNotNull && value.constructor === String;
63
+ result.push(_validateType.getResult(keyName, isString, getDetails(isString)));
64
+ break;
65
+ }
66
+ case Date: {
67
+ const isDate = isNotNull && value.constructor === Date;
68
+ const isValid = isDate && !isNaN(value.getTime());
69
+ const isValidDate = isDate && isValid;
70
+ result.push(_validateType.getResult(keyName, isValidDate, getDetails(isValidDate, EValidationDetails.INVALID_DATE)));
71
+ break;
72
+ }
73
+ case Boolean: {
74
+ const isBoolean = isNotNull && value.constructor === Boolean;
75
+ result.push(_validateType.getResult(keyName, isBoolean, getDetails(isBoolean)));
76
+ break;
77
+ }
78
+ case Array: {
79
+ const isArray = isNotNull && value.constructor === Array;
80
+ if (!isArray) {
81
+ result.push(_validateType.getResult(keyName, false, getDetails(isArray)));
82
+ break;
83
+ }
84
+ let isEachChecked = { passed: true, details: "" };
85
+ if ('eachType' in requirements) {
86
+ for (const el of value) {
87
+ const result = validateType('each of ' + key, el, { type: requirements.eachType, required: true });
88
+ if (!result[0].passed) {
89
+ isEachChecked.passed = false;
90
+ isEachChecked.details = result[0].details || '';
91
+ break;
92
+ }
93
+ }
94
+ }
95
+ const isOk = isArray && isEachChecked.passed;
96
+ const details = !isEachChecked.passed ? isEachChecked.details : getDetails(isOk);
97
+ result.push(_validateType.getResult(keyName, isOk, details));
98
+ break;
99
+ }
100
+ case Object: {
101
+ const isObject = _validations.isObject(value) && value.constructor === Object;
102
+ result.push(_validateType.getResult(keyName, isObject, getDetails(isObject)));
103
+ break;
104
+ }
105
+ case RegExp: {
106
+ const isRegex = _validations.isRegex(value);
107
+ result.push(_validateType.getResult(keyName, isRegex, getDetails(isRegex)));
108
+ break;
109
+ }
110
+ case null: {
111
+ const isNull = value === null;
112
+ result.push(_validateType.getResult(keyName, isNull, getDetails(isNull)));
113
+ break;
114
+ }
115
+ default: {
116
+ const isInstanceOf = typeof typeBySchema === 'function' && value instanceof typeBySchema;
117
+ const isConstructorSame = typeof typeBySchema === 'function' && ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
118
+ const isOK = isInstanceOf && isConstructorSame;
119
+ result.push(_validateType.getResult(keyName, isOK, getDetails(isOK)));
120
+ }
121
+ }
122
+ return result;
123
+ };
124
+ export default validateType;
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "validno",
3
- "version": "0.2.4",
3
+ "version": "0.2.6",
4
4
  "description": "",
5
5
  "main": "dist/index.js",
6
6
  "scripts": {
package/dist/checkType.js DELETED
@@ -1,163 +0,0 @@
1
- import { ErrorKeywords } from "./constants/details.js";
2
- import _validations from "./utils/validations.js";
3
- import _errors from "./utils/errors.js";
4
- const checkTypeMultiple = (key, value, requirements, keyName = key) => {
5
- const constructorNames = requirements.type.map((el) => String((el === null || el === void 0 ? void 0 : el.name) || el));
6
- const result = {
7
- key: keyName,
8
- passed: false,
9
- details: _errors.getErrorDetails(keyName, constructorNames.join('/'), value)
10
- };
11
- let i = 0;
12
- while (i < requirements.type.length) {
13
- const requirementsRe = Object.assign(Object.assign({}, requirements), { type: requirements.type[i] });
14
- const check = checkType(key, value, requirementsRe);
15
- if (check[0].passed === true) {
16
- result.passed = true;
17
- result.details = 'OK';
18
- return result;
19
- }
20
- i++;
21
- }
22
- return result;
23
- };
24
- const checkType = (key, value, requirements, keyName = key) => {
25
- var _a;
26
- const isNotNull = value !== null;
27
- const keyTitle = 'title' in requirements ? requirements.title : keyName;
28
- const hasCustomMessage = requirements.customMessage && typeof requirements.customMessage === 'function';
29
- if (value === undefined && requirements.required) {
30
- return [{ key: keyName, passed: false, details: `Значение "${keyName}" отсутствует` }];
31
- }
32
- let result = [];
33
- if (Array.isArray(requirements.type)) {
34
- return [checkTypeMultiple(key, value, requirements)];
35
- }
36
- if (value === undefined && requirements.required !== true) {
37
- result.push({
38
- key: keyName,
39
- passed: true,
40
- details: 'OK'
41
- });
42
- return result;
43
- }
44
- const customErrDetails = hasCustomMessage ?
45
- requirements.customMessage({
46
- keyword: ErrorKeywords.Type,
47
- value: value,
48
- key: keyName,
49
- title: keyTitle,
50
- reqs: requirements,
51
- schema: null
52
- }) :
53
- null;
54
- const baseErrDetails = _errors.getErrorDetails(keyName, requirements.type, value);
55
- const getDetails = (isOK) => isOK ? 'OK' : customErrDetails || baseErrDetails;
56
- const typeBySchema = requirements.type;
57
- switch (typeBySchema) {
58
- case 'any':
59
- result.push({
60
- key: keyName,
61
- passed: true,
62
- details: 'OK'
63
- });
64
- break;
65
- case Number:
66
- const isNumber = isNotNull && value.constructor === Number;
67
- result.push({
68
- key: keyName,
69
- passed: isNumber,
70
- details: getDetails(isNumber)
71
- });
72
- break;
73
- case String:
74
- const isString = isNotNull && value.constructor === String;
75
- result.push({
76
- key: keyName,
77
- passed: isString,
78
- details: getDetails(isString)
79
- });
80
- break;
81
- case Date:
82
- const isDate = isNotNull && value.constructor === Date;
83
- const isValid = isDate && !isNaN(value.getTime());
84
- const errorMsg = isValid ? getDetails(isDate) : 'Дата невалидна';
85
- result.push({
86
- key: keyName,
87
- passed: isDate && isValid,
88
- details: isDate && isValid ? 'OK' : errorMsg
89
- });
90
- break;
91
- case Boolean:
92
- const isBoolean = isNotNull && value.constructor === Boolean;
93
- result.push({
94
- key: keyName,
95
- passed: isBoolean,
96
- details: isBoolean ? 'OK' : getDetails(isBoolean)
97
- });
98
- break;
99
- case Array:
100
- const isArray = isNotNull && value.constructor === Array;
101
- if (!isArray) {
102
- result.push({
103
- key: keyName,
104
- passed: false,
105
- details: getDetails(isArray)
106
- });
107
- break;
108
- }
109
- let isEachChecked = { passed: true, details: "" };
110
- if ('eachType' in requirements) {
111
- isEachChecked.passed = value.every((el) => {
112
- const checkResult = checkType('each of ' + key, el, { type: requirements.eachType, required: true });
113
- if (!checkResult[0].passed) {
114
- isEachChecked.details = checkResult[0].details;
115
- isEachChecked.passed = false;
116
- }
117
- return true;
118
- });
119
- }
120
- const isOk = isArray && isEachChecked.passed;
121
- result.push({
122
- key: keyName,
123
- passed: isOk,
124
- details: isOk ? 'OK' : !isEachChecked.passed ? isEachChecked.details : getDetails(isOk)
125
- });
126
- break;
127
- case Object:
128
- const isObject = _validations.isObject(value) && value.constructor === Object;
129
- result.push({
130
- key: keyName,
131
- passed: isObject,
132
- details: isObject ? 'OK' : getDetails(isObject)
133
- });
134
- break;
135
- case RegExp:
136
- const isRegex = _validations.isRegex(value);
137
- result.push({
138
- key: keyName,
139
- passed: isRegex,
140
- details: isRegex ? 'OK' : getDetails(isRegex)
141
- });
142
- break;
143
- case null:
144
- const isNull = value === null;
145
- result.push({
146
- key: keyName,
147
- passed: isNull,
148
- details: isNull ? 'OK' : getDetails(isNull)
149
- });
150
- break;
151
- default:
152
- const isInstanceOf = value instanceof typeBySchema;
153
- const isConstructorSame = ((_a = value.constructor) === null || _a === void 0 ? void 0 : _a.name) === (typeBySchema === null || typeBySchema === void 0 ? void 0 : typeBySchema.name);
154
- const checked = isInstanceOf && isConstructorSame;
155
- result.push({
156
- key: keyName,
157
- passed: checked,
158
- details: getDetails(checked)
159
- });
160
- }
161
- return result;
162
- };
163
- export default checkType;
@@ -1 +0,0 @@
1
- "use strict";