-
Notifications
You must be signed in to change notification settings - Fork 0
/
json-validator.js
304 lines (279 loc) · 8.52 KB
/
json-validator.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*
* The place to put validator functions
*
*/
import { getType } from '@/scripts/util/helpers.js'
/*
* Checks whether inputData complies with validator
*
* Format validator example:
*
* Types are based on function getType in helpers.js
*
* There is the option to validate by type, using string, or array, or validate by value
*
* examples:
*
*
* [1] basic example (Employee record)
*
* inputData:
* {
* name: 'John Doe',
* age: 32,
* adress: {
* country: 'USA',
* city: 'Texas',
* postal: '7148'
* },
* salary: 5000
* }
*
* validator:
* {
* name: 'string',
* age: 'number'
* adress: {
* country: 'string',
* city: 'string',
* postal: 'string'
* },
* salary: 'number'
* }
*
* output: true (valid)
*
* reason: it is valid because all of the types match, and all of the keys were supplied.
* if inputData didn't have key name for example, the output would be false '
*
* or if the postal code was of type number, the output would be false as well
*
*
* [2] example with multiple types and optional
*
* inputData: {
* product_name: 'Jack Daniel\'s',
* price: 17.79,
* info: 'Nice alcohol...',
* code: '0123456789',
* contents: 350,
* alcohol: 40,
* country: 'United States',
* taste: 'Flexible & Friendly'
* }
*
* validator: {
* product_name: 'string',
* price: 'number',
* info: 'undefined|string',
* code: 'number|string',
* contents: 'number|string',
* alcohol: 'number',
* country: 'string',
* type: 'undefined|string',
* taste: 'undefined|string'
* }
*
* valid: true,
*
* reason: It is valid because keys can be removed if the validator has the type undefined as an option.
* also some keys allow for a number or string, as it could be converted. or done some more elaborate processing.
*
*
* [3] example with multiple complex types (Arrays data, arrays types, subobjects)
*
* inputData: {
* userName: 'xxx_CyberDoomEvan_xxx',
* paymentMethods: [
* {
* method: 'PayPal',
* 'e-mail': '[email protected]'
* }
* ],
* products: [
* {
* type: 'Game',
* title: 'GTA V',
* product_code: '2424',
* purchase_data: '02-07-2017',
* install_data: '02-08-2017',
* playtime: 2052384,
* regkey: '245-acd-593-5dfc',
* fileSize: '51.05GB',
* achievements: [
* {
* date: '03-02-2017',
* id: 24
* }
* ],
* userFeedback: {
* rating: 4,
* }
* },
* {
* type: 'user_app',
* title: 'Unreal Tournament 99',
* executable_location: 'c:/program files/ut99/System/Unreal Tournament.exe',
* thumb: 'c:/program files/ut99/System/Unreal Tournament.ico',
* added_date: '01-30-2015',
* playtime: 19942,
* },
* ]
* }
*
* validator: {
* userName: 'string',
* paymentMethods: [[/each/, {
* method: '"paypal"',
* 'e-mail': 'string'
* },
* {
* method: '"card"',
* code: 'string'
* }]],
* products: [[/each/, {
* type: ['"Game"', '"software"'],
* title: 'string',
* product_code: 'number|string',
* purchase_data: 'string',
* install_data: 'string',
* playtime: 'number',
* regkey: 'string',
* fileSize: 'string',
* achievements: [/each/, {
* date: 'string',
* id: 'number'
* }]],
* userFeedback: {
* rating: 'number',
* comment: 'undefined|string'
* },
* }, {
* type: '"user_app"',
* title: 'undefined|string',
* executable_location: 'string',
* thumb: 'undefined|string',
* added_date: 'string',
* playtime: 'number',
* }]
* }
*
* output: true
*
* reason:
*
*
*
* [4] array type
*
* inputData: [
* {
* name: 'Google',
* location: 'Mountain View, United states',
* tel: '+0031-1234567',
* sector: 'Education'
* },
* {
* name: 'Google',
* sector: 'Tech'
* }
* ]
*
* validator: [[/each/, {
* name: 'string',
* location: 'string',
* tel: 'string',
* sector: 'string'
* }]]
*
* output: false
*
* reason: the first item in the array is valid, but the second item isn't because it's missing the location and tel fields
* ----------------------------------------------------
Value properties / possibilities
- 'number'
- 'string'
- 'boolean'
- 'arrlike'
- 'undefined'
See getType for all possibilities...
Cannot user RegExp because it is used for /each/ syntax for array thingy
-------------
This function works by looping through the keys and such.
*/
export function JSONCheck(inputData, validator) {
let validatorType = getType(validator)
const inputType = getType(inputData)
// turn validators like string|number into -> ['string', 'number']
{
if(validatorType == 'string' && validator.indexOf('|') != -1) {
validator = validator.split('|')
validatorType = getType(validator)
}
}
if(validatorType == 'objectlike') {
if(inputType != 'objectlike') {return false}
for(var k in validator) {
const valid = JSONCheck(inputData[k], validator[k])
if(valid == false) {
return false
}
}
return true
} else
if(validatorType == 'arraylike') {
if(inputType == 'arraylike') {
var valid = validator.map(validatorItemArr => {
// Each case
if(
getType(validatorItemArr[0]) == 'RegExp' &&
validatorItemArr[0].toString() === '/each/')
{
const validations = validatorItemArr.slice(1)
const mapRes = inputData.map(inputDataItem => {
return validations
.map(validationItem => JSONCheck(inputDataItem, validationItem))
.filter(valid => valid == true)
.length > 0
})
return mapRes.filter(item => item == false).length == 0
}
// The not each case
const validArr = []
const validatorItemArrType = getType(validatorItemArr)
if(validatorItemArrType == 'string') {
return JSONCheck(inputData, validatorItemArr)
}
// depth 1
for(let i = 0; i < validatorItemArr.length; i++) {
// depth 2
validArr[i] = JSONCheck(inputData[i], validatorItemArr[i])
}
// if more than 0 items are false, return false else true
return validArr.filter(item => item == false).length == 0
})
// at least 1 true
return valid.filter(validItem => validItem == true).length > 0
}
return validator.map(validatorItem => JSONCheck(inputData, validatorItem))
.filter(bool => bool == true)
.length > 0
} else {
// type based compare
if(validatorType == 'string') {
if(validator.indexOf('"') == '-1') {
if(inputType === validator) {
return true
} else {
return false
}
}
}
// value based compare
let validatorVal = validator
if(validatorType == 'string') {
validatorVal = validator.split('"').join('')
}
return inputData === validatorVal
}
}