Skip to content

Commit 5e495af

Browse files
author
Thomas G.
committed
fix: 🐛 ensure normalization
1 parent e5e4d07 commit 5e495af

3 files changed

Lines changed: 45 additions & 38 deletions

File tree

src/common-utils/_configure-feature.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ for package in package composer; do
7878
else
7979
# Pre-sort the existing package.json
8080
echo "${Yellow}Pre-merge normalize $package.json${None}"
81-
normalize-json -t ${tabSize:-4} $package.json
81+
normalize-json -s -a -i -t ${tabSize:-4} $package.json
8282
fi
8383

8484
# Merge all package folder json files into the top-level package.json

src/common-utils/_normalize-json.sh

Lines changed: 43 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -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
7964
get_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+
188206
traverse() {
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
576580
fi
577581

578582
# if schema file does not exist, use fallback schema
@@ -601,6 +605,9 @@ if test -z "$schema"; then
601605
exit 1
602606
fi
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
606613
if 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
620627
fi
621628

622629
# Clean up
623-
rm -f /tmp/$$.json
630+
rm -f /tmp/$$.*

src/common-utils/devcontainer-feature.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "Common Utils",
3-
"version": "3.3.2",
3+
"version": "3.3.5",
44
"description": "Common utils for tomgrv/devcontainer-features",
55
"dependsOn": {
66
"ghcr.io/devcontainers/features/common-utils": {}

0 commit comments

Comments
 (0)