Skip to content

Commit b4bda20

Browse files
author
Thomas G.
committed
feat: allow json validation caching
1 parent 0c50b65 commit b4bda20

8 files changed

Lines changed: 79 additions & 401 deletions

src/common-utils/_configure-feature.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ for package in package composer; do
9292

9393
# Post merge normalize package.json
9494
zz_log i "Post-merge normalize {U $package.json}"
95-
normalize-json -s -a -i -t ${tabSize:-4} $package.json
95+
normalize-json -c -w -a -i -t ${tabSize:-4} $package.json
9696
done
9797

9898
# Call all configure-xxx.sh scripts

src/common-utils/_default.schema.json

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,13 +31,19 @@
3131
}
3232
},
3333
"dependsOn": {
34-
"type": "array",
34+
"type": [
35+
"array",
36+
"object"
37+
],
3538
"items": {
3639
"type": "string"
3740
}
3841
},
3942
"installsAfter": {
40-
"type": "array",
43+
"type": [
44+
"array",
45+
"object"
46+
],
4147
"items": {
4248
"type": "string"
4349
}

src/common-utils/_devcontainer-feature.schema.json

Lines changed: 0 additions & 348 deletions
This file was deleted.

src/common-utils/_normalize-json.sh

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -8,28 +8,32 @@ set -e
88
# Function to print help and manage arguments
99
eval $(
1010
zz_args "Normalize JSON according to schema" $0 "$@" <<-help
11-
s - save save normalized json to original file
11+
w - write write normalized json to original file
1212
t tabSize tabSize tab size for indentation
13+
c - cache allow caching of schema validation map
1314
a - allow allow additional properties at root level
1415
d - debug debug output
1516
f fallback fallback fallback schema to use if none found locally
1617
l local local infer schema in <local> folder from json file name (x.y.json => <local>/y.schema.json)
1718
i - import infer on schema store if nothing found locally (x.y.json => "y" on schema store)
18-
- json json json to normalize
19-
+ schema schema schema to use for normalization
19+
s schema schema schema to use for normalization
20+
+ files files jsons to normalize
21+
2022
help
2123
)
2224

23-
# Validate JSON
24-
zz_log i "Normalizing JSON..."
25-
list=$(validate-json ${allow:+-a} ${debug:+-d} ${fallback:+-f "$fallback"} ${local:+-l "$local"} ${import:+-i} "$json" "$schema")
25+
for file in $files; do
26+
27+
# Validate JSON
28+
zz_log i "Normalizing {U $file}..."
29+
list=$(validate-json ${allow:+-a} ${cache:+-c} ${debug:+-d} ${fallback:+-f "$fallback"} ${local:+-l "$local"} ${import:+-i} ${schema:+-s"$schema"} $file)
2630

27-
if test -z "$list"; then
28-
zz_log e "JSON {U $json} not valid, cannot normalize" && exit 1
29-
fi
31+
if test -z "$list"; then
32+
zz_log e "JSON {U $file} not valid, cannot normalize" && exit 1
33+
fi
3034

31-
# Normalize JSON
32-
zz_json $json | jq -r --arg list "$list" '
35+
# Normalize JSON
36+
zz_json $file | jq -r --arg list "$list" '
3337
def transform($lst):
3438
$lst | split("\n")
3539
| map(select(length > 0))
@@ -54,17 +58,18 @@ zz_json $json | jq -r --arg list "$list" '
5458
5559
traverse(transform($list))' >/tmp/$$.json
5660

57-
# Handle output
58-
if test -s /tmp/$$.json; then
59-
if test -z "$save"; then
60-
jq -C --indent ${tabSize:-2} . /tmp/$$.json
61+
# Handle output
62+
if test -s /tmp/$$.json; then
63+
if test -z "$write"; then
64+
jq -C --indent ${tabSize:-2} . /tmp/$$.json
65+
else
66+
jq -M --indent ${tabSize:-4} . /tmp/$$.json >$file
67+
fi
68+
zz_log s "File {U $file} normalized"
6169
else
62-
jq -M --indent ${tabSize:-4} . /tmp/$$.json >$json
70+
zz_log e "File {U $file} not normalized"
6371
fi
64-
zz_log s "File {U $json} normalized"
65-
else
66-
zz_log e "File {U $json} not normalized"
67-
fi
6872

69-
# Clean up
70-
rm -f /tmp/$$.*
73+
# Clean up
74+
rm -f /tmp/$$.*
75+
done

src/common-utils/_validate-json.sh

Lines changed: 37 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -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
1617
help
1718
)
1819

@@ -455,43 +456,41 @@ 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/')
462463

463464
if [ "$local" == "true" ]; then
464-
local=$(dirname $0)
465+
local=$(dirname $(readlink -f $0))
465466
fi
466467

