Skip to content

Conversation

@lklimek
Copy link
Contributor

@lklimek lklimek commented Jan 15, 2026

Issue being fixed or feature implemented

Implements the refersTo document property keyword (per docs/specs/reference-validation.md), enabling data contracts to declare that identifier properties reference existing identities. When refersTo.mustExist
is true, document state validation rejects transitions where the referenced identity does not exist on Platform.

What was done?

  • Extended document meta-schema to accept a refersTo object (type: "identity", mustExist: bool) on identifier-typed properties
  • Added DocumentProperty.reference field to store parsed reference metadata during DocumentType::try_from_schema
  • Implemented DocumentReferenceValidation trait with versioned v0 module that fetches identity revision as an existence check
  • Added new state validators (state_v2) for both document create and replace transition actions that enforce mustExist: true semantics
  • Defined ReferencedEntityNotFoundError consensus state error with dedicated error code
  • Added contract update validation to reject adding/modifying refersTo after contract creation
  • Added schema verification that refersTo is only allowed on identifier-typed properties
  • Gated enforcement behind platform version 12 (v8 validation versions)
  • Fixed enum ordering issues in DPP and added missing WASM error variant

How Has This Been Tested?

  • Unit tests for refersTo schema parsing and rejection on non-identifier properties (test(dpp): refersTo tests)
  • State validation tests for document creation and replacement with valid/invalid identity references (batch/tests/document/creation.rs, replacement.rs)
  • Contract update validation tests ensuring refersTo cannot be added post-creation
  • Schema meta-validator tests
  • WASM build verification with new error variant

Breaking Changes

  • New protocol version 12 required for contracts using refersTo
  • Nodes on older versions will reject contracts containing the refersTo keyword
  • New consensus error code added (ReferencedEntityNotFoundError) — clients need updated error handling

Checklist:

  • I have performed a self-review of my own code
  • I have commented my code, particularly in hard-to-understand areas
  • I have added or updated relevant unit/integration/functional/e2e tests
  • I have added "!" to the title and described breaking changes in the corresponding section if my code contains any
  • I have made corresponding changes to the documentation if needed

For repository code-owners and collaborators only

  • I have assigned this pull request to a milestone

Summary by CodeRabbit

  • New Features

    • Added support for document reference validation, enabling properties to reference identities with configurable existence checks during document creation and updates.
    • References are immutable after contract creation and support nested fields.
  • Documentation

    • New reference validation specification documenting schema constraints, validation rules, and implementation details.
  • Chores

    • Updated network configuration script for improved YAML parsing and HTTPS API endpoint support.

✏️ Tip: You can customize this high-level summary in your review settings.

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 15, 2026

📝 Walkthrough

Walkthrough

This pull request introduces a new "refersTo" feature for document properties that enables existence validation of referenced identities. It includes schema updates, data model changes, validation logic across multiple platform versions, error handling, test fixtures, and WebAssembly bindings to support reference validation during document creation and replacement operations.

Changes

