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

JakartaValidationPlugin을 사용하는 경우에, 유효하지 않은 값으로 커스터마이징 할 수 있나요? #966

Closed
kiwonseo opened this issue Apr 4, 2024 · 2 comments
Labels
question Further information is requested

Comments

@kiwonseo
Copy link

kiwonseo commented Apr 4, 2024

Describe your question

공식문서에 존재하는 Product의 예시에서,

@Value
public class Product {
    @Min(1)
    long id;

    @NotBlank
    String productName;

    @Max(100000)
    long price;

    @Size(min = 3)
    List<@NotBlank String> options;

    @Past
    Instant createdAt;
}

아래와 같이 @Min(1)을 위반하는 값으로 커스터마이징 불가능 한 것으로 보입니다.

@Test
void test() {
    // given
    FixtureMonkey fixtureMonkey = FixtureMonkey.builder()
            .objectIntrospector(ConstructorPropertiesArbitraryIntrospector.INSTANCE)
            .plugin(new JakartaValidationPlugin())
            .build();

    // when
    Product actual = fixtureMonkey.giveMeBuilder(Product.class)
            .set("id", -1L) // IllegalArgumentException expected
            .sample();
}

혹시 커스터마이징 하는 프로퍼티에 대해서만 Validation을 무시하는 다른 API를 제공하고 있나요?

@kiwonseo kiwonseo added the question Further information is requested label Apr 4, 2024
@flex-myeonghyeon
Copy link

@kiwonseo 아래처럼 validOnly(false) 로 지정하면 생성된 객체 jakarata validation 을 실행하지 않고 skip 합니다

Product actual = fixtureMonkey.giveMeBuilder(Product.class)
            .set("id", -1L) // IllegalArgumentException expected
            .validOnly(false)
            .sample();
}

의도적으로 Validation 을 통과하지 못하는 객체를 만들때 사용하시면 됩니다

@kiwonseo
Copy link
Author

kiwonseo commented Apr 4, 2024

답변 감사드립니다!

@kiwonseo kiwonseo closed this as completed Apr 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants