2020
2121# Check if JSON exists and is readable
2222if test -z " $json " && test ! -f " $json " ; then
23- echo -e " ${Red} JSON is missing${None} "
23+ echo -e " ${Red} JSON is missing${None} \r "
2424 exit 1
2525fi
2626
@@ -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 ${None} " >&2
168+ echo -e " ${Red} Unable to download schema file $schema ${None} \r " >&2
169169 exit 1
170170 fi
171171}
@@ -178,25 +178,25 @@ get_schema() {
178178
179179 # if schema is a file, load it
180180 if test -f " $schema " ; then
181- echo -e " ${lvl}${Green} Loading schema ${UGreen} $schema ${None} " >&2
181+ echo -e " ${lvl}${Green} Loading schema ${UGreen} $schema ${None} \r " >&2
182182 schema=$( load_json_schema $schema )
183183 fi
184184
185185 # if schema is a url, download it
186186 if test -n " $( echo $schema | grep -E ' ^http' ) " ; then
187- echo -e " ${lvl}${Green} Downloading schema ${UGreen} $schema ${None} " >&2
187+ echo -e " ${lvl}${Green} Downloading schema ${UGreen} $schema ${None} \r " >&2
188188 schema=$( get_schema_json $schema )
189189 fi
190190
191191 # if schema is not a valid JSON, return error
192192 if ! is_json <<< " $schema" ; then
193- echo -e " ${Red} Invalid schema${None} " >&2
193+ echo -e " ${Red} Invalid schema${None} \r " >&2
194194 exit 1
195195 fi
196196
197197 # if allow flag is set, allow additional properties at root level
198198 if test -n " $allow " ; then
199- echo -e " ${lvl}${Purple} Additional properties allowed at root level${None} " >&2
199+ echo -e " ${lvl}${Purple} Additional properties allowed at root level${None} \r " >&2
200200 echo $schema | jq ' . + {"additionalProperties": true}'
201201 else
202202 echo $schema
@@ -249,13 +249,13 @@ validate() {
249249
250250 # if json does not start with '{' and is a file, load it
251251 if test -f " $json " ; then
252- echo -e " ${lvl}${Green} Loading json ${UGreen} $json ${None} " >&2
252+ echo -e " ${lvl}${Green} Loading json ${UGreen} $json ${None} \r " >&2
253253 json=$( load_json $json )
254254 fi
255255
256256 # if schema is not a valid JSON, return error
257257 if ! is_json <<< " $json" ; then
258- echo -e " ${Red} Invalid json${None} " >&2
258+ echo -e " ${Red} Invalid json${None} \r " >&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 >" >&2
289+ echo -e " ${lvl}${Blue} Parsing schema <${path:- .} > == <${real:- .} >${None} at level <$level >\r " >&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,20 +296,20 @@ validate() {
296296 continue
297297 fi
298298
299- echo -e " ${lvl} - Processing <${Blue}${path} :$entry ${None} >" >&2
299+ echo -e " ${lvl} - Processing <${Blue}${path} :$entry ${None} >\r " >&2
300300
301301 case $entry in
302302
303303 \"\$ id\" )
304304
305305 # log
306- echo -e " ${lvl} - Processing ${BWhite} \$ id${None} " >&2
306+ echo -e " ${lvl} - Processing ${BWhite} \$ id${None} \r " >&2
307307
308308 # get id from schema
309309 id=$( get_json " $path .$entry " <<< " $schema" || echo " ." )
310310
311311 # log
312- echo -e " ${lvl} - Resolving ${Yellow} $id ${None} " >&2
312+ echo -e " ${lvl} - Resolving ${Yellow} $id ${None} \r " >&2
313313 ;;
314314
315315 \"\$ ref\" )
@@ -318,62 +318,62 @@ 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 ${None} " >&2
321+ echo -e " ${Red} Reference not found for $path .$entry ${None} \r " >&2
322322 exit 1
323323 fi
324324
325- echo -e " ${lvl} - Ref is ${Yellow}${ref}${None} " >&2
326- echo -e " ${lvl} From ${Yellow}${id}${None} ..." >&2
325+ echo -e " ${lvl} - Ref is ${Yellow}${ref}${None} \r " >&2
326+ echo -e " ${lvl} From ${Yellow}${id}${None} ...\r " >&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} ..." >&2
334+ echo -e " ${lvl} - Loading ${Yellow}${uri}${None} ...\r " >&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} ..." >&2
339+ echo -e " ${lvl} - Loading ${Yellow}${uri}${None} ...\r " >&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} ..." >&2
343+ echo -e " ${lvl} - Loading ${Yellow} $( dirname $id ) /$uri ${None} ...\r " >&2
344344 schema=$( get_schema_json $( dirname $id ) /$uri )
345345 else
346- echo -e " ${Red} Unable to resolve reference $ref with $id ${None} " >&2
346+ echo -e " ${Red} Unable to resolve reference $ref with $id ${None} \r " >&2
347347 exit 1
348348 fi
349349
350350 fi
351351
352352 # if schema is not a valid JSON, return error
353353 if ! validate " $json " " $schema " " $( echo $fgt | tr ' /' ' .' ) " " $real " " $path " " $not " " $level " ; then
354- echo -e " ${lvl} - ${Yellow} Reference $ref is invalid${None} " >&2
354+ echo -e " ${lvl} - ${Yellow} Reference $ref is invalid${None} \r " >&2
355355 return 1
356356 fi
357357
358358 ;;
359359
360360 \" not\" )
361- echo -e " ${lvl} - ${Purple} Start Not${None} " >&2
361+ echo -e " ${lvl} - ${Purple} Start Not${None} \r " >&2
362362
363363 # Parse sub schema with not flag
364364 if ! validate " $json " " $schema " " $path .$entry " " $real " " $path .$entry " " !" " $level " ; then
365- echo -e " ${lvl} - ${Yellow} Condition is invalid${None} " >&2
365+ echo -e " ${lvl} - ${Yellow} Condition is invalid${None} \r " >&2
366366 return 1
367367 fi
368368
369- echo -e " ${lvl} - ${Purple} End Not${None} " >&2
369+ echo -e " ${lvl} - ${Purple} End Not${None} \r " >&2
370370
371371 ;;
372372
373373 \" oneOf\" | \" allOf\" | \" anyOf\" )
374374
375375 if ! is_json_array " $path .$entry " <<< " $schema" ; then
376- echo -e " ${Red} Invalid $entry schema${None} " >&2
376+ echo -e " ${Red} Invalid $entry schema${None} \r " >&2
377377 exit 1
378378 fi
379379
@@ -382,7 +382,7 @@ validate() {
382382 local last=$( expr $size - 1)
383383 local count=0
384384
385- echo -e " ${lvl} - ${Cyan} Start $entry loop${None} " >&2
385+ echo -e " ${lvl} - ${Cyan} Start $entry loop${None} \r " >&2
386386
387387 for i in $( seq 0 $last ) ; do
388388 if validate " $json " " $schema " " $path .$entry [$i ]" " $real " " $path .$entry [$i ]" " $not " " $level " ; then
@@ -391,14 +391,14 @@ validate() {
391391 ${entry// \" / } _rule $size $count $i && break
392392 done
393393
394- echo -e " ${lvl} - ${Cyan} End $entry loop ($count /$size )${None} " >&2
394+ echo -e " ${lvl} - ${Cyan} End $entry loop ($count /$size )${None} \r " >&2
395395 ${entry// \" / } _rule $size $count $last || return 1
396396 ;;
397397
398398 \" type\" )
399399
400400 # Log
401- echo -e " ${lvl} - Processing ${BWhite} Type${None} " >&2
401+ echo -e " ${lvl} - Processing ${BWhite} Type${None} \r " >&2
402402
403403 local type_json=$( get_json_type " $real " <<< " $json" )
404404
@@ -420,14 +420,14 @@ validate() {
420420
421421 # If no type was found, return error
422422 if test -z " $type " ; then
423- echo -e " ${lvl} - ${Yellow} Property ${real:- .} is ${type_json} , not of expected type ${type_schema}${None} " >&2
423+ echo -e " ${lvl} - ${Yellow} Property ${real:- .} is ${type_json} , not of expected type ${type_schema}${None} \r " >&2
424424 return 1
425425 fi
426426
427427 echo ${real:- .}
428428
429429 # Log
430- echo -e " ${lvl} - ${Green} Property ${real:- .} is of expected type ${type}${None} " >&2
430+ echo -e " ${lvl} - ${Green} Property ${real:- .} is of expected type ${type}${None} \r " >&2
431431 ;;
432432
433433 \" required\" )
@@ -437,7 +437,7 @@ validate() {
437437
438438 # if type is not an object, do not process
439439 if test " $type " ! = " \" object\" " ; then
440- echo -e " ${lvl} - ${Yellow} Skip required not an object ($type )${None} " >&2
440+ echo -e " ${lvl} - ${Yellow} Skip required not an object ($type )${None} \r " >&2
441441 continue
442442 fi
443443
@@ -448,30 +448,30 @@ validate() {
448448
449449 if is_existing_path " $real .$prop " <<< " $json" ; then
450450 if test -n " $not " ; then
451- echo -e " ${lvl} - ${Yellow} Property $prop is present but not required${None} " >&2
451+ echo -e " ${lvl} - ${Yellow} Property $prop is present but not required${None} \r " >&2
452452 return 1
453453 fi
454454 else
455455 if test -z " $not " ; then
456- echo -e " ${lvl} - ${Yellow} Property $prop is missing but required${None} " >&2
456+ echo -e " ${lvl} - ${Yellow} Property $prop is missing but required${None} \r " >&2
457457 return 1
458458 fi
459459 fi
460460
461- echo -e " ${lvl} - ${Green} Property requirement is valid for $prop ${None} " >&2
461+ echo -e " ${lvl} - ${Green} Property requirement is valid for $prop ${None} \r " >&2
462462 done
463463 ;;
464464
465465 \" items\" )
466466
467467 # If items is not an array, do not process
468468 if test " $type " ! = " \" array\" " ; then
469- echo -e " ${lvl} - ${Yellow} Skip properties not an ($type )${None} " >&2
469+ echo -e " ${lvl} - ${Yellow} Skip properties not an ($type )${None} \r " >&2
470470 continue
471471 fi
472472
473473 # Log
474- echo -e " ${lvl} - Processing ${BWhite} Items${None} " >&2
474+ echo -e " ${lvl} - Processing ${BWhite} Items${None} \r " >&2
475475
476476 # get items schema
477477 items=$( get_path " $path .$entry " <<< " $schema" )
@@ -480,10 +480,10 @@ validate() {
480480 for item in $( get_keys " $real " <<< " $json" | sort) ; do
481481
482482 # Log
483- echo -e " ${lvl} - Processing item ${Purple} $item ${None} " >&2
483+ echo -e " ${lvl} - Processing item ${Purple} $item ${None} \r " >&2
484484
485485 if ! validate " $json " " $schema " " $path .$entry " " $real .$item " " $path .$entry " " $not " " $level " ; then
486- echo -e " ${lvl} - ${Orange} Item $item is invalid${None} " >&2
486+ echo -e " ${lvl} - ${Orange} Item $item is invalid${None} \r " >&2
487487 return 1
488488 fi
489489 done
@@ -493,12 +493,12 @@ validate() {
493493
494494 # If items is not an object, do not process
495495 if test " $type " ! = " \" object\" " ; then
496- echo -e " ${lvl} - ${Yellow} Skip properties not an object ($type )${None} " >&2
496+ echo -e " ${lvl} - ${Yellow} Skip properties not an object ($type )${None} \r " >&2
497497 continue
498498 fi
499499
500500 # Log
501- echo -e " ${lvl} - Processing ${BWhite} Properties${None} " >&2
501+ echo -e " ${lvl} - Processing ${BWhite} Properties${None} \r " >&2
502502
503503 # get properties list
504504 props=$( get_keys " $path .$entry " <<< " $schema" | tr " \n" " " )
@@ -509,11 +509,11 @@ validate() {
509509 # Loop through properties
510510 for prop in $props ; do
511511
512- # echo -e "${lvl}- Processing property ${Purple}$prop${None}" >&2
512+ # echo -e "${lvl}- Processing property ${Purple}$prop${None}\r " >&2
513513 if $not is_existing_path " $real .$prop " <<< " $json" ; then
514514
515515 # Log
516- echo -e " ${lvl} - Processing <${Purple} $prop ${None} >" >&2
516+ echo -e " ${lvl} - Processing <${Purple} $prop ${None} >\r " >&2
517517
518518 # Parse sub schema
519519 if ! validate " $json " " $schema " " $path .$entry .$prop " " $real .$prop " " $path .$entry .$prop " " $not " " $level " ; then
@@ -522,7 +522,7 @@ validate() {
522522 fi
523523 else
524524 # Log
525- echo -e " ${lvl} - ${Yellow} Skip property $real .$prop not present${None} " >&2
525+ echo -e " ${lvl} - ${Yellow} Skip property $real .$prop not present${None} \r " >&2
526526 fi
527527 done
528528
@@ -531,15 +531,15 @@ validate() {
531531
532532 \" additionalProperties\" )
533533
534- echo -e " ${lvl} - Processing ${BWhite} Additional properties${None} " >&2
534+ echo -e " ${lvl} - Processing ${BWhite} Additional properties${None} \r " >&2
535535 if is_existing_path " $path .$entry " <<< " $schema" ; then
536536
537- echo -e " ${lvl} - ${Purple} Additional properties allowed for ${real:- .}${None} " >&2
537+ echo -e " ${lvl} - ${Purple} Additional properties allowed for ${real:- .}${None} \r " >&2
538538
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} >" >&2
542+ echo -e " ${lvl} - Adding additional property <${Purple} $real .$prop ${None} >\r " >&2
543543
544544 # Keep track of validated path
545545 echo " $real .$prop "
@@ -551,7 +551,7 @@ validate() {
551551 done
552552
553553 # Log
554- echo -e " ${lvl}${Blue} End Parsing, all valid!${None} " >&2
554+ echo -e " ${lvl}${Blue} End Parsing, all valid!${None} \r " >&2
555555}
556556
557557# if local flag is set, get schema from json file name
@@ -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 ${None} " >&2
567+ echo -e " ${Yellow} Infering schema from local folder for ${UYellow} $json ${None} \r " >&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!" }${None} " >&2
579+ echo -e " ${Yellow} Infering schema from schema store for ${UYellow} $search ${Yellow} ${schema: +" found!" }${None} \r " >&2
580580fi
581581
582582# if schema file does not exist, use fallback schema
@@ -596,12 +596,12 @@ if [ -n "$fallback" ] && [ -z "$schema" ]; then
596596 fi
597597
598598 # log
599- echo -e " ${Yellow} Using fallback schema ${UYellow} $schema ${None} " >&2
599+ echo -e " ${Yellow} Using fallback schema ${UYellow} $schema ${None} \r " >&2
600600fi
601601
602602# Check if schema is readable
603603if test -z " $schema " ; then
604- echo -e " ${Red} Schema is missing${None} " >&2
604+ echo -e " ${Red} Schema is missing${None} \r " >&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${None} " >&2
614+ echo -e " ${Green} JSON is valid and normalized${None} \r " >&2
615615else
616- echo -e " ${Red} JSON is empty or invalid${None} " >&2
616+ echo -e " ${Red} JSON is empty or invalid${None} \r " >&2
617617 exit 1
618618fi > /tmp/$$ .json
619619
0 commit comments