From bb38f6c1abac4e42e00fc70a3bd21fd55b0c592e Mon Sep 17 00:00:00 2001 From: Chris Rehn Date: Mon, 21 Nov 2022 10:11:05 -0800 Subject: [PATCH 1/4] feat: fast schema validation --- Makefile | 2 +- bin/validate.sh | 8 -------- bin/validate_schema.py | 44 ++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 45 insertions(+), 9 deletions(-) delete mode 100755 bin/validate.sh create mode 100644 bin/validate_schema.py diff --git a/Makefile b/Makefile index 9513ab2072..93de3c4154 100755 --- a/Makefile +++ b/Makefile @@ -35,7 +35,7 @@ lint: # Linter performs static analysis to catch latent bugs pylint --rcfile .pylintrc samtranslator # Ensure templates adhere to JSON schema - bin/validate.sh + bin/validate_schema.py prepare-companion-stack: pytest -v --no-cov integration/setup -m setup diff --git a/bin/validate.sh b/bin/validate.sh deleted file mode 100755 index d4cbb0e23c..0000000000 --- a/bin/validate.sh +++ /dev/null @@ -1,8 +0,0 @@ -#!/usr/bin/env bash -set -euxo pipefail - -# TODO: Enable the valid templates -# TODO: Switch to way-faster Python script -find tests/translator/input -type f -name '*.yaml' | grep -v error_ | grep -v unsupported_resources | grep -v resource_with_invalid_type | shuf | while read -r template; do - jsonschema -i <(cfn-flip --json "${template}") samtranslator/schema/schema.json -done diff --git a/bin/validate_schema.py b/bin/validate_schema.py new file mode 100644 index 0000000000..a8037b1d7d --- /dev/null +++ b/bin/validate_schema.py @@ -0,0 +1,44 @@ +import json +from pathlib import Path +from typing import Iterator + +from cfn_flip import to_json # type: ignore +from jsonschema import validate + +SCHEMA = json.loads(Path("samtranslator/schema/schema.json").read_bytes()) + + +def get_templates() -> Iterator[Path]: + paths = ( + list(Path("tests/translator/input").glob("**/*.yaml")) + + list(Path("tests/translator/input").glob("**/*.yml")) + + list(Path("integration/resources/templates").glob("**/*.yaml")) + + list(Path("integration/resources/templates").glob("**/*.yml")) + ) + # TODO: Enable (most likely) everything but error_ + skips = [ + "error_", + "unsupported_resources", + "resource_with_invalid_type", + ] + + def should_skip(s: str) -> bool: + for skip in skips: + if skip in s: + return True + return False + + for path in paths: + if not should_skip(str(path)): + yield path + + +def main() -> None: + for path in get_templates(): + print(f"Checking {path}") + obj = json.loads(to_json(path.read_bytes())) + validate(obj, schema=SCHEMA) + + +if __name__ == "__main__": + main() \ No newline at end of file From 3bb3e0965018a12276482fd32385de0447255bfa Mon Sep 17 00:00:00 2001 From: Chris Rehn Date: Mon, 21 Nov 2022 10:13:40 -0800 Subject: [PATCH 2/4] make black --- bin/validate_schema.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/bin/validate_schema.py b/bin/validate_schema.py index a8037b1d7d..6d2662167a 100644 --- a/bin/validate_schema.py +++ b/bin/validate_schema.py @@ -41,4 +41,4 @@ def main() -> None: if __name__ == "__main__": - main() \ No newline at end of file + main() From 1fc17070624bb92014f5704928a0d5f6ad02c9ea Mon Sep 17 00:00:00 2001 From: Chris Rehn Date: Mon, 21 Nov 2022 10:16:12 -0800 Subject: [PATCH 3/4] Make executable --- bin/validate_schema.py | 0 1 file changed, 0 insertions(+), 0 deletions(-) mode change 100644 => 100755 bin/validate_schema.py diff --git a/bin/validate_schema.py b/bin/validate_schema.py old mode 100644 new mode 100755 From afbc88ce1142f4cecabc298f00ad902c8890178b Mon Sep 17 00:00:00 2001 From: Chris Rehn Date: Mon, 21 Nov 2022 10:17:14 -0800 Subject: [PATCH 4/4] Add shebang --- bin/validate_schema.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/bin/validate_schema.py b/bin/validate_schema.py index 6d2662167a..44771593ff 100755 --- a/bin/validate_schema.py +++ b/bin/validate_schema.py @@ -1,3 +1,5 @@ +#!/usr/bin/env python + import json from pathlib import Path from typing import Iterator