From 19f1ffc27cf6fbc6b630d0d32a51ec6b8923527e Mon Sep 17 00:00:00 2001 From: alexander-yevsyukov Date: Wed, 11 Mar 2026 21:31:31 +0000 Subject: [PATCH 1/4] Bump Version -> `2.0.0-SNAPSHOT.403` --- version.gradle.kts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/version.gradle.kts b/version.gradle.kts index 53c8ff9dd9..3ea203f6a1 100644 --- a/version.gradle.kts +++ b/version.gradle.kts @@ -29,4 +29,4 @@ * * For Spine-based dependencies please see [io.spine.dependency.local.Spine]. */ -val validationVersion by extra("2.0.0-SNAPSHOT.402") +val validationVersion by extra("2.0.0-SNAPSHOT.403") From 52e3c45128b2cb60d517a573ef3d0f60b5ace43d Mon Sep 17 00:00:00 2001 From: alexander-yevsyukov Date: Wed, 11 Mar 2026 21:31:42 +0000 Subject: [PATCH 2/4] Update dependency reports --- dependencies.md | 56 ++++++++++++++++++++++++------------------------- pom.xml | 2 +- 2 files changed, 29 insertions(+), 29 deletions(-) diff --git a/dependencies.md b/dependencies.md index cb14dd4906..c5ca424b08 100644 --- a/dependencies.md +++ b/dependencies.md @@ -1,6 +1,6 @@ -# Dependencies of `io.spine.tools:validation-context:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-context:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.20.0. @@ -1139,14 +1139,14 @@ The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-context-tests:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-context-tests:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.20.0. @@ -1743,7 +1743,7 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). @@ -1764,7 +1764,7 @@ This report was generated on **Fri Mar 06 18:16:01 WET 2026** using -# Dependencies of `io.spine.tools:validation-gradle-plugin:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-gradle-plugin:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.20.0. @@ -2853,14 +2853,14 @@ This report was generated on **Fri Mar 06 18:16:01 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-java:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-java:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.20.0. @@ -3939,14 +3939,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-java-bundle:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-java-bundle:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : org.jetbrains. **Name** : annotations. **Version** : 13.0. @@ -3993,14 +3993,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:36 WET 2026** using +This report was generated on **Wed Mar 11 21:28:45 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine:validation-jvm-runtime:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine:validation-jvm-runtime:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2. @@ -4833,14 +4833,14 @@ This report was generated on **Tue Mar 10 16:36:36 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-consumer:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-consumer:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.20.0. @@ -5423,14 +5423,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-consumer-dependency:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-consumer-dependency:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2. @@ -5941,14 +5941,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-extensions:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-extensions:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.20.0. @@ -6628,14 +6628,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-runtime:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-runtime:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2. @@ -7257,14 +7257,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-validating:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-validating:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2. @@ -7929,14 +7929,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-validator:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-validator:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.fasterxml.jackson. **Name** : jackson-bom. **Version** : 2.20.0. @@ -8719,14 +8719,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-validator-dependency:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-validator-dependency:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2. @@ -8996,14 +8996,14 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). -# Dependencies of `io.spine.tools:validation-vanilla:2.0.0-SNAPSHOT.402` +# Dependencies of `io.spine.tools:validation-vanilla:2.0.0-SNAPSHOT.403` ## Runtime 1. **Group** : com.google.code.findbugs. **Name** : jsr305. **Version** : 3.0.2. @@ -9346,6 +9346,6 @@ This report was generated on **Tue Mar 10 16:36:37 WET 2026** using The dependencies distributed under several licenses, are used according their commercial-use-friendly license. -This report was generated on **Tue Mar 10 16:36:37 WET 2026** using +This report was generated on **Wed Mar 11 21:28:46 WET 2026** using [Gradle-License-Report plugin](https://github.com/jk1/Gradle-License-Report) by Evgeny Naumenko, licensed under [Apache 2.0 License](https://github.com/jk1/Gradle-License-Report/blob/master/LICENSE). \ No newline at end of file diff --git a/pom.xml b/pom.xml index 9fd4c18b88..0f84885f88 100644 --- a/pom.xml +++ b/pom.xml @@ -10,7 +10,7 @@ all modules and does not describe the project structure per-subproject. --> io.spine.tools validation -2.0.0-SNAPSHOT.402 +2.0.0-SNAPSHOT.403 2015 From aeb829419f36b3af075c4c4a89fc8f7772718551 Mon Sep 17 00:00:00 2001 From: alexander-yevsyukov Date: Wed, 11 Mar 2026 21:31:49 +0000 Subject: [PATCH 3/4] Update `config` ref. --- config | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/config b/config index 17e0dbb819..0bca88448a 160000 --- a/config +++ b/config @@ -1 +1 @@ -Subproject commit 17e0dbb819839d9b65b711efb085b38bcbb5eae9 +Subproject commit 0bca88448a18cde81f83ead25dfbd36a364aa7c0 From 24c162d9e57e43b676ec03dff57b16fd56d6dfef Mon Sep 17 00:00:00 2001 From: alexander-yevsyukov Date: Wed, 11 Mar 2026 21:32:38 +0000 Subject: [PATCH 4/4] Partially restore `Required` option and its parent types Also: * Deprecate the restored types. --- .../validation/option/AlwaysRequired.java | 59 ++++++++ .../option/FieldValidatingOption.java | 116 ++++++++++++++++ .../io/spine/validation/option/Required.java | 128 ++++++++++++++++++ .../validation/option/ValidatingOption.java | 53 ++++++++ 4 files changed, 356 insertions(+) create mode 100644 jvm-runtime/src/main/java/io/spine/validation/option/AlwaysRequired.java create mode 100644 jvm-runtime/src/main/java/io/spine/validation/option/FieldValidatingOption.java create mode 100644 jvm-runtime/src/main/java/io/spine/validation/option/Required.java create mode 100644 jvm-runtime/src/main/java/io/spine/validation/option/ValidatingOption.java diff --git a/jvm-runtime/src/main/java/io/spine/validation/option/AlwaysRequired.java b/jvm-runtime/src/main/java/io/spine/validation/option/AlwaysRequired.java new file mode 100644 index 0000000000..8034d28279 --- /dev/null +++ b/jvm-runtime/src/main/java/io/spine/validation/option/AlwaysRequired.java @@ -0,0 +1,59 @@ +/* + * Copyright 2025, TeamDev. All rights reserved. + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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 io.spine.validation.option; + +import com.google.errorprone.annotations.Immutable; +import io.spine.code.proto.FieldContext; + +/** + * A special case of {@code Required} option that assumes that the option is present regardless + * of the actual field declaration. + * + * @deprecated Please use generated validation code. + */ +@Immutable +@Deprecated(forRemoval = true) +final class AlwaysRequired extends Required { + + /** + * Creates a new instance of this option. + */ + AlwaysRequired() { + super(); + } + + /** + * {@inheritDoc} + * + *

