Skip to content

Conversation

vicheey
Copy link
Contributor

@vicheey vicheey commented Aug 22, 2025

Issue #, if available

None

Description of changes

This PR introduces a comprehensive validation framework for SAM resource properties, and enhancing the translator's ability to catch configuration errors early in the deployment process.

  • Added property rule validation method in samtranslator/model/init.py
  • Enhanced add propagate tags logic in samtranslator/model/init.py
  • Add enhance property rule validation logic samtranslator/validator/property_rule.py
  • Updated SAM resources in samtranslator/model/sam_resources.py to integrate
    validation
  • Comprehensive test coverage in tests/test_model.py
  • Comprehensive test coverage in tests/validator/test_property_rule.py

Description of how you validated changes

  • Added extensive test suite covering validation scenarios
  • All existing tests continue to pass

Checklist

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@vicheey vicheey requested a review from a team as a code owner August 22, 2025 21:16
@vicheey
Copy link
Contributor Author

vicheey commented Aug 22, 2025

The Tests failed because of lower coverage percentage. (94.82% instead of 95%). Added validation logic push the overall total line increase and lower the coverage percentage.

Low coverage files:
• samtranslator/validator/validator.py - 27% coverage
• samtranslator/model/init.py - 69% coverage
• samtranslator/model/naming.py - 71% coverage
• samtranslator/region_configuration.py - 79% coverage

Removing deprecated and unused samtranslator/validator/validator.py

@vicheey
Copy link
Contributor Author

vicheey commented Aug 22, 2025

Current usage analysis:
• No active imports of SamTemplateValidator in any source
files
• No test files directly testing SamTemplateValidator

What IS being used:
samtranslator.validator.value_validator.sam_expect - Used extensively across 12+ files
• Direct jsonschema.validate() calls in tests (instead of the deprecated SAM wrapper)

However, we cannot delete this file yet because this public facing interface might be used by package consuming this package.
I added the samtranslator.validator.validator.py to test coverage ignore list.

--------------------------------
TOTAL   9640    314   3402    220    95%

Required test coverage of 95% reached. Total coverage: 95.28%

=============== 4118 passed in 86.15s (0:01:26) ================

@vicheey vicheey force-pushed the validation-framework-extraction branch from 188db2a to b52c322 Compare August 22, 2025 23:50
@vicheey vicheey requested a review from roger-zhangg August 28, 2025 05:00
Comment on lines +256 to +270
# def get_property_validation_rules(self) -> Optional[PropertyRules]:
# """Override to provide PropertyRules validation for SAM Function."""
# TODO: To enable these rules, we need to update translator test input/output files to property configure template
# to avoid fail-fast. eg: test with DeploymentPreference without AutoPublishAlias would fail fast before reaching testing state
# from samtranslator.internal.schema_source.aws_serverless_function import Properties as FunctionProperties
# return (PropertyRules(FunctionProperties)
# .addMutuallyExclusive("ImageUri", "InlineCode", "CodeUri")
# .addConditionalInclusive("DeploymentPreference", ["AutoPublishAlias"])
# .addConditionalInclusive("ProvisionedConcurrencyConfig", ["AutoPublishAlias"])
# .addConditionalInclusive("PackageType=Zip", ["Runtime", "Handler"])
# .addConditionalInclusive("PackageType=Image", ["ImageUri"])
# .addConditionalExclusive("PackageType=Zip", ["ImageUri", "ImageConfig"])
# .addConditionalExclusive("PackageType=Image", ["Runtime", "Handler", "Layers"]))
# return None

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we plan to leave this here, commented out?

# TODO: Skip pass schema_class=aws_serverless_function.Properties to skip schema validation for now.
# - adding this now would required update error message in error error_function_*_test.py
# - add this when we can verify that changing error message would not break customers
# self.validate_before_transform(schema_class=aws_serverless_function.Properties)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what is this validate_before_transform that appears here? Is it defined somewhere else?

missing = [prop for prop in target_props if not self._check_property_condition(validated_model, prop)]
if missing:
missing_props = ", ".join(f"'{p}'" for p in missing)
return f"'{source_props[0]}' requires all of: {missing_props}."
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is source_props only of length 1?

If so, can you change line 101 to just take the first one from source_props instead of loop over the array?

Or if we want to still keep it extensible, we would have to change this error here to point to the all the source_props (or the right one), instead of just using source_props[0]

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same for conditional_exclusive

Comment on lines +179 to +182
mock_attr1 = Mock()
mock_attr2 = Mock()
mock_attr1.return_value = "value1"
mock_attr2.return_value = "value2"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not necessary (and I don't know how much it's used in other places, but you can also set the return value right away:

  mock_attr1 = Mock(return_value="value1")
  mock_attr2 = Mock(return_value="value2")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants