Repository navigation
Conditional Validation Based on Enum Values
๐น 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_nois required only whencontext.typeis"organization".
โ Support for Multiple Conditions
- You can define multiple dependent fields in a single
whentag. - Example:
๐น
type Business struct { VATNumber *string `json:"vat_number" when:"context.type=business;context.country=EU;binding=required"` }vat_numberis required only whencontext.type=businessANDcontext.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! ๐
๐น 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_nois required only whencontext.typeis"organization".
โ Support for Multiple Conditions
- You can define multiple dependent fields in a single
whentag. - Example:
๐น
type Business struct { VATNumber *string `json:"vat_number" when:"context.type=business;context.country=EU;binding=required"` }
vat_numberis required only whencontext.type=businessANDcontext.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/godanticThen, 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! ๐