Skip to content

Commit 6a16639

Browse files
authored
chore: remove legacy schema validation code (#3257)
1 parent 8b1b143 commit 6a16639

File tree

88 files changed

+8
-4283
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+8
-4283
lines changed

samtranslator/validator/validator.py

Lines changed: 8 additions & 177 deletions
Original file line numberDiff line numberDiff line change
@@ -1,196 +1,27 @@
1-
import json
2-
import os
31
import re
4-
from pathlib import Path
5-
from typing import Any
62

7-
import jsonschema
8-
9-
from . import sam_schema
3+
from samtranslator.internal.deprecation_control import deprecated
104

115

126
class SamTemplateValidator:
137
"""
14-
SAM template validator
8+
Using dummy values; unused code.
159
"""
1610

17-
# Useful to find a unicode prefixed string type to replace it with the non unicode version
18-
# On Python2, the validator returns types in error messages prefixed with 'u'
19-
# We remove them to be consistent between python versions
20-
# Example: "u'integer'" -> "'integer'"
2111
UNICODE_TYPE_REGEX = re.compile("u('[^']+')")
2212

13+
@deprecated()
2314
def __init__(self, schema=None) -> None: # type: ignore[no-untyped-def]
24-
"""
25-
Constructor
26-
27-
Parameters
28-
----------
29-
schema_path : str, optional
30-
Path to a schema to use for validation, by default None, the default schema.json will be used
31-
"""
32-
if not schema:
33-
schema = self._read_json(sam_schema.SCHEMA_NEW_FILE)
34-
35-
# Helps resolve the $Ref to external files
36-
# For cross platform resolving, we have to load the sub schemas into
37-
# a store and pass it to the Resolver. We cannot use the "file://" style
38-
# of referencing inside a "$ref" of a schema as this will lead to mixups
39-
# on Windows because of different path separator: \\ instead of /
40-
schema_store = {}
41-
definitions_dir = sam_schema.SCHEMA_DIR / "definitions"
42-
43-
for sub_schema in os.listdir(definitions_dir):
44-
if sub_schema.endswith(".json"):
45-
with (definitions_dir / sub_schema).open(encoding="utf-8") as f:
46-
schema_content = f.read()
47-
schema_store[sub_schema] = json.loads(schema_content)
48-
49-
resolver = jsonschema.RefResolver.from_schema(schema, store=schema_store) # type: ignore[no-untyped-call]
50-
51-
SAMValidator = jsonschema.validators.extend(
52-
jsonschema.Draft7Validator,
53-
type_checker=jsonschema.Draft7Validator.TYPE_CHECKER.redefine_many(
54-
{"object": is_object, "intrinsic": is_intrinsic}
55-
),
56-
)
57-
self.validator = SAMValidator(schema, resolver=resolver)
15+
pass
5816

5917
@staticmethod
18+
@deprecated()
6019
def validate(template_dict, schema=None): # type: ignore[no-untyped-def]
61-
"""
62-
Validates a SAM Template
63-
64-
[DEPRECATED]: Instanciate this class and use the get_errors instead:
65-
validator = SamTemplateValidator()
66-
validator.get_errors(template_dict)
67-
68-
Kept for backward compatibility
69-
70-
Parameters
71-
----------
72-
template_dict : dict
73-
Template
74-
schema : dict, optional
75-
Schema content, defaults to the integrated schema
76-
77-
Returns
78-
-------
79-
str
80-
Validation errors separated by commas ","
81-
"""
82-
validator = SamTemplateValidator(schema)
83-
84-
return ", ".join(validator.get_errors(template_dict)) # type: ignore[no-untyped-call]
20+
return ""
8521

22+
@deprecated()
8623
def get_errors(self, template_dict): # type: ignore[no-untyped-def]
87-
"""
88-
Validates a SAM Template
89-
90-
Parameters
91-
----------
92-
template_dict : dict
93-
Template to validate
94-
schema : str, optional
95-
Schema content, by default None
96-
97-
Returns
98-
-------
99-
list[str]
100-
List of validation errors if any, empty otherwise
101-
"""
102-
103-
# Tree of Error objects
104-
# Each object can have a list of child errors in its Context attribute
105-
validation_errors = self.validator.iter_errors(template_dict)
106-
107-
# Set of "[Path.To.Element] Error message"
108-
# To track error uniqueness, Dict instead of List, for speed
109-
errors_set = {} # type: ignore[var-annotated]
110-
111-
for e in validation_errors:
112-
self._process_error(e, errors_set) # type: ignore[no-untyped-call]
113-
114-
# To be consistent across python versions 2 and 3, we have to sort the final result
115-
# It seems that the validator is not receiving the properties in the same order between python 2 and 3
116-
# It thus returns errors in a different order
117-
return sorted(errors_set.keys())
118-
119-
def _process_error(self, error, errors_set): # type: ignore[no-untyped-def]
120-
"""
121-
Processes the validation errors recursively
122-
error is actually a tree of errors
123-
Each error can have a list of child errors in its 'context' attribute
124-
125-
Parameters
126-
----------
127-
error : Error
128-
Error at the head
129-
errors_set : Dict
130-
Set of formatted errors
131-
"""
132-
if error is None:
133-
return
134-
135-
if not error.context:
136-
# We only display the leaves
137-
# Format the message with pseudo JSON Path:
138-
# [Path.To.Element] Error message
139-
error_path = ".".join([str(p) for p in error.absolute_path]) if error.absolute_path else "."
140-
141-
error_content = f"[{error_path}] {self._cleanup_error_message(error)}" # type: ignore[no-untyped-call]
142-
143-
if error_content not in errors_set:
144-
# We set the value to None as we don't use it
145-
errors_set[error_content] = None
146-
return
147-
148-
for context_error in error.context:
149-
# Each "context" item is also a validation error
150-
self._process_error(context_error, errors_set) # type: ignore[no-untyped-call]
151-
152-
def _cleanup_error_message(self, error): # type: ignore[no-untyped-def]
153-
"""
154-
Cleans an error message up to remove unecessary clutter or replace
155-
it with a more meaningful one
156-
157-
Parameters
158-
----------
159-
error : Error
160-
Error message to clean
161-
162-
Returns
163-
-------
164-
str
165-
Cleaned message
166-
"""
167-
final_message = re.sub(self.UNICODE_TYPE_REGEX, r"\1", error.message)
168-
169-
if final_message.endswith(" under any of the given schemas"):
170-
return "Is not valid"
171-
if final_message.startswith(("None is not of type ", "None is not one of ")):
172-
return "Must not be empty"
173-
if " does not match " in final_message and "patternError" in error.schema:
174-
return re.sub("does not match .+", error.schema.get("patternError"), final_message)
175-
176-
return final_message
177-
178-
def _read_json(self, filepath: Path) -> Any:
179-
"""
180-
Returns the content of a JSON file
181-
182-
Parameters
183-
----------
184-
filepath : Path
185-
File path
186-
187-
Returns
188-
-------
189-
dict
190-
Dictionary representing the JSON content
191-
"""
192-
with filepath.open(encoding="utf-8") as fp:
193-
return json.load(fp)
24+
return []
19425

19526

19627
# Type definition redefinitions

tests/validator/__init__.py

Whitespace-only changes.

tests/validator/input/api/error_accesslogsetting.yaml

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

tests/validator/input/api/error_auth.yaml

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

0 commit comments

Comments
 (0)