Project

General

Profile

« Previous | Next » 

Revision 2fa41a54

Added by koszko about 2 years ago

validate settings on import

View differences:

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

  
html/options_main.js
12 12
 * IMPORT TYPE_NAME
13 13
 * IMPORT list_prefixes
14 14
 * IMPORT nice_name
15
 * IMPORT parse_json_with_schema
15 16
 * IMPORTS_END
16 17
 */
17 18

  
......
223 224
    ul.work_li.insertBefore(bag_components_ul, old_components_ul);
224 225
    ul.work_li.removeChild(old_components_ul);
225 226

  
227
    console.log("bag components", components);
226 228
    add_bag_components(components);
227 229
}
228 230

  
......
543 545
		       _read_file(file, resolve, reject));
544 546
}
545 547

  
548
const url_regex = /^[a-z0-9]+:\/\/[^/]+\.[^/]{2}(\/[^?#]*)?$/;
549
const sha256_regex = /^[0-9a-f]{64}$/;
550
const component_schema = [
551
    new RegExp(`^[${TYPE_PREFIX.SCRIPT}${TYPE_PREFIX.BAG}]$`),
552
    /.+/
553
];
554

  
555
const settings_schema = [
556
    [{}, "matchentry", "minentries", 1,
557
     new RegExp(`^${TYPE_PREFIX.SCRIPT}`), {
558
	 /* script data */
559
	 "url":    ["optional", url_regex],
560
	 "sha256": ["optional", sha256_regex],
561
	 "text":   ["optional", "string"]
562
     },
563
     new RegExp(`^${TYPE_PREFIX.BAG}`), [
564
	 "optional",
565
	 [component_schema, "repeat"],
566
	 "default", undefined
567
     ],
568
     new RegExp(`^${TYPE_PREFIX.PAGE}`), {
569
	 /* page data */
570
	 "components": ["optional", component_schema]
571
     }], "repeat"
572
]
573

  
546 574
async function import_from_file(event)
547 575
{
548 576
    let files = event.target.files;
......
555 583
    let result = undefined;
556 584

  
557 585
    try {
558
	result = JSON.parse(await read_file(files[0]));
586
	result = parse_json_with_schema(settings_schema,
587
					await read_file(files[0]));
559 588
    } catch(e) {
560 589
	bad_file_errormsg.textContent = "" + e;
561 590
	import_failed_radio.checked = true;
562 591
	return;
563 592
    }
564 593

  
565
    let errormsg = validate_settings(result);
566
    if (errormsg !== false) {
567
	bad_file_errormsg.textContent = errormsg;
568
	import_failed_radio.checked = true;
569
	return;
570
    }
571

  
572 594
    populate_import_list(result);
573 595
    import_selection_radio.checked = true;
574 596
}
575 597

  
576
function validate_settings(settings)
577
{
578
    // TODO
579
    return false;
580
}
581

  
582 598
function import_li_id(prefix, item)
583 599
{
584 600
    return `ili_${prefix}_${item}`;

Also available in: Unified diff