Skip to content
This repository was archived by the owner on Jul 22, 2026. It is now read-only.

Conditional Validation Based on Enum Values

Choose a tag to compare

@GraHms GraHms released this 08 Mar 07:00
· 15 commits to main since this release
61576aa

๐Ÿ”น Godantic Release Notes - Version 1.3.0

๐Ÿš€ New Feature: Conditional Validation Based on Enum Values ๐Ÿš€

๐Ÿ”น What's New?

โœ… Conditional Field Validation Using the when Tag

  • Fields can now be validated only when another field has a specific value.
  • Example:
    type User struct {
        RegNo *string `json:"reg_no" when:"context.type=organization;binding=required"`
    }
    
    ๐Ÿ”น reg_no is required only when context.type is "organization".

โœ… Support for Multiple Conditions

  • You can define multiple dependent fields in a single when tag.
  • Example:
    type Business struct {
        VATNumber *string `json:"vat_number" when:"context.type=business;context.country=EU;binding=required"`
    }
    
    ๐Ÿ”น vat_number is required only when context.type=business AND context.country=EU.

โœ… Operator Support
Currently, the = operator is supported for exact matches:

Operator Example Meaning
= context.type=business Field must be equal to value

๐Ÿ”ง Improvements & Fixes

โœ… Optimized validation performance with precomputed enum value maps.
โœ… Fixed = prefix issue in parseCondition(), ensuring values are extracted correctly.
โœ… Clearer error messages when a required field is missing due to a condition.


๐Ÿ“Œ Example Usage

Valid JSON Input

{
  "context": { "type": "organization" },
  "user": { "reg_no": "56789" }
}

โœ… Passes validation because reg_no is provided for organization.

Invalid JSON Input

{
  "context": { "type": "organization" },
  "user": {}
}

โŒ Fails validation with:

Field <user.reg_no> is required when context.type=organization

๐Ÿ“ข Upgrade Guide

To upgrade your project to this version, update godantic using:

go get -u github.com/grahms/godantic

Then, update your structs to use the new when tag for conditional validation.


๐Ÿ”ฎ What's Next?

๐ŸŸข Support for additional operators (!=, >, <, >=, <=)
๐ŸŸข More binding options (regex, ignore, default)
๐ŸŸข Better structured error reporting


๐Ÿ’ก Feedback & Contributions

Have suggestions or improvements? Open an issue or PR on GitHub.

๐Ÿš€ Enjoy better validation with godantic! ๐Ÿš€

### **๐Ÿ”น Godantic Release Notes - Version X.X.X** ๐Ÿš€ **New Feature: Conditional Validation Based on Enum Values** ๐Ÿš€

๐Ÿ”น What's New?

โœ… Conditional Field Validation Using the when Tag

  • Fields can now be validated only when another field has a specific value.
  • Example:
    type User struct {
        RegNo *string `json:"reg_no" when:"context.type=organization;binding=required"`
    }
    ๐Ÿ”น reg_no is required only when context.type is "organization".

โœ… Support for Multiple Conditions

  • You can define multiple dependent fields in a single when tag.
  • Example:
    type Business struct {
        VATNumber *string `json:"vat_number" when:"context.type=business;context.country=EU;binding=required"`
    }
    ๐Ÿ”น vat_number is required only when context.type=business AND context.country=EU.

โœ… Operator Support
Currently, the = operator is supported for exact matches:

Operator Example Meaning
= context.type=business Field must be equal to value

๐Ÿ”ง Improvements & Fixes

โœ… Optimized validation performance with precomputed enum value maps.

โœ… Clearer error messages when a required field is missing due to a condition.


๐Ÿ“Œ Example Usage

Valid JSON Input

{
  "context": { "type": "organization" },
  "user": { "reg_no": "56789" }
}

โœ… Passes validation because reg_no is provided for organization.

Invalid JSON Input

{
  "context": { "type": "organization" },
  "user": {}
}

โŒ Fails validation with:

Field <user.reg_no> is required when context.type=organization

๐Ÿ“ข Upgrade Guide

To upgrade your project to this version, update godantic using:

go get -u github.com/grahms/godantic

Then, update your structs to use the new when tag for conditional validation.


๐Ÿ”ฎ What's Next?

๐ŸŸข Support for additional operators (!=, >, <, >=, <=)
๐ŸŸข More binding options (regex, ignore, default)
๐ŸŸข Better structured error reporting


๐Ÿ’ก Feedback & Contributions

Have suggestions or improvements? Open an issue or PR on [GitHub](https://github.com/grahms/godantic).

๐Ÿš€ Enjoy better validation with godantic! ๐Ÿš€