-
-
Notifications
You must be signed in to change notification settings - Fork 4
Cookbook and HowTo new validation system #111
Hello Tunay.
I'm back 😀 I was a bit ill. I saw that you've been very busy, respect.
I would like to use the "new" validation engine, but I find the description in the cookbook very "vague".
In the example code, i can see that you include rustapi_macros::Validate and use rustapi_validate::v2::{ValidationContext, RuleError}, but under "Troubleshooting: Common Gotchas/Use rustapi_rs, Not Internal Crates" in the cookbook that the internal crates should not be imported. That is totally confusing.
Do you have a working example somewhere?
All reactions
-
👀 1
Replies: 2 comments 21 replies
Hey! Welcome back, and sorry to hear you were ill. Hope you’re feeling better 🙏
Great question! you’re right that this is currently confusing.
Rule of thumb:
- For app-facing API, use
rustapi_rs(use rustapi_rs::prelude::*). - For the new validation derive, use
#[derive(rustapi_macros::Validate)](or#[derive(rustapi_rs::Validate)]if exposed in your setup). - For
custom_asyncfunction signatures, you currently still needrustapi_validate::v2::{ValidationContext, RuleError}.
So "don’t import internal crates" is the general guidance, but validation v2 custom async is an exception right now.
Working examples in repo:
crates/rustapi-rs/tests/validation_tests.rscrates/rustapi-validate/tests/custom_async.rs
Minimal working async example:
use rustapi_rs::prelude::*; use rustapi_macros::Validate; use rustapi_validate::v2::{ValidationContext, RuleError}; #[derive(Debug, Deserialize, Serialize, Validate)] struct CreateUserRequest { #[validate(custom_async = "check_username")] username: String, } async fn check_username(v: &String, _ctx: &ValidationContext) -> Result<(), RuleError> { if v == "admin" { Err(RuleError::new("reserved", "This username is reserved")) } else { Ok(()) } }
All reactions
Ok, will try it later. At the moment i update my client source.
Should i add rustapi-validate and rustapi-macros to the dependencies in cargo.toml?
All reactions
-
🚀 1
If you use the facade style (rustapi_rs), keep only:
rustapi-rs in Cargo.toml
imports from rustapi_rs::prelude::*
derive as #[derive(rustapi_rs::Validate)] (or via prelude patterns)
Add rustapi-macros`` **only if** you explicitly write #[derive(rustapi_macros::Validate)].`
Add rustapi-validate only if you explicitly reference v2 types like rustapi_validate::v2::{ValidationContext, RuleError} (common in custom_async signatures right now).
So for a clean client API surface: start with just rustapi-rs add the others only when compiler asks for those direct paths.
All reactions
By the way, I'm so glad you're back. I was bored without you <3
All reactions
Hey Tunay.
I have add rustapi_rs::Validate to my struct.
Now i get the following error:
cargo build
Compiling backend v0.1.0 (/Users/peter/Develop/rust/pg-social-backend)
error[E0433]: failed to resolve: could not find `rustapi_validate` in the list of imported crates
--> src/features/pins/request.rs:5:47
|
5 | #[derive(Debug, Deserialize, Schema, Default, rustapi_rs::Validate)]
| ^^^^^^^^^^^^^^^^^^^^ could not find `rustapi_validate` in the list of imported crates
|
= note: this error originates in the derive macro `rustapi_rs::Validate` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0433]: failed to resolve: could not find `rustapi_core` in the list of imported crates
--> src/features/pins/request.rs:5:47
|
5 | #[derive(Debug, Deserialize, Schema, Default, rustapi_rs::Validate)]
| ^^^^^^^^^^^^^^^^^^^^ could not find `rustapi_core` in the list of imported crates
|
= note: this error originates in the derive macro `rustapi_rs::Validate` (in Nightly builds, run with -Z macro-backtrace for more info)
error[E0433]: failed to resolve: could not find `rustapi_core` in the list of imported crates
--> src/features/pins/request.rs:5:47
|
5 | #[derive(Debug, Deserialize, Schema, Default, rustapi_rs::Validate)]
| ^^^^^^^^^^^^^^^^^^^^ could not find `rustapi_core` in the list of imported crates
|
= note: this error originates in the derive macro `rustapi_rs::Validate` (in Nightly builds, run with -Z macro-backtrace for more info)
Otherwise, I haven't changed anything.
All reactions
-
👀 1
Have updated to latest versions 0.1.412
All reactions
All reactions
Just open the create pin panel, then the error occurs
You don't need to apologize, we are software developers.
All reactions
-
❤️ 1
Can you try new update "0.1.417"
All reactions
Now it works like a charme. Nice. Thank you.
All reactions
-
❤️ 1