MCPcopy Create free account
hub / github.com/StackStorm/st2 / validate_config_against_schema

Function validate_config_against_schema

st2common/st2common/util/pack.py:135–190  ·  view source on GitHub ↗

Validate provided config dictionary against the provided config schema dictionary.

(
    config_schema, config_object, config_path, pack_name=None
)

Source from the content-addressed store, hash-verified

133
134
135def validate_config_against_schema(
136 config_schema, config_object, config_path, pack_name=None
137):
138 """
139 Validate provided config dictionary against the provided config schema
140 dictionary.
141 """
142 # NOTE: Lazy improt to avoid performance overhead of importing this module when it's not used
143 import jsonschema
144
145 pack_name = pack_name or "unknown"
146
147 schema = util_schema.get_schema_for_resource_parameters(
148 parameters_schema=config_schema, allow_additional_properties=True
149 )
150 instance = config_object
151
152 try:
153 cleaned = util_schema.validate(
154 instance=instance,
155 schema=schema,
156 cls=util_schema.CustomValidator,
157 use_default=True,
158 allow_default_none=True,
159 )
160 for key in cleaned:
161 if (
162 jinja_utils.is_jinja_expression(value=cleaned.get(key))
163 and "decrypt_kv" in cleaned.get(key)
164 and config_schema.get(key).get("secret")
165 ):
166 raise ValueValidationException(
167 'Values specified as "secret: True" in config '
168 "schema are automatically decrypted by default. Use "
169 'of "decrypt_kv" jinja filter is not allowed for '
170 "such values. Please check the specified values in "
171 "the config or the default values in the schema."
172 )
173 except jsonschema.ValidationError as e:
174 attribute = getattr(e, "path", [])
175
176 if isinstance(attribute, (tuple, list, Iterable)):
177 attribute = [str(item) for item in attribute]
178 attribute = ".".join(attribute)
179 else:
180 attribute = str(attribute)
181
182 msg = 'Failed validating attribute "%s" in config for pack "%s" (%s): %s' % (
183 attribute,
184 pack_name,
185 config_path,
186 six.text_type(e),
187 )
188 raise jsonschema.ValidationError(msg)
189
190 return cleaned
191
192

Callers 2

mainFunction · 0.90

Calls 3

validateMethod · 0.45
getMethod · 0.45

Tested by

no test coverage detected