Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Aug 20, 2025

This PR fixes an issue where embedded struct fields with json:",inline" tags were being validated even when their parent embedded struct had conditional validation rules (like required_if) that should have skipped the validation.

Problem

When using embedded structs with conditional validation, nested fields were always validated regardless of whether the parent struct's condition was met:

type Permission struct {
    UserData `json:",inline" validate:"required_if:Type,give"`
    Type     string `json:"type" validate:"required|in:give,remove"`
    Access   string `json:"access" validate:"required_if:Type,remove"`
}

type UserData struct {
    NameField   `json:",inline"`
    BranchField `json:",inline"`
}

type NameField struct {
    Name string `json:"name" validate:"required|max_len:5000"`
}

type BranchField struct {
    Branch string `json:"branch" validate:"required|min_len:32|max_len:32"`
}

// This should pass but was failing
perm := Permission{
    Type:   "remove",
    Access: "change_types", 
}
// Error: map[branch:map[required:branch is required] name:map[required:name is required]]

The UserData struct has validate:"required_if:Type,give" meaning it should only be validated when Type equals "give". However, when Type was "remove", the nested fields (name and branch) were still being validated and causing validation failures.

Solution

Added logic in the validation flow to properly handle conditional validation for embedded structs:

  1. Enhanced validation flow: Modified Rule.Apply() to check if a field belongs to an embedded struct with conditional validation before processing
  2. Condition evaluation: Added shouldSkipEmbeddedFieldValidation() method that evaluates parent struct conditions
  3. Proper condition checking: Implemented separate condition evaluation logic for required_if, required_unless, and other conditional validators
  4. Recursive checking: The fix works for deeply nested embedded structures

Key Changes

  • validating.go: Added check for embedded field validation skipping in Rule.Apply()
  • validation.go: Added methods for evaluating conditional validator conditions:
    • shouldSkipEmbeddedFieldValidation()
    • evaluateConditionalValidatorCondition()
    • evaluateRequiredIfCondition()
    • evaluateRequiredUnlessCondition()

Testing

  • Added comprehensive test cases covering the original issue scenario
  • Verified all existing tests continue to pass
  • Added edge cases for required_unless and other conditional validators
  • Tested with both pointer and value struct validation

After the fix:

  • Type="remove" → validation passes (nested fields skipped) ✅
  • Type="give" → validation properly checks nested fields ✅
  • All existing functionality preserved ✅

Fixes #259.


✨ Let Copilot coding agent set things up for you — coding agent works faster and does higher quality work when set up for your repo.

Copy link
Contributor Author

Copilot AI commented Aug 20, 2025

@inhere 👋 This repository doesn't have Copilot instructions. With Copilot instructions, I can understand the repository better, work faster and produce higher quality PRs.

I can generate a .github/copilot-instructions.md file for you automatically. Click here to open a pre-filled issue and assign it to me. I'll write the instructions, and then tag you for review.

Copilot AI and others added 3 commits August 20, 2025 14:14
Co-authored-by: inhere <5302062+inhere@users.noreply.github.com>
…_if for nested fields

Co-authored-by: inhere <5302062+inhere@users.noreply.github.com>
Co-authored-by: inhere <5302062+inhere@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Embedded structs are not validated properly Fix embedded struct conditional validation - nested fields not respecting parent required_if conditions Aug 20, 2025
@Copilot Copilot AI requested a review from inhere August 20, 2025 14:24
Copilot finished work on behalf of inhere August 20, 2025 14:24
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.

Embedded structs are not validated properly

2 participants