Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: add Excel validation utility for required field validation#458

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
tianyufeng925 wants to merge1 commit intoapache:main
base:main
Choose a base branch
Loading
fromtianyufeng925:feature/excel-validation-utils

Conversation

tianyufeng925
Copy link

Purpose of the pull request

This PR introduces a lightweight Excel validation utility to help developers validate required fields during Excel data import operations. This addresses a common need for data integrity validation when processing Excel files, providing a foundation for future validation enhancements.

What's changed?

🛠️ New Utility Class

  • ExcelValidationUtils - Provides batch validation capabilities for Excel data
    • validateRequired(Object) - Validates required fields for a single object
    • validateRequiredBatch(List<?>) - Batch validates a list of objects with row indexing
    • ValidationResult - Inner class to hold validation results with row index and error messages
    • Supports reflection-based field validation with proper error handling

🎯 New Annotation

  • @ExcelRequired - Simple annotation to mark fields as required during data validation
    • Customizable error message withmessage() attribute
    • Works seamlessly with existing@ExcelProperty annotations
    • Runtime retention for reflection-based validation

🧪 Comprehensive Testing

  • ExcelValidationUtilsTest - Complete test coverage with 5 test methods:
    • Valid data validation scenarios
    • Missing required fields detection
    • Batch validation with multiple objects
    • Edge cases (null objects, empty lists)
    • Error message validation and row indexing

📚 Technical Implementation

  • Zero breaking changes - Completely backward compatible
  • Minimal dependencies - Uses only existing FastExcel and standard Java APIs
  • Clean API design - Simple, intuitive method signatures following Java conventions
  • Proper error handling - Graceful handling of reflection exceptions with logging
  • English documentation - All JavaDoc comments and error messages in English
  • Performance optimized - Efficient reflection caching and batch processing

🎨 Usage Example