Cohort / File(s) Summary
Documentation & Specification
docs/specs/reference-validation.md
New specification document describing refersTo keyword for document properties with identity existence validation requirements, backward compatibility, and acceptance criteria.
Document Meta Schema
packages/rs-dpp/schema/meta_schemas/document/v0/document-meta.json
Added refersTo property definition with type and mustExist fields; added dependent schema constraints for identifier-related refersTo validation.
Document Property Types & Parsing
packages/rs-dpp/src/data_contract/document_type/property/mod.rs, packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/mod.rs, packages/rs-dpp/src/data_contract/document_type/mod.rs
Introduced DocumentPropertyReference and DocumentPropertyReferenceTarget types; added parsing logic to extract and validate refersTo constraints on identifier properties.
Document Type Validation
packages/rs-dpp/src/data_contract/document_type/methods/validate_update/v0/mod.rs
Added tests validating that refersTo additions or modifications are rejected as incompatible schema changes.
Document Property Initialization
packages/rs-dpp/src/data_contract/document_type/v0/random_document_type.rs
Initialize reference field to None in property construction paths.
DPP Error Types
packages/rs-dpp/src/errors/consensus/state/document/referenced_entity_not_found_error.rs, packages/rs-dpp/src/errors/consensus/state/document/mod.rs, packages/rs-dpp/src/errors/consensus/state/state_error.rs, packages/rs-dpp/src/errors/consensus/basic/basic_error.rs, packages/rs-dpp/src/errors/consensus/codes.rs
Added ReferencedEntityNotFoundError type with entity_id, entity_type, and path fields; integrated into StateError and BasicError enums with error code mappings.
DPP Meta Validators
packages/rs-dpp/src/validation/meta_validators/mod.rs
Added tests validating refersTo schema compliance against document meta schema.
Document Reference Validation Trait
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_reference_validation/mod.rs
Introduced DocumentReferenceValidation trait with versioned dispatch for document reference validation.
Document Reference Validation V0
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_reference_validation/v0/mod.rs
Implemented v0 validation logic that retrieves document types, iterates properties, validates identity existence for references marked must_exist, and returns appropriate ConsensusErrors.
Document Create Validation V2
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_create_transition_action/state_v2/mod.rs, packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_create_transition_action/mod.rs
Added v2 validation that delegates to v1, then validates document references via the new trait.
Document Replace Validation V1 & V2
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_replace_transition_action/mod.rs, packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_replace_transition_action/state_v1/mod.rs, packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/action_validation/document/document_replace_transition_action/state_v2/mod.rs
Added v1 (delegates to v0) and v2 (delegates to v1 then reference validation) traits and implementations.
Batch Advanced Structure Validation
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/advanced_structure/v0/mod.rs, packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/mod.rs
Reordered network parameter in validate_advanced_structure_from_state_v0 signature and updated call sites.
State Transition Validation Traits
packages/rs-drive-abci/src/execution/validation/state_transition/processor/traits/advanced_structure_with_state.rs, packages/rs-drive-abci/src/execution/validation/state_transition/processor/v0/mod.rs
Added clippy allowance for too_many_arguments; removed redundant comments.
Test Fixtures
packages/rs-drive-abci/tests/supporting_files/contract/reference-validation/reference-validation-contract.json, reference-validation-contract-must-exist-false.json, reference-validation-contract-nested.json
Added three contract schema fixtures defining toUserId and otherUserId identifier properties with refersTo constraints for reference validation testing.
Document Creation Tests
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/creation.rs
Added constants and helper functions for reference validation; added tests for missing identity, existing identity, mustExist false, nested references; converted vec! to slice literals.
Document Replacement Tests
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/replacement.rs
Added reference validation test helper and tests covering missing identity, mustExist false, changed fields filtering, and reference field modifications.
Platform Versioning
packages/rs-platform-version/src/version/drive_abci_versions/drive_abci_validation_versions/mod.rs, v1.rsv8.rs
Added document_reference_validation field to DriveAbciDocumentsStateTransitionValidationVersions; introduced v8 with document_create and document_replace validation set to version 2.
Platform V12
packages/rs-platform-version/src/version/v12.rs
Updated to use DRIVE_ABCI_VALIDATION_VERSIONS_V8 with comment noting reference validation support.
WASM Error Bindings
packages/wasm-dpp/src/errors/consensus/consensus_error.rs, packages/wasm-dpp/src/errors/consensus/state/document/mod.rs, packages/wasm-dpp/src/errors/consensus/state/document/referenced_entity_not_found_error.rs
Added ReferencedEntityNotFoundErrorWasm with JS-friendly accessors for entity_id, entity_type, path, and error code.
Identity State Transition
packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/identity_create_from_addresses/mod.rs
Updated TransactionArg import path from drive::grovedb to drive::query.
JSON Schema Compatibility Validator
packages/rs-json-schema-compatibility-validator/src/rules/rule_set.rs, tests/rules.rs
Added refersTo keyword compatibility rules disabling addition/removal; added test validating refersTo additions trigger incompatibility errors.
Build & Configuration
packages/dapi-grpc/Cargo.toml, packages/rs-drive-abci/tests/strategy_tests/test_cases/identity_and_document_tests.rs, scripts/configure_test_suite_network.sh
Minor formatting adjustment in Cargo.toml comments; added stack_size annotations to four tests; updated YAML path access and INSIGHT API URL to HTTPS with port 443.

Sequence Diagram(s)

