-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Expand file tree
/
Copy pathany_cfn_resource.py
More file actions
30 lines (22 loc) · 1.07 KB
/
any_cfn_resource.py
File metadata and controls
30 lines (22 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
from typing import Any, Dict
from pydantic import field_validator
from samtranslator.internal.schema_source.common import LenientBaseModel
# Anything goes if has string Type but is not AWS::Serverless::*
class Resource(LenientBaseModel):
Type: str
# Use model_json_schema_extra to add the pattern to JSON Schema
# Pydantic's Rust regex doesn't support lookahead, but JSON Schema validators do
model_config = {
"json_schema_extra": lambda schema, _: _add_type_pattern(schema),
}
@field_validator("Type")
@classmethod
def type_must_not_be_serverless(cls, v: str) -> str:
"""Validate that Type does not start with AWS::Serverless::"""
if v.startswith("AWS::Serverless::"):
raise ValueError("Type must not start with 'AWS::Serverless::'")
return v
def _add_type_pattern(schema: Dict[str, Any]) -> None:
"""Add pattern constraint to Type field in JSON Schema."""
if "properties" in schema and "Type" in schema["properties"]:
schema["properties"]["Type"]["pattern"] = r"^(?!AWS::Serverless::).+$"