Revision 2fa41a54
Added by koszko about 2 years ago
common/sanitize_JSON.js | ||
---|---|---|
31 | 31 |
|
32 | 32 |
function sanitize_unknown(schema, item) |
33 | 33 |
{ |
34 |
console.log(`sanitize_unknown ${JSON.stringify(schema)}`); |
|
35 | 34 |
let error_msg = undefined; |
36 | 35 |
let schema_options = []; |
37 | 36 |
let has_default = false; |
... | ... | |
89 | 88 |
|
90 | 89 |
function sanitize_unknown_no_alternatives(schema, item) |
91 | 90 |
{ |
92 |
console.log(`sanitize_unknown_no_alternatives ${JSON.stringify(schema)}`); |
|
93 | 91 |
for (const [schema_check, item_check, sanitizer, type_name] of checks) { |
94 |
console.log(`checking ${type_name}`); |
|
95 | 92 |
if (schema_check(schema)) { |
96 | 93 |
if (item_check(item)) |
97 | 94 |
return sanitizer(schema, item); |
... | ... | |
205 | 202 |
|
206 | 203 |
function sanitize_array(schema, array) |
207 | 204 |
{ |
208 |
console.log(`sanitize_array ${JSON.stringify(schema)}`); |
|
209 | 205 |
let min_length = 0; |
210 | 206 |
let max_length = Infinity; |
211 | 207 |
let repeat_length = 1; |
... | ... | |
231 | 227 |
} |
232 | 228 |
if (["repeat", "repeatfull"].includes(schema[schema.length - 1])) { |
233 | 229 |
var repeat_directive = schema.pop(); |
234 |
repeat = schema.splice(schema.length - repeat_length); |
|
230 |
var repeat = schema.splice(schema.length - repeat_length);
|
|
235 | 231 |
} else if (schema.length !== array.length) { |
236 |
throw error_message(`does not not have exactly ${schema.length} items`);
|
|
232 |
throw error_message(`does not have exactly ${schema.length} items`); |
|
237 | 233 |
} |
238 | 234 |
|
239 | 235 |
if (repeat_directive === "repeatfull" && |
240 | 236 |
(array.length - schema.length) % repeat_length !== 0) |
241 |
throw error_message(`does not not contain a full number of item group repetitions`);
|
|
237 |
throw error_message(`does not contain a full number of item group repetitions`); |
|
242 | 238 |
|
243 | 239 |
if (array.length < min_length) |
244 | 240 |
throw error_message(`has less than ${min_length} element${min_length === 1 ? "" : "s"}`); |
... | ... | |
246 | 242 |
if (array.length > max_length) |
247 | 243 |
throw error_message(`has more than ${max_length} element${max_length === 1 ? "" : "s"}`); |
248 | 244 |
|
249 |
console.log(schema, repeat); |
|
250 |
|
|
251 | 245 |
for (const item of array) { |
252 | 246 |
if (i >= schema.length) { |
253 | 247 |
i = 0; |
... | ... | |
268 | 262 |
|
269 | 263 |
function sanitize_regex(schema, string) |
270 | 264 |
{ |
271 |
console.log(`sanitize_regex ${schema}`); |
|
272 | 265 |
if (schema.test(string)) |
273 | 266 |
return string; |
274 | 267 |
|
... | ... | |
279 | 272 |
|
280 | 273 |
function sanitize_string(schema, string) |
281 | 274 |
{ |
282 |
console.log(`sanitize_string ${JSON.stringify(schema)}`); |
|
283 | 275 |
const regex = string_spec_regex.exec(schema)[2]; |
284 | 276 |
|
285 | 277 |
if (regex === undefined) |
... | ... | |
290 | 282 |
|
291 | 283 |
function sanitize_object(schema, object) |
292 | 284 |
{ |
293 |
console.log(`sanitize_object ${JSON.stringify(schema)}`); |
|
294 | 285 |
const result = {}; |
295 | 286 |
|
296 | 287 |
for (let [key, entry_schema] of Object.entries(schema)) { |
... | ... | |
304 | 295 |
|
305 | 296 |
function sanitize_object_entry(result, key, entry_schema, object) |
306 | 297 |
{ |
307 |
console.log(`sanitize_object_entry ${JSON.stringify(entry_schema)}`); |
|
308 | 298 |
let optional = false; |
309 | 299 |
let has_default = false; |
310 | 300 |
let _default = undefined; |
... | ... | |
347 | 337 |
|
348 | 338 |
function take_literal(schema, item) |
349 | 339 |
{ |
350 |
console.log(`take_literal ${JSON.stringify(schema)}`); |
|
351 | 340 |
return item; |
352 | 341 |
} |
353 | 342 |
|
... | ... | |
357 | 346 |
*/ |
358 | 347 |
function discard(schema, item) |
359 | 348 |
{ |
360 |
console.log(`discard ${JSON.stringify(schema)}`); |
|
361 | 349 |
return discard; |
362 | 350 |
} |
363 | 351 |
|
Also available in: Unified diff
validate settings on import