validno 0.1.9 → 0.1.10

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.
@@ -1,111 +0,0 @@
1
- const _validations: {[key: string]: Function} = {};
2
-
3
- _validations.isString = (value: any) => {
4
- return typeof value === 'string';
5
- };
6
-
7
- _validations.isDate = (value: any) => {
8
- return value instanceof Date && String(value) !== 'Invalid Date';
9
- };
10
-
11
- _validations.isNumber = (value: any) => {
12
- return typeof value === 'number';
13
- };
14
-
15
- _validations.isNumberGte = (value: any, gte: number) => {
16
- return typeof value === 'number' && value >= gte;
17
- };
18
-
19
- _validations.isNumberLte = (value: any, lte: number) => {
20
- return typeof value === 'number' && value <= lte;
21
- };
22
-
23
- _validations.isArray = (value: any) => {
24
- return Array.isArray(value);
25
- };
26
-
27
- _validations.isObject = (value: any) => {
28
- return typeof value === 'object' && !Array.isArray(value) && value !== null
29
- };
30
-
31
- _validations.lengthIs = (value: any, length: number) => {
32
- return value.length === length;
33
- };
34
-
35
- _validations.lengthNot = (value: any, length: number) => {
36
- return value.length !== length;
37
- };
38
-
39
- _validations.lengthMin = (value: any, min: number) => {
40
- return value.length >= min;
41
- };
42
-
43
- _validations.lengthMax = (value: any, max: number) => {
44
- return value.length <= max;
45
- };
46
-
47
- _validations.isEmail = (value: string) => {
48
- const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
49
- return emailRegex.test(value);
50
- };
51
-
52
- _validations.isRegex = (value: any) => {
53
- return value instanceof RegExp
54
- }
55
-
56
- _validations.hasKey = (obj: object, key: string) => {
57
- return key in obj;
58
- };
59
-
60
- _validations.isNot = (value: any, not: any) => {
61
- if (typeof not === 'object' && Array.isArray(not)) {
62
- for (let i = 0; i < not.length; i++) {
63
- if (value === not[i]) return false;
64
- }
65
-
66
- return true;
67
- }
68
-
69
- return value !== not;
70
- };
71
-
72
- _validations.is = (value: any, compareTo: any) => {
73
- if (typeof compareTo === 'object' && Array.isArray(compareTo)) {
74
- for (let i = 0; i < compareTo.length; i++) {
75
- if (value === compareTo[i]) return true;
76
- }
77
-
78
- return false;
79
- }
80
-
81
- return value === compareTo;
82
- };
83
-
84
- _validations.isDateYYYYMMDD = (value: string) => {
85
- const regex = /^\d{4}-\d{2}-\d{2}$/;
86
- return regex.test(value);
87
- };
88
-
89
- _validations.regexTested = (value: string, regex: RegExp) =>{
90
- if (!regex) throw new Error('regex argument is not defined');
91
- return regex.test(value);
92
- };
93
-
94
- _validations.isHex = (value: string) => {
95
- const regex = /^#(?:[0-9a-fA-F]{3}){1,2}$/;
96
- return regex.test(value);
97
- };
98
-
99
- _validations.isBoolean = (value: string) => {
100
- return typeof value === 'boolean'
101
- }
102
-
103
- _validations.isNull = (value: string) => {
104
- return value === null
105
- }
106
-
107
- _validations.isUndefined = (value: string) => {
108
- return value === undefined
109
- }
110
-
111
- export default _validations
package/src/validate.ts DELETED
@@ -1,198 +0,0 @@
1
- import checkRules from "./checkRules.js";
2
- import checkType from "./checkType.js";
3
- import _errors from "./utils/errors.js";
4
- import _validations from "./utils/validations.js";
5
- import { defaultSchemaKeys, Schema, TSchemaInput } from "./Schema.js";
6
- import { ErrorKeywords } from "./constants/details.js";
7
-
8
- export type TResult = {
9
- ok: null | boolean,
10
- missed: string[],
11
- failed: string[],
12
- passed: string[],
13
- errors: string[],
14
- byKeys: {[key: string]: boolean},
15
- errorsByKeys: {[key: string]: string[]},
16
- };
17
-
18
- export const getResultDefaults = (): TResult => {
19
- return {
20
- ok: null,
21
- missed: [],
22
- failed: [],
23
- passed: [],
24
- errors: [],
25
- byKeys: {},
26
- errorsByKeys: {},
27
- };
28
- }
29
-
30
- const checkIsNested = (obj: {[key: string]: any}) => {
31
- if (!_validations.isObject(obj)) return false
32
-
33
- const objKeys = Object.keys(obj)
34
-
35
- if (objKeys.every((k: any) => defaultSchemaKeys.includes(k))) {
36
- return false
37
- } else {
38
- return true
39
- }
40
- }
41
-
42
- export const mergeResults = (resultsOld: TResult, resultsNew: TResult) => {
43
- const output = getResultDefaults()
44
-
45
- output.failed = [...resultsOld.failed, ...resultsNew.failed]
46
- output.errors = [...resultsOld.errors, ...resultsNew.errors]
47
- output.missed = [...resultsOld.missed, ...resultsNew.missed]
48
- output.passed = [...resultsOld.passed, ...resultsNew.passed]
49
- output.byKeys = {...resultsOld.byKeys, ...resultsNew.byKeys}
50
- output.errorsByKeys = {...resultsOld.errorsByKeys, ...resultsNew.errorsByKeys}
51
-
52
- return output
53
- }
54
-
55
- export function handleReqKey(this: any, key: string, data: any, reqs: TSchemaInput, deepKey = key) {
56
- let results = getResultDefaults()
57
- const hasNested = checkIsNested(reqs)
58
- const keyTitle = 'title' in reqs ? reqs.title : deepKey
59
-
60
- const missedCheck: boolean[] = [];
61
- const typeChecked: boolean[] = [];
62
- const rulesChecked: boolean[] = [];
63
-
64
- // If nested key is present but no data provided
65
- // @ts-ignore
66
- if (reqs.required && (
67
- data === undefined ||
68
- (_validations.isObject(data) && !Object.keys(data).length))
69
- ) {
70
- results.missed.push(deepKey)
71
- results.failed.push(deepKey)
72
- results.byKeys[deepKey] = false
73
-
74
- return results
75
- }
76
-
77
- // Handle nested keys first
78
- if (hasNested) {
79
- const nestedReqKeys: string[] = Object.keys(reqs)
80
- results.byKeys[deepKey] = true
81
-
82
- let i = 0;
83
- while (i < nestedReqKeys.length) {
84
- const reqKeyI: string = nestedReqKeys[i]
85
-
86
- const deepResults = handleReqKey.call(
87
- this,
88
- reqKeyI,
89
- data[key],
90
- // @ts-ignore
91
- reqs[reqKeyI],
92
- deepKey + '.' + reqKeyI
93
- )
94
-
95
- results = mergeResults(results, deepResults)
96
-
97
- i++
98
- }
99
-
100
- return results
101
- }
102
-
103
- // Check missing keys
104
- // @ts-ignore
105
- if (reqs.required === true && key in data === false || data === undefined) {
106
- let errMsg = _errors.getMissingError(deepKey)
107
-
108
- if (reqs.customMessage && typeof reqs.customMessage === 'function') {
109
- // @ts-ignore
110
- errMsg = reqs.customMessage({
111
- keyword: ErrorKeywords.Missing,
112
- value: data[key],
113
- key: deepKey,
114
- title: keyTitle,
115
- reqs: reqs,
116
- schema: this.schema
117
- })
118
- }
119
-
120
- missedCheck.push(false)
121
- results.missed.push(deepKey)
122
- results.failed.push(deepKey)
123
- results.errors.push(errMsg)
124
- results.byKeys[deepKey] = false
125
-
126
- return results
127
- }
128
-
129
- // Check value type
130
- const typeCheck = checkType(key, data[key], reqs, deepKey);
131
-
132
- typeCheck.forEach(res => {
133
- if (res.passed === false) {
134
- typeChecked.push(res.passed)
135
- results.errors.push(res.details)
136
- }
137
- })
138
-
139
- // Check all rules
140
- // @ts-ignore
141
- const ruleCheck = checkRules.call(this, deepKey, data[key], reqs, data);
142
-
143
- if (!ruleCheck.ok) {
144
- rulesChecked.push(false)
145
- ruleCheck.details.forEach((el) => {
146
- if (deepKey in results.errorsByKeys) results.errorsByKeys[deepKey] = []
147
-
148
- results.errors.push(el)
149
- results.errorsByKeys[deepKey] = ['1']
150
- })
151
- }
152
-
153
- // Combine final result
154
- if (missedCheck.length) results.missed.push(deepKey)
155
-
156
- if (typeChecked.length || rulesChecked.length) {
157
- results.failed.push(deepKey)
158
- } else {
159
- results.passed.push(deepKey)
160
- }
161
-
162
- results.errorsByKeys[deepKey] = [
163
- ...results.errors
164
- ]
165
-
166
- results.byKeys[deepKey] = (
167
- missedCheck.length + typeChecked.length + rulesChecked.length
168
- ) === 0
169
-
170
- return results
171
- }
172
-
173
- const checkIfValidationIsNeeded = (key: string, hasLimits: boolean, onlyKeys?: string | string[]) => {
174
- return !hasLimits || (key === onlyKeys || Array.isArray(onlyKeys) && onlyKeys?.includes(key))
175
- }
176
-
177
- function validate(schema: Schema, data: any, onlyKeys?: string | string[]): TResult {
178
- let results: TResult = getResultDefaults()
179
- const areKeysLimited = (Array.isArray(onlyKeys) && onlyKeys.length > 0) || (typeof onlyKeys === 'string' && onlyKeys.length > 0)
180
-
181
- for (const [key, reqs] of Object.entries(schema.schema)) {
182
- const isValidationRequired = checkIfValidationIsNeeded(key, areKeysLimited, onlyKeys)
183
-
184
- if (isValidationRequired) {
185
- // @ts-ignore
186
- const keyResult = handleReqKey.call(this, key, data, reqs)
187
-
188
- results = mergeResults(results, keyResult)
189
- }
190
- }
191
-
192
- if (results.failed.length) results.ok = false
193
- else results.ok = true
194
-
195
- return results;
196
- };
197
-
198
- export default validate;
@@ -1,131 +0,0 @@
1
- import {describe, expect, test} from '@jest/globals';
2
- import { Schema } from '../dist/Schema.js';
3
- import _errors from '../dist/utils/errors.js';
4
-
5
- describe('Тестирование обработки пропущенных свойста', () => {
6
- test('Отсутствующий ключ правильно отображается в результате', () => {
7
- const missedKey = 'testKey'
8
- const missedKey2 = 'testKey2'
9
-
10
- const okKey = 'okKey'
11
-
12
- const scheme = new Schema({
13
- [missedKey]: {
14
- required: true,
15
- type: String
16
- },
17
- [missedKey2]: {
18
- required: true,
19
- type: String
20
- },
21
- [okKey]: {
22
- required: true,
23
- type: String
24
- }
25
- })
26
-
27
- const obj = {
28
- [okKey]: 'string'
29
- }
30
-
31
- const result = scheme.validate(obj)
32
-
33
- expect(result).toEqual({
34
- ok: false,
35
- failed: [missedKey, missedKey2],
36
- missed: [missedKey, missedKey2],
37
- errors: [
38
- _errors.getMissingError(missedKey),
39
- _errors.getMissingError(missedKey2)
40
- ],
41
- passed: [okKey],
42
- byKeys: {
43
- [missedKey]: false,
44
- [missedKey2]: false,
45
- [okKey]: true
46
- },
47
- errorsByKeys: {
48
- [okKey]: [],
49
- [missedKey]: [_errors.getMissingError(missedKey)],
50
- [missedKey2]: [_errors.getMissingError(missedKey2)]
51
- }
52
- })
53
- })
54
-
55
- test('Присутствующий ключ правильно отображается в результате', () => {
56
- const key = 'testKey'
57
- const key2 = 'testKey2'
58
-
59
- const scheme = new Schema({
60
- [key]: {
61
- required: true,
62
- type: String
63
- },
64
- [key2]: {
65
- required: true,
66
- type: String
67
- }
68
- })
69
-
70
- const obj = {
71
- [key]: 'string',
72
- [key2]: 'string'
73
- }
74
-
75
- const result = scheme.validate(obj)
76
-
77
- expect(result).toEqual({
78
- ok: true,
79
- failed: [],
80
- missed: [],
81
- errors: [],
82
- passed: [key, key2],
83
- byKeys: {
84
- [key]: true,
85
- [key2]: true
86
- },
87
- errorsByKeys: {
88
- [key]: [],
89
- [key2]: []
90
- }
91
- })
92
- })
93
-
94
- test('Отсутствующий необязательный ключ правильно отображается в результах', () => {
95
- const key = 'testKey'
96
- const key2 = 'testKey2'
97
-
98
- const scheme = new Schema({
99
- [key]: {
100
- required: false,
101
- type: String
102
- },
103
- [key2]: {
104
- required: true,
105
- type: String
106
- }
107
- })
108
-
109
- const obj = {
110
- [key2]: 'string'
111
- }
112
-
113
- const result = scheme.validate(obj)
114
-
115
- expect(result).toEqual({
116
- ok: true,
117
- failed: [],
118
- missed: [],
119
- errors: [],
120
- passed: [key, key2],
121
- byKeys: {
122
- [key]: true,
123
- [key2]: true
124
- },
125
- errorsByKeys: {
126
- [key]: [],
127
- [key2]: []
128
- }
129
- })
130
- })
131
- })