sequenceDiagram
    participant ST as State Transition<br/>Processor
    participant DCV as Document Create/<br/>Replace Validator
    participant DRV as Document Reference<br/>Validator
    participant PT as Platform State
    participant IR as Identity Registry
    
    ST->>DCV: validate_state_v2()
    DCV->>DCV: validate_state_v1()
    alt v1 validation fails
        DCV-->>ST: return error
    end
    
    DCV->>DRV: validate_document_references()
    DRV->>PT: get document type
    alt document type not found
        DRV-->>DCV: InvalidDocumentTypeError
    end
    
    loop for each property with refersTo
        DRV->>DRV: check if must_exist=true
        alt must_exist and identity reference
            DRV->>IR: retrieve identity by ID
            alt identity not found
                IR-->>DRV: not found
                DRV-->>DCV: ReferencedEntityNotFoundError
            else identity found
                IR-->>DRV: identity data
            end
        end
    end
    
    DRV-->>DCV: validation result
    alt reference validation fails
        DCV-->>ST: return error
    else success
        DCV-->>ST: SimpleConsensusValidationResult::valid()
    end
Loading

Estimated code review effort

🎯 4 (Complex) | ⏱️ ~60 minutes

Suggested reviewers

  • pauldelucia

Poem

🐰 Hop along with references true,
Identity checks in all we do,
From schema to validation's might,
References now held tight!
v2 brings truth, no more ghost IDs,
Platform versioning flows with ease. 🥕

🚥 Pre-merge checks | ✅ 2 | ❌ 1
❌ Failed checks (1 warning)
Check name Status Explanation Resolution
Docstring Coverage ⚠️ Warning Docstring coverage is 69.66% which is insufficient. The required threshold is 80.00%. Write docstrings for the functions missing them to satisfy the coverage threshold.
✅ Passed checks (2 passed)
Check name Status Explanation
Title check ✅ Passed The title 'feat: identity reference validation' clearly and concisely describes the main feature added in the changeset—a reference validation capability for document properties pointing to identities.
Description Check ✅ Passed Check skipped - CodeRabbit’s high-level summary is enabled.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing touches
  • 📝 Generate docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands and usage tips.

@github-actions github-actions bot added this to the v3.0.0 milestone Jan 15, 2026
@lklimek lklimek force-pushed the feat/reference-validation branch from 72fede5 to 2eab1ca Compare January 20, 2026 12:56
@lklimek lklimek modified the milestones: v3.0.0, v3.1.0 Jan 22, 2026
@lklimek lklimek marked this pull request as ready for review January 22, 2026 13:02
@lklimek
Copy link
Contributor Author

lklimek commented Jan 22, 2026

@coderabbitai review

@coderabbitai
Copy link
Contributor

coderabbitai bot commented Jan 22, 2026

✅ Actions performed

Review triggered.

Note: CodeRabbit is an incremental review system and does not re-review already reviewed commits. This command is applicable only when automatic reviews are paused.

@github-actions github-actions bot modified the milestones: v3.1.0, v3.0.0 Jan 22, 2026
@lklimek lklimek modified the milestones: v3.0.0, v3.1.0 Jan 22, 2026
Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actionable comments posted: 5

🤖 Fix all issues with AI agents
In `@docs/specs/reference-validation.md`:
- Around line 31-33: The spec currently names the error
ReferencedIdentityNotFoundError but the implementation defines
ReferencedEntityNotFoundError; update the spec text to use
ReferencedEntityNotFoundError everywhere (including any examples/fields like {
path, identityId } -> keep fields consistent with the implementation) so the
documented error name and payload match the actual error type used by the code
(ReferencedEntityNotFoundError).

In `@packages/rs-dpp/schema/meta_schemas/document/v0/document-meta.json`:
- Around line 126-144: The schema currently allows "refersTo" on any property,
which can let non-identifier fields pass validation; update the JSON Schema so
that whenever a property contains "refersTo" it is constrained to the identifier
media type and appropriate base type: add an if/then (or dependencies) clause
that checks for the presence of "refersTo" and then enforces "type": "string"
(or the correct identifier base type) and "mediaType":
"application/vnd.dash.identifier" (or the canonical identifier media type used
in the repo), and apply this guard to both occurrences of the "refersTo" block
(the one with properties including "type" and "mustExist" and the second similar
block later in the file) so reference validation no longer accepts
non-identifier fields.

