-
Notifications
You must be signed in to change notification settings - Fork 12
Add Enum and Boolean fields validators #190
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
43cdcd1
Add Enum and Boolean fields validators
dmytro-kashcheiev 5c04edf
Review updates, for Enum and Boolean fields validators
dmytro-kashcheiev 8ce62c0
Merge branch 'master' into vallidators-enum-boolean
dmytro-kashcheiev 757c1d0
Update javadoc for BooleanFieldValidator
dmytro-kashcheiev b934d1a
Merge branch 'vallidators-enum-boolean' of https://github.com/SpineEv…
dmytro-kashcheiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
77 changes: 77 additions & 0 deletions
77
server/src/main/java/org/spine3/server/validate/BooleanFieldValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /* | ||
| * Copyright 2016, TeamDev Ltd. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and/or binary forms, with or without | ||
| * modification, must retain the above copyright notice and the following | ||
| * disclaimer. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package org.spine3.server.validate; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.protobuf.Descriptors; | ||
| import org.slf4j.Logger; | ||
| import org.slf4j.LoggerFactory; | ||
| import org.spine3.base.FieldPath; | ||
| import org.spine3.validate.ConstraintViolation; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Validates fields of type {@link Boolean}. | ||
| * | ||
| * @author Dmitry Kashcheiev | ||
| */ | ||
| /* package */ class BooleanFieldValidator extends FieldValidator<Boolean> { | ||
|
|
||
| /** | ||
| * Creates a new validator instance. | ||
| * | ||
| * @param descriptor a descriptor of the field to validate | ||
| * @param fieldValues values to validate | ||
| * @param rootFieldPath a path to the root field (if present) | ||
| */ | ||
| /*package*/ BooleanFieldValidator(Descriptors.FieldDescriptor descriptor, ImmutableList<Boolean> fieldValues, FieldPath rootFieldPath) { | ||
| super(descriptor, fieldValues, rootFieldPath, false); | ||
| } | ||
|
|
||
| /** | ||
| * In Protobuf there is no way to tell if the value is {@code false} or was not set. | ||
| * | ||
| * @return false | ||
| */ | ||
| @Override | ||
| protected boolean isValueNotSet(Boolean value) { | ||
| return false; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Please test this. Do you have a Codecov browser plug-in installed?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. done |
||
| } | ||
|
|
||
| @Override | ||
| protected List<ConstraintViolation> validate() { | ||
| if (isRequiredField()) { | ||
| log().warn("'required' option not allowed for boolean field"); | ||
| } | ||
| return super.validate(); | ||
| } | ||
|
|
||
| private enum LogSingleton { | ||
| INSTANCE; | ||
| @SuppressWarnings("NonSerializableFieldInSerializableClass") | ||
| private final Logger value = LoggerFactory.getLogger(BooleanFieldValidator.class); | ||
| } | ||
|
|
||
| private static Logger log() { | ||
| return LogSingleton.INSTANCE.value; | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
61 changes: 61 additions & 0 deletions
61
server/src/main/java/org/spine3/server/validate/EnumFieldValidator.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,61 @@ | ||
| /* | ||
| * Copyright 2016, TeamDev Ltd. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and/or binary forms, with or without | ||
| * modification, must retain the above copyright notice and the following | ||
| * disclaimer. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package org.spine3.server.validate; | ||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.protobuf.Descriptors; | ||
| import org.spine3.base.FieldPath; | ||
| import org.spine3.validate.ConstraintViolation; | ||
|
|
||
| import java.util.List; | ||
|
|
||
| /** | ||
| * Validates fields of type {@link Descriptors.EnumValueDescriptor}. | ||
| * | ||
| * @author Dmitry Kashcheiev | ||
| */ | ||
| /* package */ class EnumFieldValidator extends FieldValidator<Descriptors.EnumValueDescriptor> { | ||
|
|
||
| /** | ||
| * Creates a new validator instance. | ||
| * | ||
| * @param descriptor a descriptor of the field to validate | ||
| * @param fieldValues values to validate | ||
| * @param rootFieldPath a path to the root field (if present) | ||
| */ | ||
| /*package*/ EnumFieldValidator(Descriptors.FieldDescriptor descriptor, ImmutableList<Descriptors.EnumValueDescriptor> fieldValues, FieldPath rootFieldPath) { | ||
| super(descriptor, fieldValues, rootFieldPath, false); | ||
| } | ||
|
|
||
| @Override | ||
| protected boolean isValueNotSet(Descriptors.EnumValueDescriptor value) { | ||
| final int intValue = value.getNumber(); | ||
| final boolean result = intValue == 0; | ||
| return result; | ||
| } | ||
|
|
||
| @Override | ||
| protected List<ConstraintViolation> validate() { | ||
| checkIfRequiredAndNotSet(); | ||
| final List<ConstraintViolation> violations = super.validate(); | ||
| return violations; | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
43 changes: 43 additions & 0 deletions
43
server/src/test/java/org/spine3/server/validate/BooleanFieldValidatorShould.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| /* | ||
| * Copyright 2016, TeamDev Ltd. All rights reserved. | ||
| * | ||
| * Redistribution and use in source and/or binary forms, with or without | ||
| * modification, must retain the above copyright notice and the following | ||
| * disclaimer. | ||
| * | ||
| * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS | ||
| * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT | ||
| * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR | ||
| * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT | ||
| * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, | ||
| * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT | ||
| * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, | ||
| * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY | ||
| * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | ||
| * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | ||
| * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | ||
| */ | ||
|
|
||
| package org.spine3.server.validate; | ||
|
|
||
|
|
||
| import com.google.common.collect.ImmutableList; | ||
| import com.google.protobuf.Any; | ||
| import org.junit.Test; | ||
| import org.spine3.base.FieldPath; | ||
|
|
||
| import static org.junit.Assert.assertFalse; | ||
|
|
||
| /** | ||
| * @author dmitry.kashcheiev | ||
| */ | ||
| public class BooleanFieldValidatorShould { | ||
|
|
||
| private final BooleanFieldValidator validator = | ||
| new BooleanFieldValidator(Any.getDescriptor().getFields().get(0), ImmutableList.of(false), FieldPath.getDefaultInstance()); | ||
|
|
||
| @Test | ||
| public void convert_string_to_number() { | ||
| assertFalse(validator.isValueNotSet(false)); | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please remove excessive empty line.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done