@@ -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,7 +456,7 @@ 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/' )
@@ -472,7 +473,7 @@ if [ -n "$local" ]; then
472473fi
473474
474475# Check if schema file exists, and if not and import allowed, download it from schema store
475- if [ -n " $import " ] && [ ! -f " $schema " ]; then
476+ if [ -n " $import " ] && [ -z " $schema " ]; then
476477
477478 search=$( basename -s .json $json | sed -E ' s/.*\.(.*)/\1/' ) .json
478479
@@ -521,9 +522,19 @@ if test -n "$allow"; then
521522 schema=$( echo " $schema " | jq ' . + {"additionalProperties": true}' -)
522523fi
523524
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"
525+ # is cache flag is set, cache schema
526+ hash=$( echo " $schema$json " | md5sum | awk ' {print $1}' )
527+ map=~ /.cache/$hash .schema.map
528+ zz_log i " Hash is {B $hash }"
529+
530+ if test -n " $cache " && test -s $map ; then
531+ zz_log i " Using cached validation map"
532+ cat $map
527533else
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'
534+ # Validate JSON according to schema and display valid json paths
535+ if validate " $json " " $schema " ; then
536+ zz_log s " File {U $json } valid"
537+ else
538+ zz_log e " File {U $json } empty or invalid" && exit 1
539+ fi | sed -n -e ' s/^.//g' -e ' /^$/d' -e ' G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | tee $map
540+ fi
0 commit comments