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

Custom extra validation on ParseFromJSON #918

Open
Shadow53 opened this issue Nov 26, 2024 · 0 comments
Open

Custom extra validation on ParseFromJSON #918

Shadow53 opened this issue Nov 26, 2024 · 0 comments
Labels
enhancement New feature or request

Comments

@Shadow53
Copy link

Description of the feature

I'd like to be able to include extra validation beyond what's supported by OpenAPI/#[oai(validator)] as part of ParseFromJSON so that I can be sure validation always runs on received data.

I can see a few ways it could be implemented, including allowing mutation of the parsed object.

See also related issues in serde:

Code example (if possible)

#[derive(poem_openapi::Object)]
#[oai(finalize = finalize_foo)]
struct Foo {
    #[oai(validator(unique_items = true))]
    sorted: Vec<usize>,
}

// Alternatively, a signature of fn(Foo) -> ParseResult<Foo>
fn finalize_foo(parsed: &Foo) -> poem_openapi::types::ParseResult<()> {
    if !parsed.sorted.is_sorted() {
        return Err(ParseError::custom("`sorted` must be sorted"));
    }

    Ok(())
}

I'm open to providing a PR for this if the feature would be accepted.

@Shadow53 Shadow53 added the enhancement New feature or request label Nov 26, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

1 participant