Skip to content

Commit

Permalink
Add notOneOf (#399)
Browse files Browse the repository at this point in the history
closes gh-383
  • Loading branch information
making committed Jun 18, 2024
1 parent cbb973b commit 9a525ad
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/main/java/am/ik/yavi/core/Constraint.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_EQUAL_TO;
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_IS_NULL;
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_NOT_NULL;
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_NOT_ONE_OF;
import static am.ik.yavi.core.ViolationMessage.Default.OBJECT_ONE_OF;

public interface Constraint<T, V, C extends Constraint<T, V, C>> {
Expand Down Expand Up @@ -56,6 +57,16 @@ default C oneOf(Collection<V> values) {
return this.cast();
}

/**
* @since 0.14.1
*/
default C notOneOf(Collection<V> values) {
Predicate<V> notOneOf = v -> !values.contains(v);
this.predicates().add(ConstraintPredicate.of(notOneOf, OBJECT_NOT_ONE_OF,
() -> new Object[] { values }, NullAs.VALID));
return this.cast();
}

default C message(String message) {
ConstraintPredicate<V> predicate = this.predicates().pollLast();
if (predicate == null) {
Expand Down
2 changes: 2 additions & 0 deletions src/main/java/am/ik/yavi/core/ViolationMessage.java
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ enum Default implements ViolationMessage {
OBJECT_IS_NULL("object.isNull", "\"{0}\" must be null"), //
OBJECT_EQUAL_TO("object.equalTo", "\"{0}\" must be equal to {1}"), //
OBJECT_ONE_OF("object.oneOf", "\"{0}\" must be one of the following values: {1}"), //
OBJECT_NOT_ONE_OF("object.notOneOf",
"\"{0}\" must not be one of the following values: {1}"), //
CONTAINER_NOT_EMPTY("container.notEmpty", "\"{0}\" must not be empty"), //
CONTAINER_LESS_THAN("container.lessThan",
"The size of \"{0}\" must be less than {1}. The given size is {2}"), //
Expand Down
58 changes: 58 additions & 0 deletions src/test/java/am/ik/yavi/core/Gh338Test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
/*
* Copyright (C) 2018-2024 Toshiaki Maki <makingx@gmail.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package am.ik.yavi.core;

import java.util.Arrays;

import am.ik.yavi.builder.ValidatorBuilder;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

public class Gh338Test {

static Validator<EntityUpdate> validator = ValidatorBuilder.<EntityUpdate> of()
.constraint(EntityUpdate::getMessageType, "messageType",
c -> c.notOneOf(Arrays.asList(-9999, -137)))
.build();

@Test
void valid() {
ConstraintViolations violations = validator.validate(new EntityUpdate(100));
assertThat(violations.isValid()).isTrue();
}

@Test
void invalid() {
ConstraintViolations violations = validator.validate(new EntityUpdate(-137));
assertThat(violations.isValid()).isFalse();
assertThat(violations.size()).isEqualTo(1);
assertThat(violations.get(0).message()).isEqualTo(
"\"messageType\" must not be one of the following values: [-9999, -137]");
}

static class EntityUpdate {
private final Integer messageType;

EntityUpdate(Integer messageType) {
this.messageType = messageType;
}

public Integer getMessageType() {
return messageType;
}
}
}

0 comments on commit 9a525ad

Please sign in to comment.