@@ -8,11 +8,12 @@ eval $(
88 zz_args " Validate JSON according to schema" $0 " $@ " << -help
99 a - allow allow additional properties at root level
1010 d - debug debug output
11+ c - cache allow caching
1112 f fallback fallback fallback schema to use if none found locally
1213 l local local infer schema in <local> folder from json file name (x.y.json => <local>/y.schema.json). Use "true" to use script folder
1314 i - import infer on schema store if nothing found locally (x.y.json => "y" on schema store)
14- - json json json to normalize
15- + schema schema schema to use for normalization
15+ s schema schema schema to use to validate json
16+ - json json json to validate
1617help
1718)
1819
@@ -455,43 +456,41 @@ validate() {
455456}
456457
457458# if local flag is set, get schema from json file name
458- if [ -n " $local " ]; then
459+ if [ -n " $local " ] && [ -z " $schema " ] ; then
459460
460461 # Identify package type from file name just before json extension
461462 type=$( basename -s .json $json | sed -E ' s/.*\.(.*)/\1/' )
462463
463464 if [ " $local " == " true" ]; then
464- local=$( dirname $0 )
465+ local=$( dirname $( readlink -f $0 ) )
465466 fi
466467
467- # Identify schema file
468- schema=$local /_$type .schema.json
468+ # Check if schema file exists
469+ if [ -f " $local /_$type .schema.json" ]; then
470+ schema=$local /_$type .schema.json
471+ fi
469472
470473 # log
471474 zz_log i " Infering schema from local folder {U $local } for {UYellow $json }"
472475fi
473476
474477# Check if schema file exists, and if not and import allowed, download it from schema store
475- if [ -n " $import " ] && [ ! -f " $schema " ]; then
478+ if [ -n " $import " ] && [ -z " $schema " ]; then
476479
477480 search=$( basename -s .json $json | sed -E ' s/.*\.(.*)/\1/' ) .json
478481
479482 # find schema file url from schema store catalog
480483 schema=$( get_schema_url $search )
481484
482485 # log
483- zz_log i " Infering schema from schema store for {U $search } ${schema: +" found!" } "
486+ zz_log i " Infering schema from schema store for {UYellow $search } ${schema: +" found!" } "
484487fi
485488
486489# if schema file does not exist, use fallback schema
487490if [ -n " $fallback " ] && [ -z " $schema " ]; then
488491
489- if [ " $fallback " == " true" ]; then
490- if [ -n " $local " ]; then
491- schema=$( readlink -f $local ) /_default.schema.json
492- else
493- schema=$( dirname $0 ) /_default.schema.json
494- fi
492+ if [ " $fallback " == " local" ] && [ -n " $local " ]; then
493+ schema=$( readlink -f $local ) /_default.schema.json
495494 elif [ -f " $fallback " ]; then
496495 schema=$fallback
497496 else
@@ -502,14 +501,14 @@ if [ -n "$fallback" ] && [ -z "$schema" ]; then
502501 zz_log w " Using fallback schema {UYellow $schema }"
503502fi
504503
504+ # Download schema file and add id to it if not present
505+ schema=$( zz_json -s " $schema " )
506+
505507# Check if schema is readable
506508if test -z " $schema " ; then
507509 zz_log e " Schema is missing" && exit 1
508510fi
509511
510- # Download schema file and add id to it if not present
511- schema=$( zz_json -s " $schema " )
512-
513512# if schema is not a valid JSON, return error
514513if ! is_json <<< " $schema" ; then
515514 zz_log e " Invalid schema" && exit 1
@@ -521,9 +520,25 @@ if test -n "$allow"; then
521520 schema=$( echo " $schema " | jq ' . + {"additionalProperties": true}' -)
522521fi
523522
524- # Validate JSON according to schema and display valid json paths
525- if validate " $json " " $schema " ; then
526- zz_log s " File {U $json } valid"
523+ # is cache flag is set, cache schema
524+ hash=$(
525+ (
526+ jq ' paths | map(tostring) | join(".")' $json
527+ echo " $schema " | jq ' paths | map(tostring) | join(".")'
528+ ) | sort -u | md5sum | awk ' {print $1}'
529+ )
530+ map=~ /.cache/$hash .schema.map
531+ zz_log i " Hash is {B $hash }"
532+
533+ if test -n " $cache " && test -s $map ; then
534+ zz_log i " Using cached validation map"
535+ cat $map
527536else
528- zz_log e " File {U $json } empty or invalid" && exit 1
529- fi | sed -n -e ' s/^.//g' -e ' /^$/d' -e ' G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P'
537+
538+ # Validate JSON according to schema and display valid json paths
539+ if validate " $json " " $schema " ; then
540+ zz_log s " File {U $json } valid"
541+ else
542+ zz_log e " File {U $json } empty or invalid" && exit 1
543+ fi | sed -n -e ' s/^.//g' -e ' /^$/d' -e ' G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | tee $map
544+ fi
0 commit comments