Custom fields with custom validation for reuse between structures.

Open
#239 8 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
35/100
Issue type
Feature
Clarity
Mostly clear
Activity status
Stale
Tech stack
rust
Domain
tooling

Research direction

No files or tests are named. Start by locating the validation derive handling for #[validate(customtype)] and the restriction on unnamed fields; use the LocaleName example to trace the intended behavior. Done means a single-field tuple struct can validate its inner value while existing named-field behavior remains intact, with coverage for the example.

Written by the indexing model from the issue text.

Description

Marshmallow has an option of defining custom field types that can be reused multiple times. This significantly reduces logic errors when you have to reuse the same field over and over again across the whole business logic.

Right now there is a limitation that structs annotated with #validate should have only named fields.

Is it possible to remove that limitation for a case when a structure has only one unnamed field and nothing else. And in this case validate the inner data instead?

Below is the code example that utilizes this feature: both UpdateLocaleRequest and GetLocaleResponse use the same type LocaleName which has a validation defined on the struct and propagated to fields that use this type by having #[validate(customtype)]

#[derive(Debug, Deserialize, Serialize, JsonSchema)]
#[validate(length(equal = 2))]
pub struct LocaleName(pub String);

#[derive(Serialize, Deserialize, JsonSchema, Validate)]
struct UpdateLocaleRequest {
	/// user_id is ignored. need for old api.
	user_id: Option<u64>,
	/// Locale name
	#[validate(customtype)]
	locale: LocaleName,
}

#[derive(Serialize, Deserialize, JsonSchema)]
struct GetLocaleResponse {
	/// Locales' names
	locales: Option<Vec<String>>,
	/// The unique identifier for the user.
	user_id: Option<u64>,
	/// Locale name
        #[validate(customtype)]
	locale: Option<LocaleName>,
}
Dominant language
Rust
Stars
2.5k
Forks
190
PR merge metrics
No merged PRs in 30d

Getting set up

This project ships no dev container, Dockerfile or contributing guide, so setting up is up to you: start from its README, and see our first-contribution guide for the general steps.

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from Keats/validator

All issues in Keats/validator

Similar issues

More Rust issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.