467-
# Identify schema file
468-
schema=$local/_$type.schema.json
468+
# Check if schema file exists
469+
if [ -f "$local/_$type.schema.json" ]; then
470+
schema=$local/_$type.schema.json
471+
fi
469472

470473
# log
471474
zz_log i "Infering schema from local folder {U $local} for {UYellow $json}"
472475
fi
473476

474477
# Check if schema file exists, and if not and import allowed, download it from schema store
475-
if [ -n "$import" ] && [ ! -f "$schema" ]; then
478+
if [ -n "$import" ] && [ -z "$schema" ]; then
476479

477480
search=$(basename -s .json $json | sed -E 's/.*\.(.*)/\1/').json
478481

479482
# find schema file url from schema store catalog
480483
schema=$(get_schema_url $search)
481484

482485
# log
483-
zz_log i "Infering schema from schema store for {U $search} ${schema:+"found!"}"
486+
zz_log i "Infering schema from schema store for {UYellow $search} ${schema:+"found!"}"
484487
fi
485488

486489
# if schema file does not exist, use fallback schema
487490
if [ -n "$fallback" ] && [ -z "$schema" ]; then
488491

489-
if [ "$fallback" == "true" ]; then
490-
if [ -n "$local" ]; then
491-
schema=$(readlink -f $local)/_default.schema.json
492-
else
493-
schema=$(dirname $0)/_default.schema.json
494-
fi
492+
if [ "$fallback" == "local" ] && [ -n "$local" ]; then
493+
schema=$(readlink -f $local)/_default.schema.json
495494
elif [ -f "$fallback" ]; then
496495
schema=$fallback
497496
else
@@ -502,14 +501,14 @@ if [ -n "$fallback" ] && [ -z "$schema" ]; then
502501
zz_log w "Using fallback schema {UYellow $schema}"
503502
fi
504503

504+
# Download schema file and add id to it if not present
505+
schema=$(zz_json -s "$schema")
506+
505507
# Check if schema is readable
506508
if test -z "$schema"; then
507509
zz_log e "Schema is missing" && exit 1
508510
fi
509511

510-
# Download schema file and add id to it if not present
511-
schema=$(zz_json -s "$schema")
512-
513512
# if schema is not a valid JSON, return error
514513
if ! is_json <<<"$schema"; then
515514
zz_log e "Invalid schema" && exit 1
@@ -521,9 +520,25 @@ if test -n "$allow"; then
521520
schema=$(echo "$schema" | jq '. + {"additionalProperties": true}' -)
522521
fi
523522

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"
523+
# is cache flag is set, cache schema
524+
hash=$(
525+
(
526+
jq 'paths | map(tostring) | join(".")' $json
527+
echo "$schema" | jq 'paths | map(tostring) | join(".")'
528+
) | sort -u | md5sum | awk '{print $1}'
529+
)
530+
map=~/.cache/$hash.schema.map
531+
zz_log i "Hash is {B $hash}"
532+
533+
if test -n "$cache" && test -s $map; then
534+
zz_log i "Using cached validation map"
535+
cat $map
527536
else
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'
537+
538+
# Validate JSON according to schema and display valid json paths
539+
if validate "$json" "$schema"; then
540+
zz_log s "File {U $json} valid"
541+
else
542+
zz_log e "File {U $json} empty or invalid" && exit 1
543+
fi | sed -n -e 's/^.//g' -e '/^$/d' -e 'G; s/\n/&&/; /^\([ -~]*\n\).*\n\1/d; s/\n//; h; P' | tee $map
544+
fi

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.4.3",
3+
"version": "3.5.1",
44
"description": "Common utils for tomgrv/devcontainer-features",
55
"dependsOn": {
66
"ghcr.io/devcontainers/features/common-utils": {}

src/githooks/_lint-staged.package.json

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@
66
"*.php": [
77
"composer lint"
88
],
9-
"*.{js,jsx,ts,tsx,md,html,css,json,vue, yaml, yml, sh}": [
9+
"*.{js,jsx,ts,tsx,md,html,css,vue,yaml,yml,sh}": [
1010
"npx --yes prettier --write"
1111
],
1212
"*.json": [
13-
"normalize-json -s -a -i -t 4 -l true"
13+
"normalize-json -c -w -a -i -t 4 -f true -l true"
1414
]
1515
}
16-
}
16+
}

src/githooks/devcontainer-feature.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
"id": "githooks",
33
"name": "Git Hooks",
44
"description": "A feature to add useful Git hooks to your project",
5-
"version": "5.6.0",
5+
"version": "5.7.1",
66
"dependsOn": {
77
"ghcr.io/devcontainers/features/node": {},
88
"ghcr.io/tomgrv/devcontainer-features/common-utils:3": {},
@@ -27,4 +27,4 @@
2727
"postCreateCommand": {
2828
"config": "configure-feature githooks"
2929
}
30-
}
30+
}

0 commit comments

Comments
 (0)