2020
2121# Check if JSON exists and is readable
2222if test -z " $json " && test ! -f " $json " ; then
23- echo -e " ${Red} JSON is missing${End} "
23+ echo " ${Red} JSON is missing${End} " >&2
2424 exit 1
2525fi
2626
@@ -106,14 +106,14 @@ get_path() {
106106 gsub("\\["; ".") | gsub("\\]"; "") | split(".") | map(
107107 select(length > 0) | gsub("^\"|\"$";"") | if test("^[0-9]+$") then tonumber else . end
108108 );
109- getpath($path | split_path)'
109+ getpath($path | split_path)' 2> /dev/null
110110}
111111
112112# Function to get remove comments from JSON.
113113# Do not remove comments inside strings.
114114load_json () {
115115 local file=${1:- -}
116- sed -e ' s:^[[:blank:]]*//.*$::g' $file
116+ sed -e ' s:^[[:blank:]]*//.*$::g' $file 2> /dev/null
117117}
118118
119119load_json_schema () {
@@ -165,7 +165,7 @@ get_schema_json() {
165165 curl -s $url | load_json_schema $url -
166166
167167 if [ $? -ne 0 ]; then
168- echo -e " ${Red} Unable to download schema file $schema ${End} " >&2
168+ echo " ${Red} Unable to download schema file $schema ${End} " >&2
169169 exit 1
170170 fi
171171}
@@ -190,7 +190,7 @@ get_schema() {
190190
191191 # if schema is not a valid JSON, return error
192192 if ! is_json <<< " $schema" ; then
193- echo -e " ${Red} Invalid schema${End} " >&2
193+ echo " ${Red} Invalid schema${End} " >&2
194194 exit 1
195195 fi
196196
@@ -255,7 +255,7 @@ validate() {
255255
256256 # if schema is not a valid JSON, return error
257257 if ! is_json <<< " $json" ; then
258- echo -e " ${Red} Invalid json${End} " >&2
258+ echo " ${Red} Invalid json${End} " >&2
259259 exit 1
260260 fi
261261
@@ -286,7 +286,7 @@ validate() {
286286 fi
287287
288288 # Log
289- echo -e " ${lvl}${Blue} Parsing schema <${path:- .} > == <${real:- .} >${None} at level <$level >\r " >&2
289+ echo -e " ${lvl}${Blue} Parsing schema <${path:- .} > == <${real:- .} >${None} at level <$level >${End} " >&2
290290
291291 # get 1st level entry points (oneOf, allOf, anyOf, properties) and loop through them
292292 for entry in ' "$id"' ' "not"' ' "oneOf"' ' "allOf"' ' "anyOf"' ' "type"' ' "required"' ' "$ref"' ' "properties"' ' "items"' ' "additionalProperties"' ; do
@@ -296,7 +296,7 @@ validate() {
296296 continue
297297 fi
298298
299- echo -e " ${lvl} - Processing <${Blue}${path} :$entry ${None} >\r " >&2
299+ echo -e " ${lvl} - Processing <${Blue}${path} :$entry ${None} >${End} " >&2
300300
301301 case $entry in
302302
@@ -318,32 +318,32 @@ validate() {
318318 local ref=$( get_path " $path .$entry " <<< " $schema" )
319319
320320 if test " $ref " == " null" || test -z " $ref " ; then
321- echo -e " ${Red} Reference not found for $path .$entry ${End} " >&2
321+ echo " ${Red} Reference not found for $path .$entry ${End} " >&2
322322 exit 1
323323 fi
324324
325325 echo -e " ${lvl} - Ref is ${Yellow}${ref}${End} " >&2
326- echo -e " ${lvl} From ${Yellow}${id}${None} ...\r " >&2
326+ echo -e " ${lvl} From ${Yellow}${id}${None} ...${End} " >&2
327327
328328 # separate uri before # from fragment after #
329329 local uri=$( echo $ref | awk -F ' #' ' {print $1}' )
330330 local fgt=$( echo $ref | awk -F ' #' ' {print $2}' )
331331
332332 # if ref is a url, download it and use it as schema
333333 if test -n " $( echo $uri | grep -E ' ^http' ) " ; then
334- echo -e " ${lvl} - Loading ${Yellow}${uri}${None} ...\r " >&2
334+ echo -e " ${lvl} - Loading ${Yellow}${uri}${None} ...${End} " >&2
335335 schema=$( get_schema_json $uri )
336336 elif test -n " $uri " ; then
337337 # check if file exists
338338 if test -f " $uri " ; then
339- echo -e " ${lvl} - Loading ${Yellow}${uri}${None} ...\r " >&2
339+ echo -e " ${lvl} - Loading ${Yellow}${uri}${None} ...${End} " >&2
340340 schema=$( load_json_schema $uri )
341341 elif test -n " $id " ; then
342342 # load schema from file
343- echo -e " ${lvl} - Loading ${Yellow} $( dirname $id ) /$uri ${None} ...\r " >&2
343+ echo -e " ${lvl} - Loading ${Yellow} $( dirname $id ) /$uri ${None} ...${End} " >&2
344344 schema=$( get_schema_json $( dirname $id ) /$uri )
345345 else
346- echo -e " ${Red} Unable to resolve reference $ref with $id ${End} " >&2
346+ echo " ${Red} Unable to resolve reference $ref with $id ${End} " >&2
347347 exit 1
348348 fi
349349
@@ -373,7 +373,7 @@ validate() {
373373 \" oneOf\" | \" allOf\" | \" anyOf\" )
374374
375375 if ! is_json_array " $path .$entry " <<< " $schema" ; then
376- echo -e " ${Red} Invalid $entry schema${End} " >&2
376+ echo " ${Red} Invalid $entry schema${End} " >&2
377377 exit 1
378378 fi
379379
@@ -504,20 +504,20 @@ validate() {
504504 props=$( get_keys " $path .$entry " <<< " $schema" | tr " \n" " " )
505505
506506 # Log
507- # echo -e "${lvl}- Properties are ${Purple}$props${End}" >&2
507+ # echo "${lvl}- Properties are ${Purple}$props${End}" >&2
508508
509509 # Loop through properties
510510 for prop in $props ; do
511511
512- # echo -e "${lvl}- Processing property ${Purple}$prop${End}" >&2
512+ # echo "${lvl}- Processing property ${Purple}$prop${End}" >&2
513513 if $not is_existing_path " $real .$prop " <<< " $json" ; then
514514
515515 # Log
516- echo -e " ${lvl} - Processing <${Purple} $prop ${None} >\r " >&2
516+ echo -e " ${lvl} - Processing <${Purple} $prop ${None} >${End} " >&2
517517
518518 # Parse sub schema
519519 if ! validate " $json " " $schema " " $path .$entry .$prop " " $real .$prop " " $path .$entry .$prop " " $not " " $level " ; then
520- echo -e " ${Red} Property $prop is present but invalid${Red} " >&2
520+ echo " ${Red} Property $prop is present but invalid${Red} " >&2
521521 return 1
522522 fi
523523 else
@@ -526,7 +526,7 @@ validate() {
526526 fi
527527 done
528528
529- echo -e " ${real:- .} "
529+ echo " ${real:- .} "
530530 ;;
531531
532532 \" additionalProperties\" )
@@ -539,10 +539,10 @@ validate() {
539539 for prop in $( get_keys " $real " <<< " $json" | sort) ; do
540540
541541 if ! grep -q " $prop " <<< " $props" ; then
542- echo -e " ${lvl} - Adding additional property <${Purple} $real .$prop ${None} >\r " >&2
542+ echo -e " ${lvl} - Adding additional property <${Purple} $real .$prop ${None} >${End} " >&2
543543
544544 # Keep track of validated path
545- echo -e " $real .$prop "
545+ echo " $real .$prop "
546546 fi
547547 done
548548 fi
@@ -564,7 +564,7 @@ if [ -n "$local" ]; then
564564 schema=$local /_$type .schema.json
565565
566566 # log
567- echo -e " ${Yellow} Infering schema from local folder for ${UYellow} $json ${End} " >&2
567+ echo " ${Yellow} Infering schema from local folder for ${UYellow} $json ${End} " >&2
568568fi
569569
570570# Check if schema file exists, and if not and import allowed, download it from schema store
@@ -576,7 +576,7 @@ if [ -n "$import" ] && [ ! -f "$schema" ]; then
576576 schema=$( get_schema_url $search )
577577
578578 # log
579- echo -e " ${Yellow} Infering schema from schema store for ${UYellow} $search ${Yellow} ${schema: +" found!" }${End} " >&2
579+ echo " ${Yellow} Infering schema from schema store for ${UYellow} $search ${Yellow} ${schema: +" found!" }${End} " >&2
580580fi
581581
582582# if schema file does not exist, use fallback schema
@@ -591,17 +591,17 @@ if [ -n "$fallback" ] && [ -z "$schema" ]; then
591591 elif [ -f " $fallback " ]; then
592592 schema=$fallback
593593 else
594- echo -e " ${Red} Fallback schema $fallback not found${End} " >&2
594+ echo " ${Red} Fallback schema $fallback not found${End} " >&2
595595 exit 1
596596 fi
597597
598598 # log
599- echo -e " ${Yellow} Using fallback schema ${UYellow} $schema ${End} " >&2
599+ echo " ${Yellow} Using fallback schema ${UYellow} $schema ${End} " >&2
600600fi
601601
602602# Check if schema is readable
603603if test -z " $schema " ; then
604- echo -e " ${Red} Schema is missing${End} " >&2
604+ echo " ${Red} Schema is missing${End} " >&2
605605 exit 1
606606fi
607607
@@ -611,9 +611,9 @@ schema=$(get_schema "$schema" "" "$allow")
611611# Strip comments from JSON and go through normalization
612612# if parse "$json" "$schema" >/dev/null; then ## Uncomment this line to enable debug output
613613if validate " $json " " $schema " ; then
614- echo -e " ${Green} JSON is valid and normalized${End} " >&2
614+ echo " ${Green} JSON is valid and normalized${End} " >&2
615615else
616- echo -e " ${Red} JSON is empty or invalid${End} " >&2
616+ echo " ${Red} JSON is empty or invalid${End} " >&2
617617 exit 1
618618fi > /tmp/$$ .json
619619
0 commit comments