@@ -60,21 +60,6 @@ is_true() {
6060 get_path " $path " -r | jq -e ' if . == true then . else null end' > /dev/null
6161}
6262
63- # Function to check if root path and additonal properties are allowed
64- is_allowed () {
65- local path=${1:- .}
66-
67- if test " $path " == " ." ; then
68- if test -n " $allow " ; then
69- echo -e " ${lvl} - ${Purple} Additional properties allowed at root level${None} " >&2
70- return 0
71- else
72- echo -e " ${lvl} - ${Yellow} Additional properties not allowed at root level${None} " >&2
73- return 1
74- fi
75- fi
76- }
77-
7863# Function to get the type of JSON element
7964get_json_array () {
8065 local path=${1:- .}
@@ -185,6 +170,39 @@ get_schema_json() {
185170 fi
186171}
187172
173+ # Function to get the schema file
174+ get_schema () {
175+ local schema=$1
176+ local lvl=$2
177+ local allow=$3
178+
179+ # if schema is a file, load it
180+ if test -f " $schema " ; then
181+ echo -e " ${lvl}${Green} Loading schema ${UGreen} $schema ${None} " >&2
182+ schema=$( load_json_schema $schema )
183+ fi
184+
185+ # if schema is a url, download it
186+ if test -n " $( echo $schema | grep -E ' ^http' ) " ; then
187+ echo -e " ${lvl}${Green} Downloading schema ${UGreen} $schema ${None} " >&2
188+ schema=$( get_schema_json $schema )
189+ fi
190+
191+ # if schema is not a valid JSON, return error
192+ if ! is_json <<< " $schema" ; then
193+ echo -e " ${Red} Invalid schema${None} " >&2
194+ exit 1
195+ fi
196+
197+ # if allow flag is set, allow additional properties at root level
198+ if test -n " $allow " ; then
199+ echo -e " ${lvl}${Purple} Additional properties allowed at root level${None} " >&2
200+ echo $schema | jq ' . + {"additionalProperties": true}'
201+ else
202+ echo $schema
203+ fi
204+ }
205+
188206traverse () {
189207 local json=$1
190208 local list=${2:- -}
@@ -241,23 +259,8 @@ validate() {
241259 exit 1
242260 fi
243261
244- # if schema is a file, load it
245- if test -f " $schema " ; then
246- echo -e " ${lvl}${Green} Loading schema ${UGreen} $schema ${None} " >&2
247- schema=$( load_json_schema $schema )
248- fi
249-
250- # if schema is a url, download it
251- if test -n " $( echo $schema | grep -E ' ^http' ) " ; then
252- echo -e " ${lvl}${Green} Downloading schema ${UGreen} $schema ${None} " >&2
253- schema=$( get_schema_json $schema )
254- fi
255-
256- # if schema is not a valid JSON, return error
257- if ! is_json <<< " $schema" ; then
258- echo -e " ${Red} Invalid schema${None} " >&2
259- exit 1
260- fi
262+ # load schema
263+ schema=$( get_schema " $schema " " $lvl " )
261264
262265 # Local Variables
263266 local entry=" "
@@ -529,11 +532,12 @@ validate() {
529532 \" additionalProperties\" )
530533
531534 echo -e " ${lvl} - Processing ${BWhite} Additional properties${None} " >&2
532- if is_allowed " $real " || is_existing_path " $path .$entry " <<< " $schema" ; then
535+ if is_existing_path " $path .$entry " <<< " $schema" ; then
533536
534537 echo -e " ${lvl} - ${Purple} Additional properties allowed for ${real:- .}${None} " >&2
535538
536539 for prop in $( get_keys " $real " <<< " $json" | sort) ; do
540+
537541 if ! grep -q " $prop " <<< " $props" ; then
538542 echo -e " ${lvl} - Adding additional property <${Purple} $real .$prop ${None} >" >&2
539543
@@ -572,7 +576,7 @@ if [ -n "$import" ] && [ ! -f "$schema" ]; then
572576 schema=$( get_schema_url $search )
573577
574578 # log
575- echo -e " ${Yellow} Infering schema from schema store for ${UYellow} $search ${Yellow} ${schema: +" found!" }${None}${None} " >&2
579+ echo -e " ${Yellow} Infering schema from schema store for ${UYellow} $search ${Yellow} ${schema: +" found!" }${None} " >&2
576580fi
577581
578582# if schema file does not exist, use fallback schema
@@ -601,6 +605,9 @@ if test -z "$schema"; then
601605 exit 1
602606fi
603607
608+ # Allow additional properties at root level
609+ schema=$( get_schema " $schema " " " " $allow " )
610+
604611# Strip comments from JSON and go through normalization
605612# if parse "$json" "$schema" >/dev/null; then ## Uncomment this line to enable debug output
606613if validate " $json " " $schema " ; then
@@ -615,9 +622,9 @@ if test -s /tmp/$$.json; then
615622 if test -z " $save " ; then
616623 traverse $json /tmp/$$ .json | jq -C --indent ${tabSize:- 2} .
617624 else
618- traverse $json /tmp/$$ .json | jq -M --indent ${tabSize:- 4} . > $json
625+ traverse $json /tmp/$$ .json | jq -M --indent ${tabSize:- 4} . > /tmp/ $$ .tmp && mv /tmp/ $$ .tmp $json
619626 fi
620627fi
621628
622629# Clean up
623- rm -f /tmp/$$ .json
630+ rm -f /tmp/$$ .*
0 commit comments