-
Notifications
You must be signed in to change notification settings - Fork 1.1k
clean up output text of validator #1077
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -272,56 +272,164 @@ def validate( | |||||||||||||
| root_id: Optional[str] = None, | ||||||||||||||
| strict_integrity: bool = True, | ||||||||||||||
| ) -> None: | ||||||||||||||
| """Validates an A2UI messages against the schema. | ||||||||||||||
|
|
||||||||||||||
| Args: | ||||||||||||||
| a2ui_json: The A2UI message(s) to validate. | ||||||||||||||
| root_id: Optional root component ID. | ||||||||||||||
| strict_integrity: If True, performs full topology and integrity checks. | ||||||||||||||
| If False, only performs schema validation and basic syntax checks. | ||||||||||||||
| """ | ||||||||||||||
| """Validates an A2UI messages against the schema.""" | ||||||||||||||
| messages = a2ui_json if isinstance(a2ui_json, list) else [a2ui_json] | ||||||||||||||
|
|
||||||||||||||
| # Basic schema validation | ||||||||||||||
| errors = list(self._validator.iter_errors(messages)) | ||||||||||||||
| if errors: | ||||||||||||||
| error = errors[0] | ||||||||||||||
| msg = f"Validation failed: {error.message}" | ||||||||||||||
| if error.context: | ||||||||||||||
| msg += "\nContext failures:" | ||||||||||||||
| for sub_error in error.context: | ||||||||||||||
| msg += f"\n - {sub_error.message}" | ||||||||||||||
| if self.version == VERSION_0_9: | ||||||||||||||
| self._validate_0_9_custom(messages, root_id, strict_integrity) | ||||||||||||||
| else: | ||||||||||||||
| # Fallback to old behavior for v0.8 | ||||||||||||||
| errors = list(self._validator.iter_errors(messages)) | ||||||||||||||
| if errors: | ||||||||||||||
| error = errors[0] | ||||||||||||||
| msg = f"Validation failed: {error.message}" | ||||||||||||||
| if error.context: | ||||||||||||||
| msg += "\nContext failures:" | ||||||||||||||
| for sub_error in error.context: | ||||||||||||||
| msg += f"\n - {sub_error.message}" | ||||||||||||||
| raise ValueError(msg) | ||||||||||||||
|
|
||||||||||||||
| for message in messages: | ||||||||||||||
| if not isinstance(message, dict): | ||||||||||||||
| continue | ||||||||||||||
|
|
||||||||||||||
| components = None | ||||||||||||||
| surface_id = None | ||||||||||||||
| if "surfaceUpdate" in message: # v0.8 | ||||||||||||||
| components = message["surfaceUpdate"].get(COMPONENTS) | ||||||||||||||
| surface_id = message["surfaceUpdate"].get("surfaceId") | ||||||||||||||
|
|
||||||||||||||
| if components: | ||||||||||||||
| ref_map = extract_component_ref_fields(self._catalog) | ||||||||||||||
| root_id = _find_root_id(messages, surface_id) | ||||||||||||||
| _validate_component_integrity( | ||||||||||||||
| root_id, components, ref_map, skip_root_check=not strict_integrity | ||||||||||||||
| ) | ||||||||||||||
| analyze_topology( | ||||||||||||||
| root_id, components, ref_map, raise_on_orphans=strict_integrity | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| _validate_recursion_and_paths(message) | ||||||||||||||
|
|
||||||||||||||
| def _validate_0_9_custom( | ||||||||||||||
| self, | ||||||||||||||
| messages: List[Dict[str, Any]], | ||||||||||||||
| root_id: Optional[str] = None, | ||||||||||||||
| strict_integrity: bool = True, | ||||||||||||||
| ) -> None: | ||||||||||||||
| all_errors = [] | ||||||||||||||
| for idx, message in enumerate(messages): | ||||||||||||||
| if not isinstance(message, dict): | ||||||||||||||
| all_errors.append(f"messages[{idx}]: Is not an object") | ||||||||||||||
| continue | ||||||||||||||
|
|
||||||||||||||
| if "createSurface" in message: | ||||||||||||||
| val = self._get_sub_validator("CreateSurfaceMessage") | ||||||||||||||
| all_errors.extend(self._get_formatted_errors(val, message, f"messages[{idx}]")) | ||||||||||||||
| elif "updateComponents" in message: | ||||||||||||||
| all_errors.extend(self._get_update_components_errors(message, f"messages[{idx}]")) | ||||||||||||||
| elif "updateDataModel" in message: | ||||||||||||||
| val = self._get_sub_validator("UpdateDataModelMessage") | ||||||||||||||
| all_errors.extend(self._get_formatted_errors(val, message, f"messages[{idx}]")) | ||||||||||||||
| else: | ||||||||||||||
| keys = list(message.keys()) | ||||||||||||||
| all_errors.append(f"messages[{idx}]: Unknown message type with keys {keys}") | ||||||||||||||
|
|
||||||||||||||
| if all_errors: | ||||||||||||||
| msg = "Validation failed:\n" + "\n".join(f" - {err}" for err in all_errors) | ||||||||||||||
| raise ValueError(msg) | ||||||||||||||
|
|
||||||||||||||
| # Integrity checks | ||||||||||||||
| for message in messages: | ||||||||||||||
| if not isinstance(message, dict): | ||||||||||||||
| continue | ||||||||||||||
|
|
||||||||||||||
| components = None | ||||||||||||||
| surface_id = None | ||||||||||||||
| if "surfaceUpdate" in message: # v0.8 | ||||||||||||||
| components = message["surfaceUpdate"].get(COMPONENTS) | ||||||||||||||
| surface_id = message["surfaceUpdate"].get("surfaceId") | ||||||||||||||
| elif "updateComponents" in message and isinstance( | ||||||||||||||
| if "updateComponents" in message and isinstance( | ||||||||||||||
| message["updateComponents"], dict | ||||||||||||||
| ): # v0.9 | ||||||||||||||
| ): | ||||||||||||||
| components = message["updateComponents"].get(COMPONENTS) | ||||||||||||||
| surface_id = message["updateComponents"].get("surfaceId") | ||||||||||||||
|
|
||||||||||||||
| if components: | ||||||||||||||
| ref_map = extract_component_ref_fields(self._catalog) | ||||||||||||||
| root_id = _find_root_id(messages, surface_id) | ||||||||||||||
| # Always check for basic integrity (duplicates) | ||||||||||||||
| _validate_component_integrity( | ||||||||||||||
| root_id, components, ref_map, skip_root_check=not strict_integrity | ||||||||||||||
| ) | ||||||||||||||
| # Always check topology (cycles), but only raise on orphans if strict_integrity is True | ||||||||||||||
| analyze_topology( | ||||||||||||||
| root_id, components, ref_map, raise_on_orphans=strict_integrity | ||||||||||||||
| ) | ||||||||||||||
|
|
||||||||||||||
| _validate_recursion_and_paths(message) | ||||||||||||||
|
Comment on lines
348
to
369
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This block for performing integrity and topology checks is nearly identical to the logic in the |
||||||||||||||
|
|
||||||||||||||
| def _get_sub_validator(self, def_name: str) -> Draft202012Validator: | ||||||||||||||
| sub_schema = self._catalog.s2c_schema["$defs"][def_name] | ||||||||||||||
| return Draft202012Validator(sub_schema, registry=self._validator._registry) | ||||||||||||||
wrenj marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||||||||||||||
|
|
||||||||||||||
| def _get_formatted_errors(self, validator: Draft202012Validator, instance: Any, base_path: str) -> List[str]: | ||||||||||||||
| errors = list(validator.iter_errors(instance)) | ||||||||||||||
| formatted = [] | ||||||||||||||
| for err in errors: | ||||||||||||||
| path_str = ".".join(str(p) for p in err.path) | ||||||||||||||
| full_path = f"{base_path}.{path_str}" if path_str else base_path | ||||||||||||||
|
|
||||||||||||||
| message = err.message | ||||||||||||||
| if ("Unevaluated properties are not allowed" in message or "Additional properties are not allowed" in message) and "(" in message and ")" in message: | ||||||||||||||
| message = message[message.find("(")+1 : message.rfind(")")] | ||||||||||||||
|
||||||||||||||
| if ("Unevaluated properties are not allowed" in message or "Additional properties are not allowed" in message) and "(" in message and ")" in message: | |
| message = message[message.find("(")+1 : message.rfind(")")] | |
| if err.validator in ("additionalProperties", "unevaluatedProperties"): | |
| match = re.search(r"\(([^)]+)\)", message) | |
| if match: | |
| message = match.group(1) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Creating a new Draft202012Validator instance for every component in a loop is inefficient and will cause significant performance degradation for large payloads. These validators should be cached by comp_type.
cache = self.__dict__.setdefault("_comp_validator_cache", {})
if comp_type not in cache:
temp_schema = {
"$schema": "https://json-schema.org/draft/2020-12/schema",
"$ref": f"catalog.json#/components/{comp_type}"
}
cache[comp_type] = Draft202012Validator(
temp_schema, registry=self._validator._registry
)
validator = cache[comp_type]
Uh oh!
There was an error while loading. Please reload this page.