Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Validation via #[config(validate)] #40

Merged
merged 9 commits into from
Oct 18, 2024
Merged

Add Validation via #[config(validate)] #40

merged 9 commits into from
Oct 18, 2024

Conversation

LukasKalbertodt
Copy link
Owner

@LukasKalbertodt LukasKalbertodt commented Oct 18, 2024

Adds the validate attribute that can be attached to structs or struct fields. Its syntax is validate = path::to::function where the function takes &T and returns Result<(), impl Display>. For struct fields, a second assert!-style syntax exists: validate(!name.is_empty(), "name must not be empty").

See the added docs for more information.

Closes #16

@LukasKalbertodt LukasKalbertodt changed the title Add Validation via #config(validate)] Add Validation via #[config(validate)] Oct 18, 2024
Previously, code for each method was generated by one iterator each,
which was nice since it avoided allocating memory. However, this meant
that each block had its own `match field.kind` basically, which lead to
lots of duplication and put code close to one another that's not really
related (dealing with nested fields and leaf fields). The new approach
does require allocating vectors (but that's really irrelevant in the
proc macro context), but has one function deal with one field at a time,
generating all relevant code for it. This co-locates related code and
should make certain changes much easier.
This was planned from the beginning, but was missing in the previous
commits. Validation should always run, regardless where the value comes
from.
@LukasKalbertodt LukasKalbertodt merged commit 24d9835 into main Oct 18, 2024
2 checks passed
@LukasKalbertodt LukasKalbertodt deleted the validation branch October 18, 2024 12:48
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.

Validation
1 participant