@DatapublicclassProjectData {@ExcelProperty("Project Name")@ExcelRequired(message ="Project name is required")privateStringprojectName;@ExcelProperty("Budget")@ExcelRequired(message ="Budget cannot be empty")privateBigDecimalbudget;@ExcelProperty("Department")privateStringdepartment;// Optional field}// Usage during Excel data importList<ProjectData>dataList =EasyExcel.read(fileName,ProjectData.class,listener)    .sheet().doReadSync();// Validate required fieldsList<ExcelValidationUtils.ValidationResult>results =ExcelValidationUtils.validateRequiredBatch(dataList);// Handle validation resultsif (!results.isEmpty()) {for (ExcelValidationUtils.ValidationResultresult :results) {System.out.println("Row " + (result.getRowIndex() +1) +" errors: " +String.join(", ",result.getErrors()));    }thrownewValidationException("Data validation failed");}

- Add ExcelValidationUtils class for batch validation of required fields- Add @ExcelRequired annotation to mark mandatory fields- Support validation during Excel data import operations- Include comprehensive unit tests with 100% coverage- All code follows English documentation standards
@delei
Copy link
Member

delei commentedJul 31, 2025
edited
Loading

I noticed the usage example you provided. It seems that the existing ReadListener is sufficient。

BTW, the new utility class must be called by the user themselves, which may not be a good experience.

@tianyufeng925
Copy link
Author

I noticed the usage example you provided. It seems that the existing ReadListener is sufficient。

BTW, the new utility class must be called by the user themselves, which may not be a good experience.

Hi@delei! Thanks for the feedback! 👋

I appreciate you taking the time to review the PR. Let me address your concerns:

Regarding ReadListener sufficiency

You're absolutely right thatReadListener can handle validation logic. However, there are several advantages to a standardized approach:

Current approach with ReadListener:

publicclassProjectDataListenerextendsAnalysisEventListener<ProjectData> {@Overridepublicvoidinvoke(ProjectDatadata,AnalysisContextcontext) {// Manual validation logic for each fieldif (data.getProjectName() ==null ||data.getProjectName().trim().isEmpty()) {// Handle error - but how? Where to collect errors?        }if (data.getBudget() ==null) {// More manual validation...        }// Repeat for every required field, in every listener, for every project    }}

With ExcelValidationUtils:

@DatapublicclassProjectData {@ExcelProperty("Project Name")@ExcelRequired(message ="Project name is required")privateStringprojectName;@ExcelProperty("Budget")@ExcelRequired(message ="Budget cannot be empty")privateBigDecimalbudget;}// Simple, reusable, consistent validationList<ValidationResult>results =ExcelValidationUtils.validateRequiredBatch(dataList);

Benefits of the standardized approach:

  1. Consistency - Same validation logic across all projects
  2. Reusability - Write once, use everywhere
  3. Maintainability - Centralized validation logic
  4. Error handling - Standardized error collection and reporting
  5. Testing - Easy to unit test validation rules
  6. Documentation - Self-documenting through annotations

Regarding user experience

I understand the concern about manual calls, but consider:

Current reality:

  • Users already write custom ReadListener implementations
  • Each listener contains repetitive validation code
  • Error handling is inconsistent across projects
  • No standard way to collect and report validation errors

With this utility:

  • One-time setup with annotations
  • Consistent error reporting format
  • Reusable across multiple Excel import scenarios
  • Foundation for future enhancements (auto-validation integration)

Future vision

This is actuallyPhase 1 of a larger validation system. Future phases could include:

  • Automatic validation integration (no manual calls needed)
  • Write-time validation (dropdown, numeric, date constraints)
  • Seamless integration with existing FastExcel workflows

Real-world example

Consider a company with 10 different Excel import features.

Currently:

  • 10 different ReadListener implementations
  • 10 different validation approaches
  • Inconsistent error messages
  • Difficult to maintain and test

With ExcelValidationUtils:

  • Consistent validation across all imports
  • Standardized error reporting
  • Easy to add new validation rules
  • Much easier to maintain and test

Question for you

Have you worked on projects with multiple Excel import features? How do you currently handle validation consistency across different imports?

I'd love to hear your thoughts on how we could make the user experience even better while maintaining the benefits of standardization!


Note: This utility is designed to complement, not replace, ReadListener. Developers can still use custom listeners for complex business logic while leveraging standardized validation for common scenarios.

@psxjoy
Copy link
Member

I need to re-emphasize: anyone (including project maintainers, committers) who wants to add a new feature needs to discuss it in issue or disscusion, and only then can we confirm who to assign it to, and the subsequent release schedule.
We welcome any contributors, but will not merge features thathave not been discussed and confirmed without discussion.

@721806280
Copy link
Contributor

It works even better in combination with hibernate-validator

delei reacted with eyes emoji

@psxjoy
Copy link
Member

FYI: We're not opposed to the use of AI, but would like to keep things as simple as possible when communicating

@psxjoypsxjoy added the discussion welcomeWelcome to join the discussion together labelJul 31, 2025
@delei
Copy link
Member

Verification features are indeed what we need. The community is willing to listen to more suggestions and also needs to understand the specific implementation ideas.

@tianyufeng925
Copy link
Author

I need to re-emphasize: anyone (including project maintainers, committers) who wants to add a new feature needs to discuss it in issue or disscusion, and only then can we confirm who to assign it to, and the subsequent release schedule. We welcome any contributors, but will not merge features thathave not been discussed and confirmed without discussion.

Got it, thanks for clarifying the process.

I actually created a discussion thread about the broader validation features right after submitting this PR. This current PR is just a small utility class - nothing fancy, just basic required field validation.

Should I wait for the discussion to conclude before this gets reviewed, or can this small utility be evaluated on its own? It's pretty minimal - just one class and one annotation.

Also, I see@wangmiscoding has PR#282 on similar stuff. Should we coordinate somehow?

Happy to follow whatever process works best for the project.

@psxjoy
Copy link
Member

我需要再次强调:任何想要添加新功能的人(包括项目维护者、提交者)都需要在 issue 或 disscusion 中进行讨论,只有这样我们才能确认将其分配给谁,以及后续的发布时间表。我们欢迎任何贡献者,但不会合并未经讨论而未经讨论和确认的功能。

Got it, thanks for clarifying the process.

I actually created a discussion thread about the broader validation features right after submitting this PR. This current PR is just a small utility class - nothing fancy, just basic required field validation.

Should I wait for the discussion to conclude before this gets reviewed, or can this small utility be evaluated on its own? It's pretty minimal - just one class and one annotation.

Also, I see@wangmiscoding has PR#282 on similar stuff. Should we coordinate somehow?

Happy to follow whatever process works best for the project.

Probably won't be able to merge in 1.3.0 for a while (since we were planning to release it in the near future)
We'll try to discuss a follow-up plan after the release.

Don't worry about not conforming to the process, we welcome any contributors and are willing to trust them, trust is built gradually in the community :)

@tianyufeng925
Copy link
Author

我需要再次强调:任何想要添加新功能的人(包括项目维护者、提交者)都需要在 issue 或 disscusion 中进行讨论,只有这样我们才能确认将其分配给谁,以及后续的发布时间表。我们欢迎任何贡献者,但不会合并未经讨论而未经讨论和确认的功能。

Got it, thanks for clarifying the process.
I actually created a discussion thread about the broader validation features right after submitting this PR. This current PR is just a small utility class - nothing fancy, just basic required field validation.
Should I wait for the discussion to conclude before this gets reviewed, or can this small utility be evaluated on its own? It's pretty minimal - just one class and one annotation.
Also, I see@wangmiscoding has PR#282 on similar stuff. Should we coordinate somehow?
Happy to follow whatever process works best for the project.

Probably won't be able to merge in 1.3.0 for a while (since we were planning to release it in the near future) We'll try to discuss a follow-up plan after the release.

Don't worry about not conforming to the process, we welcome any contributors and are willing to trust them, trust is built gradually in the community :)

Thanks@psxjoy! I really appreciate the welcoming attitude and the transparency about the release timeline.

I'm totally fine with waiting until after 1.3.0 - gives us more time to refine the approach through community discussion.

Looking forward to building that trust gradually and contributing meaningfully to FastExcel! 🙂

delei reacted with thumbs up emoji

@psxjoypsxjoy added enhancementNew feature or request not nowGood idea, but not currently part of plans labelsAug 7, 2025
@deleidelei removed enhancementNew feature or request discussion welcomeWelcome to join the discussion together labelsOct 1, 2025
Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment

Reviewers

@jipengfei-jpfjipengfei-jpfAwaiting requested review from jipengfei-jpf

@psxjoypsxjoyAwaiting requested review from psxjoy

At least 1 approving review is required to merge this pull request.

Assignees

No one assigned

Labels

not nowGood idea, but not currently part of plans

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

4 participants

@tianyufeng925@delei@psxjoy@721806280

[8]ページ先頭

©2009-2025 Movatter.jp