forked from Faithful-Resource-Pack/Web-App
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathvalidator.js
More file actions
170 lines (153 loc) · 5.32 KB
/
validator.js
File metadata and controls
170 lines (153 loc) · 5.32 KB
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
/**
* @typedef {Object} LengthOption
* @property {Number?} min Minimum value or 0
* @property {Number?} max Maximum value
*/
/**
* @typedef {Object} FieldObject
* @property {String?} name name of the field to check
* @property {"string" | "integer" | "array"} type Type verifications available
* @property {String|LengthOption?} length String or array length verification
* @property {Function?} validator Validation value function
* @property {Schema} Children array children validation
*/
/**
* @typedef {Array<FieldObject>} Schema
*/
function useSchema(editions, versions) {
return [{
name: 'name',
type: 'string',
length: { min: 1 }
}, {
name: 'editions',
type: 'array',
length: { min: 1, max: 1 },
validator: function (array) {
array.forEach(edi => {
if (!editions.includes(edi)) throw new Error(`Unknown edition "${edi}" in ${JSON.stringify(array)}. Only accepts ${JSON.stringify(editions)}`)
})
}
}, {
name: 'paths',
type: 'array',
validator: function (array, parent) {
single(array, {
type: 'array',
length: 2
}, parent)
array.forEach(pair => {
single(pair[0], {
type: 'string',
length: { min: 1 }
}, pair)
single(pair[1], {
type: 'array',
length: { min: 1 }
}, pair)
pair[1].forEach(ver => {
if (!versions.includes(ver)) throw new Error(`Unknown version "${pair[1]}" in ${JSON.stringify(pair)}. Only accepts ${JSON.stringify(versions)}`)
})
})
}
}]
}
function textureSchema(types, editions, versions) {
return {
type: 'array',
children: [{
name: 'name',
type: 'string',
length: { min: 1 }
}, {
name: 'type',
type: 'array',
length: { min: 1 },
validator: function (array) {
array.forEach(type => {
if (!types.includes(type)) throw new Error('Unknown type "' + type + '". Only accepts ' + JSON.stringify(types))
})
},
}, {
name: 'uses',
type: 'array',
children: useSchema(editions, versions)
}]
}
}
/**
* Verify single value
* @param {*} value Value to verify
* @param {FieldObject} field Field object to verify
* @param {*?} parent Parent object, helps for error detection
*/
async function single(value, field, parent = undefined) {
const parentError = parent !== undefined ? ` in ${JSON.stringify(parent)}` : ''
field.type = (field.type || '').toLowerCase()
// type
switch (field.type) {
case 'string':
if (typeof (value) !== 'string') throw new Error(`${field.name || JSON.stringify(value)} type is not ${field.type}${parentError}`)
break
case 'integer':
case 'float':
case 'number':
if (typeof (value) !== 'number') throw new Error(`${field.name || JSON.stringify(value)} type is not ${field.type}${parentError}`)
break
case 'array':
if (!Array.isArray(value)) throw new Error(`${field.name || JSON.stringify(value)} type is not ${field.type}${parentError}`)
break
case 'object':
if (typeof (value) !== 'object') throw new Error(`${field.name || JSON.stringify(value)} type is not ${field.type}${parentError}`)
break
case 'boolean':
if (typeof (value) !== 'boolean') throw new Error(`${field.name || JSON.stringify(value)} type is not ${field.type}${parentError}`)
break
default:
throw new Error(`Unsupported field type for ${field.name || 'single'}${parentError}`)
}
// length
if ('length' in field && ['string', 'array'].includes(field.type)) {
const extremum = typeof (field.length) === 'string' ? { min: field.length, max: field.length } : Object.assign({}, { min: 0 }, field.length)
if (value.length < extremum.min) throw new Error(`${field.name || field.type} length lower than ${extremum.min}${parentError}`)
if ('max' in extremum && value.length > extremum.max) throw new Error(`${field.name || field.type} length greater than ${extremum.max}${parentError}`)
}
// validator
if ('validator' in field) {
if (typeof field.validator !== 'function') throw new Error(`${field.name} validator is not a function in schema ${JSON.stringify(field)}`)
await field.validator(value, parent)
}
// children
if (field.type === 'array' && 'children' in field) {
for (let c_i = 0; c_i < value.length; ++c_i) {
await validator(value[c_i], field.children)
}
}
}
/**
* Verify a complex object
* @param {Object} obj Object to verify
* @param {Schema} schema Schema array with fields to verify
*/
async function validator(obj, schema) {
if (!Array.isArray(schema)) throw new Error(`Incorrect schema ${JSON.stringify(schema)} for obj ${JSON.stringify(obj)}`)
for (let i = 0; i < schema.length; ++i) {
const field = schema[i]
// presence
if (!(field.name in obj) || obj[field.name] === undefined || obj[field.name] === null) throw new Error(`Missing field ${field.name} in ${JSON.stringify(obj)}`)
const value = obj[field.name]
await single(value, field, obj)
}
}
try {
if (module) {
module.exports = {
useSchema,
textureSchema,
single,
validator
}
}
} catch (_error) {
// normal browser
}