For {@code AlwaysRequired}, validation happens every time. + */ + @Override + public boolean shouldValidate(FieldContext context) { + checkUsage(context.targetDeclaration()); + return true; + } +} diff --git a/jvm-runtime/src/main/java/io/spine/validation/option/FieldValidatingOption.java b/jvm-runtime/src/main/java/io/spine/validation/option/FieldValidatingOption.java new file mode 100644 index 0000000000..42fd830498 --- /dev/null +++ b/jvm-runtime/src/main/java/io/spine/validation/option/FieldValidatingOption.java @@ -0,0 +1,116 @@ +/* + * Copyright 2025, TeamDev. All rights reserved. + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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 io.spine.validation.option; + +import com.google.errorprone.annotations.Immutable; +import com.google.errorprone.annotations.ImmutableTypeParameter; +import com.google.protobuf.DescriptorProtos.FieldOptions; +import com.google.protobuf.Descriptors.FieldDescriptor; +import com.google.protobuf.GeneratedMessage.GeneratedExtension; +import io.spine.code.proto.FieldContext; +import io.spine.code.proto.FieldOption; + +import java.util.Optional; + +import static java.lang.String.format; + +/** + * An option that validates a field. + * + *

Validating options impose constraint on fields that they are applied to. + * + * @param + * type of value held by this option + * @deprecated Please use generated validation code. + */ +@Immutable +@Deprecated(forRemoval = true) +public abstract class FieldValidatingOption<@ImmutableTypeParameter T> + extends FieldOption + implements ValidatingOption { + + /** Specifies the extension that corresponds to this option. */ + protected FieldValidatingOption(GeneratedExtension optionExtension) { + super(optionExtension); + } + + /** + * Returns a value of the option. + * + * @apiNote Should only be called by subclasses in circumstances that assume presence of + * the option. For all other cases please refer to + * {@link #valueFrom(FieldContext)}. + */ + protected T optionValue(FieldContext field) throws IllegalStateException { + var option = valueFrom(field); + return option.orElseThrow(() -> { + var descriptor = extension().getDescriptor(); + + var fieldName = field.targetDeclaration() + .name() + .value(); + var containingTypeName = descriptor.getContainingType() + .getName(); + return couldNotGetOptionValueFrom(fieldName, containingTypeName); + }); + } + + private IllegalStateException couldNotGetOptionValueFrom(String fieldName, + String containingTypeName) { + var optionName = extension().getDescriptor().getName(); + var message = format("Could not get value of option %s from field %s in message %s.", + optionName, + fieldName, + containingTypeName); + return new IllegalStateException(message); + } + + /** + * Takes the value of the option from the given descriptor, given the specified context. + * + * @param context + * context of the field + * @return an {@code Optional} with an option value if such exists, otherwise an empty + * {@code Optional} + * @apiNote Use this in favour of {@link + * FieldOption#optionsFrom(com.google.protobuf.Descriptors.FieldDescriptor) + * optionsFrom(FieldDescriptor)} when {@code FieldContext} matters. + */ + public Optional valueFrom(FieldContext context) { + return valueFrom(context.target()); + } + + /** + * Returns {@code true} if this option exists for the specified field, {@code false} otherwise. + * + * @param context + * the value to be validated + */ + public boolean shouldValidate(FieldContext context) { + return valueFrom(context).isPresent(); + } +} diff --git a/jvm-runtime/src/main/java/io/spine/validation/option/Required.java b/jvm-runtime/src/main/java/io/spine/validation/option/Required.java new file mode 100644 index 0000000000..163f05a213 --- /dev/null +++ b/jvm-runtime/src/main/java/io/spine/validation/option/Required.java @@ -0,0 +1,128 @@ +/* + * Copyright 2025, TeamDev. All rights reserved. + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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 io.spine.validation.option; + +import com.google.common.collect.ImmutableSet; +import com.google.errorprone.annotations.Immutable; +import com.google.protobuf.Descriptors.FieldDescriptor.JavaType; +import io.spine.base.CommandMessage; +import io.spine.base.EntityState; +import io.spine.code.proto.FieldContext; +import io.spine.code.proto.FieldDeclaration; +import io.spine.option.OptionsProto; + +import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.BYTE_STRING; +import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.ENUM; +import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.MESSAGE; +import static com.google.protobuf.Descriptors.FieldDescriptor.JavaType.STRING; + +/** + * An option that makes a field {@code required}. + * + *

If a {@code required} field is missing, an error is produced. + * + * @deprecated Please use generated validation code. + */ +@Immutable +@Deprecated(forRemoval = true) +public class Required extends FieldValidatingOption { + + static final ImmutableSet CAN_BE_REQUIRED = ImmutableSet.of( + MESSAGE, ENUM, STRING, BYTE_STRING + ); + + /** + * Creates a new instance of this option. + */ + Required() { + super(OptionsProto.required); + } + + /** + * Creates a new instance. + * + *

If the specified parameter is {@code true}, a returned option always assumes a field to + * be {@code required}, regardless of the field value. + * If the specified parameter is {@code false}, a returned option checks the actual value. + * + * @param strict + * specifies if a field is assumed to be a required one regardless of the actual + * Protobuf option value + * @return a new instance of the {@code Required} option + */ + public static Required create(boolean strict) { + return strict + ? new AlwaysRequired() + : new Required(); + } + + private boolean notAssumingRequired(FieldContext context) { + var defaultValue = context.targetDeclaration().isId(); + return valueFrom(context.target()) + .orElse(defaultValue); + } + + @Override + public boolean shouldValidate(FieldContext context) { + return notAssumingRequired(context); + } + + /** + * Produces warnings if the {@code required} option was applied incorrectly. + * + *

Examples of incorrect application include attempting to apply the option to a numeric + * field. + * + * @param field + * a value that the option is applied to + */ + @SuppressWarnings("UseOfSystemOutOrSystemErr") /* We're migrating off runtime validation + AND we do not want dependency of Validation Runtime on Spine Logging. + So we use `System.err` for the warnings. */ + void checkUsage(FieldDeclaration field) { + var type = field.javaType(); + if (!CAN_BE_REQUIRED.contains(type) && field.isNotCollection()) { + var isTheFirstField = field.descriptor().getIndex() == 0; + if (isTheFirstField) { + // The first field declared in a message type could be assumed as required + // because by convention it is an ID field of the message. + // If so, do not log the warning message for this field because ID fields + // could be of any reasonable type. + var messageClass = field.declaringType().javaClass(); + if (CommandMessage.class.isAssignableFrom(messageClass) + || EntityState.class.isAssignableFrom(messageClass)) { + return; + } + } + var typeName = field.descriptor().getType().name(); + System.err.printf( + "The field `%s.%s` has the type %s and" + + " should not be declared as `(required)`.%n", + field.declaringType().name(), field.name(), typeName); + } + } +} diff --git a/jvm-runtime/src/main/java/io/spine/validation/option/ValidatingOption.java b/jvm-runtime/src/main/java/io/spine/validation/option/ValidatingOption.java new file mode 100644 index 0000000000..e67cccedb0 --- /dev/null +++ b/jvm-runtime/src/main/java/io/spine/validation/option/ValidatingOption.java @@ -0,0 +1,53 @@ +/* + * Copyright 2025, TeamDev. All rights reserved. + * + * 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 + * + * https://www.apache.org/licenses/LICENSE-2.0 + * + * 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 io.spine.validation.option; + +import com.google.errorprone.annotations.Immutable; +import com.google.errorprone.annotations.ImmutableTypeParameter; +import com.google.protobuf.Descriptors.GenericDescriptor; +import io.spine.code.proto.Option; + +/** + * The interface common to all standard validation options. + * + * @param + * data type that this option holds, e.g., + * {@linkplain io.spine.option.OptionsProto#required required option} would + * hold a {@code Boolean} + * @param + * reflective container which describes the entity this option is applied to + * @param + * kind of entity that this option is applied to + * @deprecated Please use generated validation code. + */ +@Immutable +@Deprecated(forRemoval = true) +interface ValidatingOption<@ImmutableTypeParameter T, + @ImmutableTypeParameter C, + @ImmutableTypeParameter K extends GenericDescriptor> + extends Option { +}