Skip to content

Conversation

@kriben
Copy link
Collaborator

@kriben kriben commented Jan 16, 2026

No description provided.

Implements comprehensive type and range validation to prevent invalid data
from being set via Python GRPC interface. Invalid values are rejected with
clear error messages. Uses two-phase commit pattern to ensure atomic updates
where either all fields update or none do.

Validation Features:
- Type validation: Ensures field value types match (parsing errors caught)
- Range validation: Checks values against min/max constraints defined with
  setRange(), setMinValue(), setMaxValue()
- Object validation: Validates cross-field constraints via validate() override
- Atomic updates: Two-phase commit ensures all-or-nothing semantics
- Rollback on error: Failed validation leaves ALL fields at previous values
- Clear error messages: Aggregates and returns all validation failures

Implementation:
- Use std::expected<void, QString> for type-safe error handling (C++23)
- assignFieldValue(): Validates after parsing, rolls back individual field on error
- copyPdmObjectFromRipsToCaf(): Two-phase commit pattern
  - Phase 1: Validate all fields and collect changes (defer UI notifications)
  - Phase 2: If any validation fails, rollback all fields; otherwise commit all
- UpdateExistingPdmObject(): Returns grpc::INVALID_ARGUMENT with validation details

Testing:
- Comprehensive Python tests demonstrating validation behavior
- Test atomic rollback: multiple fields rolled back when one fails
- No regression in existing functionality

Documentation:
- Added validation documentation to CLAUDE.md with examples
- Explains how to set field ranges and handle validation errors in Python

Built on validation infrastructure from commit f342ca7 (PdmFieldHandle and
PdmObjectHandle validation support).
…thon GRPC

- Implemented strict parsing in 'writeToField' handlers to reject values with trailing garbage (e.g. '100.5test' for double, '0.99' for int/enum).
- Improved 'AppEnum' validation error message to list all valid options when an invalid value is provided.
- Added new test file 'test_validation_types_issue_12940.py' verifying strict type validation for double, int, and enum fields.
Implements automatic state restoration when field validation fails during
update(). The Python object now maintains consistency with the server state
by rolling back to the last known valid values.

- Store protobuf snapshot before update attempt
- Restore both protobuf object and Python attributes on validation failure
- Add test_validation_rollback() to verify rollback behavior
- Update bool validation test to assert automatic rollback
- Provides transaction-like semantics for field updates
Fixes cafPdmScripting_UnitTests::PdmFieldSerialization.OptionalValues test
failure where empty input for std::optional<int> and std::optional<double>
fields was incorrectly set to default values (0) instead of remaining unset.

Problem:
- Optional handler used error count to determine parsing success
- Empty stream reads set value to 0 but didn't add errors (only checked ReadCorruptData)
- Result: optional fields set to 0 instead of remaining unset

Solution:
- Modified optional handler to check for empty input before parsing
- Empty input now correctly leaves optional field unset (no error)
- Maintains strict validation for invalid input (adds error, resets optional)

Changes:
- PdmFieldScriptingCapabilityIOHandler<std::optional<T>>: Check atEnd() before parsing
- int/double handlers: Only call checkForExtraCharactersAfterValue() on success

Testing:
- All cafPdmScripting_UnitTests pass (8/8)
- All test_pdmobject_validation.py tests pass (10/10)
- Optional double fields no longer generate spurious validation errors
@kriben kriben force-pushed the 12940-python-pdm-object-validation branch from 16da372 to d831a10 Compare January 19, 2026 10:06
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.

2 participants