Skip to content

Navigation Menu

Sign in
Sign up

Cookbook and HowTo new validation system #111

PeterGumball started this conversation in General
Discussion options

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?

You must be logged in to vote

Replies: 2 comments 21 replies

Comment options

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_async function signatures, you currently still need rustapi_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.rs
  • crates/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(())
 }
}
You must be logged in to vote
3 replies
Comment options

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?

Comment options

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.

Comment options

By the way, I'm so glad you're back. I was bored without you <3

Comment options

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.

You must be logged in to vote
18 replies
Comment options

Have updated to latest versions 0.1.412

Comment options

image It's interesting that I don't see a problem. I'm continuing to investigate. I'm sorry for the issue you're experiencing.
Comment options

Just open the create pin panel, then the error occurs

You don't need to apologize, we are software developers.

Comment options

Can you try new update "0.1.417"

Comment options

Now it works like a charme. Nice. Thank you.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation

AltStyle によって変換されたページ (->オリジナル) /