In
`@packages/rs-drive-abci/src/execution/validation/state_transition/state_transitions/batch/tests/document/replacement.rs`:
- Around line 2251-2317: The test functions in this file use imperative names
instead of the required "should ..." convention; rename the four test functions
test_document_replace_fails_when_referenced_identity_missing,
test_document_replace_succeeds_when_must_exist_false,
test_document_replace_validates_only_changed_fields, and
test_document_replace_fails_when_reference_field_changed_to_missing_identity to
start with "should" (e.g.,
should_document_replace_fail_when_referenced_identity_missing,
should_document_replace_succeed_when_must_exist_false,
should_document_replace_validate_only_changed_fields,
should_document_replace_fail_when_reference_field_changed_to_missing_identity)
so they conform to the tests/** naming guideline and update any references to
these symbols accordingly (keep function bodies and assertions unchanged).

In `@packages/rs-json-schema-compatibility-validator/tests/rules.rs`:
- Around line 54-97: Rename the test function to follow the "should …" naming
convention: change the function named test_refers_to_addition_is_incompatible to
a "should" form (for example should_refers_to_addition_be_incompatible or
should_detect_incompatible_refers_to_addition) so the test name reflects the
guideline; update the function declaration (fn
test_refers_to_addition_is_incompatible) to the new name and leave the body,
assertions (result.is_compatible, result.incompatible_changes() check) and
referenced symbols (/properties/toUserId/refersTo,
validate_schemas_compatibility, Options) unchanged.

In `@packages/rs-platform-version/src/version/v12.rs`:
- Around line 33-34: Fix the typo in the module-level documentation comment in
v12.rs: change "Intruduced" to "Introduced" in the doc comment that begins "This
version introduces document reference validation..." so the sentence reads
"Introduced in Platform release 3.1.0." This is the doc comment near the top of
the v12.rs file that documents the version change.
🧹 Nitpick comments (2)
packages/rs-dpp/src/data_contract/document_type/class_methods/try_from_schema/mod.rs (1)

117-154: Align refersTo validation across object/non-object paths.

insert_values only calls parse_property_reference on non-object properties, so a refersTo placed on an object property could be silently ignored, while insert_values_nested rejects it. Consider validating refersTo before the match to keep behavior consistent.

♻️ Suggested adjustment
-        let property_type =
-            DocumentPropertyType::try_from_value_map(&inner_properties, &config.into())?;
+        let property_type =
+            DocumentPropertyType::try_from_value_map(&inner_properties, &config.into())?;
+        let reference = parse_property_reference(&inner_properties, &property_type)?;
 
         match property_type {
             DocumentPropertyType::Object(_) => {
                 if let Some(properties_as_value) = inner_properties.get(property_names::PROPERTIES)
                 {
                     ...
                 }
             }
             property_type => {
-                let reference = parse_property_reference(&inner_properties, &property_type)?;
                 document_properties.insert(
                     prefixed_property_key,
                     DocumentProperty {
                         property_type,
                         required: is_required,
                         transient: is_transient,
                         reference,
                     },
                 );
             }
         };
packages/rs-dpp/src/data_contract/document_type/property/mod.rs (1)

35-40: Consider omitting reference: null from serialized output.

If DocumentProperty is serialized anywhere, the new field will emit reference: null for properties without references. If you want to preserve the previous JSON shape, skip serialization when it’s None.

♻️ Suggested adjustment
 pub struct DocumentProperty {
     pub property_type: DocumentPropertyType,
     pub required: bool,
     pub transient: bool,
+    #[serde(skip_serializing_if = "Option::is_none")]
     pub reference: Option<DocumentPropertyReference>,
 }

lklimek and others added 4 commits January 22, 2026 14:23
Co-authored-by: coderabbitai[bot] <136622811+coderabbitai[bot]@users.noreply.github.com>
@github-actions github-actions bot modified the milestones: v3.1.0, v3.0.0 Jan 22, 2026
@lklimek lklimek changed the base branch from v3.0-dev to v3.1-dev January 23, 2026 08:47
@github-actions github-actions bot modified the milestones: v3.0.0, v3.1.0 Jan 23, 2026
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.

3 participants