281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
|
# File 'lib/obf/validator.rb', line 281
def validate_obf(content, filename, opts={})
setup
add_check('filename', "file name") do
if !filename.match(/\.obf$/)
warn "filename should end with .obf"
end
end
json = nil
add_check('valid_json', "JSON file") do
begin
json = JSON.parse(content)
rescue => e
err "Couldn't parse as JSON", true
end
end
ext = nil
add_check('to_external', "OBF structure") do
begin
External.from_obf(json, {})
ext = JSON.parse(content)
rescue External::StructureError => e
err "Couldn't parse structure: #{e.message}", true
end
end
add_check('format_version', "format version") do
if !ext['format']
err "format attribute is required, set to #{OBF::FORMAT}"
end
version = ext['format'].split(/-/, 3)[-1].to_f
if version > OBF::FORMAT_CURRENT_VERSION
err "format version (#{version}) is invalid, current version is #{OBF::FORMAT_CURRENT_VERSION}"
elsif version < OBF::FORMAT_CURRENT_VERSION
warn "format version (#{version}) is old, consider updating to #{OBF::FORMAT_CURRENT_VERSION}"
end
end
add_check('id', "board ID") do
if !ext['id']
err "id attribute is required"
end
end
add_check('locale', "locale") do
if !ext['locale']
err "locale attribute is required, please set to \"en\" for English"
end
end
add_check('extras', "extra attributes") do
attrs = ['format', 'id', 'locale', 'url', 'data_url', 'name', 'description_html', 'default_layout', 'buttons', 'images', 'sounds', 'grid', 'license']
ext.keys.each do |key|
if !attrs.include?(key) && !key.match(/^ext_/)
warn "#{key} attribute is not defined in the spec, should be prefixed with ext_yourapp_"
end
end
end
add_check('description', "descriptive attributes") do
if !ext['name']
warn "name attribute is strongly recommended"
end
if !ext['description_html']
warn "description_html attribute is recommended"
end
end
add_check('background', "background attribute") do
if ext['background'] && !ext['background'].is_a?(Hash)
err "background attributee must be a hash"
end
end
add_check('buttons', "buttons attribute") do
if !ext['buttons']
err "buttons attribute is required"
elsif !ext['buttons'].is_a?(Array)
err "buttons attribute must be an array"
end
end
add_check('grid', "grid attribute") do
if !ext['grid']
err "grid attribute is required"
elsif !ext['grid'].is_a?(Hash)
err "grid attribute must be a hash"
elsif !ext['grid']['rows'].is_a?(Fixnum) || ext['grid']['rows'] < 1
err "grid.row attribute must be a valid positive number"
elsif !ext['grid']['columns'].is_a?(Fixnum) || ext['grid']['columns'] < 1
err "grid.column attribute must be a valid positive number"
end
if ext['grid']['rows'] > 20
warn "grid.row (#{ext['grid']['rows']}) is probably too large a number for most systems"
end
if ext['grid']['columns'] > 20
warn "grid.column (#{ext['grid']['columns']}) is probably too large a number for most systems"
end
if !ext['grid']['order']
err "grid.order is required"
elsif !ext['grid']['order'].is_a?(Array)
err "grid.order must be an array of arrays"
elsif ext['grid']['order'].length != ext['grid']['rows']
err "grid.order length (#{ext['grid']['order'].length}) must match grid.rows (#{ext['grid']['rows']})"
elsif !ext['grid']['order'].all?{|r| r.is_a?(Array) && r.length == ext['grid']['columns'] }
err "grid.order must contain #{ext['grid']['rows']} arrays each of size #{ext['grid']['columns']}"
end
attrs = ['rows', 'columns', 'order']
ext['grid'].keys.each do |key|
if !attrs.include?(key) && !key.match(/^ext_/)
warn "grid.#{key} attribute is not defined in the spec, should be prefixed with ext_yourapp_"
end
end
end
add_check('grid_ids', "button IDs in grid.order attribute") do
button_ids = []
if ext['buttons'] && ext['buttons'].is_a?(Array)
ext['buttons'].each{|b| button_ids << b['id'] if b.is_a?(Hash) && b['id'] }
end
used_button_ids = []
if ext['grid'] && ext['grid']['order'] && ext['grid']['order'].is_a?(Array)
ext['grid']['order'].each do |row|
if row.is_a?(Array)
row.each do |id|
if id
used_button_ids << id
if !button_ids.include?(id)
err "grid.order references button with id #{id} but no button with that id found in buttons attribute"
end
end
end
end
end
end
warn("board has no buttons defined in the grid") if used_button_ids.length == 0
warn("not all defined buttons were included in the grid order (#{(button_ids - used_button_ids).join(',')})") if (button_ids - used_button_ids).length > 0
end
button_image_ids = []
if ext && ext['buttons'] && ext['buttons'].is_a?(Array)
ext['buttons'].each{|b| button_image_ids << b['image_id'] if b.is_a?(Hash) && b['image_id'] }
end
add_check('images', "images attribute") do
if !ext['images']
err "images attribute is required"
elsif !ext['images'].is_a?(Array)
err "images attribute must be an array"
end
end
if ext && ext['images'] && ext['images'].is_a?(Array)
ext['images'].each_with_index do |image, idx|
add_check("image[#{idx}]", "image at images[#{idx}]") do
if !image.is_a?(Hash)
err "image must be a hash"
elsif !image['id']
err "image.id is required"
elsif !image['width'] || !image['width'].is_a?(Fixnum)
err "image.width must be a valid positive number"
elsif !image['height'] || !image['height'].is_a?(Fixnum)
err "image.height must be a valid positive number"
elsif !image['content_type'] || !image['content_type'].match(/^image\/.+$/)
err "image.content_type must be a valid image mime type"
elsif !image['url'] && !image['data'] && !image['symbol'] && !image['path']
err "image must have data, url, path or symbol attribute defined"
elsif image['data'] && !image['data'].match(/^data:image\/.+;base64,.+$/)
err "image.data must be a valid data URI if defined"
elsif image['symbol'] && !image['symbol'].is_a?(Hash)
err "image.symbol must be a hash if defined"
end
if image['path']
if opts['zipper']
if opts['zipper'].glob(image['path']).length == 0
err "image.path \"#{image['path']}\" not found in .obz package"
end
else
err "image.path should not be defined outside of an .obz package"
end
end
attrs = ['id', 'width', 'height', 'content_type', 'data', 'url', 'symbol', 'path', 'data_url', 'license']
image.keys.each do |key|
if !attrs.include?(key) && !key.match(/^ext_/)
warn "image.#{key} attribute is not defined in the spec, should be prefixed with ext_yourapp_"
end
end
end
end
end
add_check('sounds', "sounds attribute") do
if !ext['sounds']
err "sounds attribute is required"
elsif !ext['sounds'].is_a?(Array)
err "sounds attribute must be an array"
end
end
if ext && ext['sounds'] && ext['sounds'].is_a?(Array)
ext['sounds'].each_with_index do |sound, idx|
add_check("sounds[#{idx}]", "sound at sounds[#{idx}]") do
if !sound.is_a?(Hash)
err "sound must be a hash"
elsif !sound['id']
err "sound.id is required"
elsif !sound['duration'] || !sound['duration'].is_a?(Fixnum)
err "sound.duration must be a valid positive number"
elsif !sound['content_type'] || !sound['content_type'].match(/^audio\/.+$/)
err "sound.content_type must be a valid audio mime type"
elsif !sound['url'] && !sound['data'] && !sound['symbol'] && !sound['path']
err "sound must have data, url, path or symbol attribute defined"
elsif sound['data'] && !sound['data'].match(/^data:audio\/.+;base64,.+$/)
err "sound.data must be a valid data URI if defined"
end
attrs = ['id', 'duration', 'content_type', 'data', 'url', 'path', 'data_url', 'license']
sound.keys.each do |key|
if !attrs.include?(key) && !key.match(/^ext_/)
warn "sound.#{key} attribute is not defined in the spec, should be prefixed with ext_yourapp_"
end
end
end
end
end
if ext && ext['buttons'] && ext['buttons'].is_a?(Array)
ext['buttons'].each_with_index do |button, idx|
add_check("buttons[#{idx}]", "button at buttons[#{idx}]") do
if !button.is_a?(Hash)
err "button must be a hash"
elsif !button['id']
err "button.id is required"
elsif !button['label']
err "button.label is required"
end
['top', 'left', 'width', 'height'].each do |attr|
if button[attr] && ((!button[attr].is_a?(Fixnum) && !button[attr].is_a?(Float)) || button[attr] < 0)
warn "button.#{attr} should be a positive number"
end
end
['background_color', 'border_color'].each do |color|
if button[color]
if !button[color].match(/^\s*rgba?\(\s*\d+\s*,\s*\d+\s*,\s*\d+\s*(,\s*[01]\.?\d*)?\)\s*/)
err "button.#{color} must be a valid rgb or rgba value if defined (\"#{button[color]}\" is invalid)"
end
end
end
if button['hidden'] != nil && button['hidden'] != true && button['hidden'] != false
err "button.hidden must be a boolean if defined"
end
if !button['image_id']
warn "button.image_id is recommended"
end
if button['action'] && !button['action'].match(/^(:|\+)/)
err "button.action must start with either : or + if defined"
end
if button['action'] && !button['action'].is_a?(Array)
err "button.actions must be an array of strings"
end
attrs = ['id', 'label', 'vocalization', 'image_id', 'sound_id', 'hidden', 'background_color', 'border_color', 'action', 'actions', 'load_board', 'top', 'left', 'width', 'height']
button.keys.each do |key|
if !attrs.include?(key) && !key.match(/^ext_/)
warn "button.#{key} attribute is not defined in the spec, should be prefixed with ext_yourapp_"
end
end
if button['load_board'] && button['load_board']['path']
if !opts['zipper']
err "button.load_board.path is set but this isn't a zipped file"
else
json = JSON.parse(opts['zipper'].read(button['load_board']['path'])) rescue nil
if !json || !json['id']
err "button.load_board.path references #{button['load_board']['path']} which isn't found in the zipped file"
end
end
end
end
end
end
return @checks
end
|