-
-
Notifications
You must be signed in to change notification settings - Fork 419
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added config validator for parameter options
Signed-off-by: Christoph Weitkamp <github@christophweitkamp.de>
- Loading branch information
Showing
7 changed files
with
104 additions
and
12 deletions.
There are no files selected for viewing
This file contains 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 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 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 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
41 changes: 41 additions & 0 deletions
41
...core/src/main/java/org/openhab/core/config/core/internal/validation/OptionsValidator.java
This file contains 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,41 @@ | ||
/** | ||
* Copyright (c) 2010-2022 Contributors to the openHAB project | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Eclipse Public License 2.0 which is available at | ||
* http://www.eclipse.org/legal/epl-2.0 | ||
* | ||
* SPDX-License-Identifier: EPL-2.0 | ||
*/ | ||
package org.openhab.core.config.core.internal.validation; | ||
|
||
import org.eclipse.jdt.annotation.NonNullByDefault; | ||
import org.eclipse.jdt.annotation.Nullable; | ||
import org.openhab.core.config.core.ConfigDescriptionParameter; | ||
import org.openhab.core.config.core.validation.ConfigValidationMessage; | ||
|
||
/** | ||
* The {@link ConfigDescriptionParameterValidator} for the options of a {@link ConfigDescriptionParameter}. | ||
* | ||
* @author Christoph Weitkamp - Initial contribution | ||
*/ | ||
@NonNullByDefault | ||
final class OptionsValidator implements ConfigDescriptionParameterValidator { | ||
|
||
@Override | ||
public @Nullable ConfigValidationMessage validate(ConfigDescriptionParameter param, @Nullable Object value) { | ||
if (value == null || !param.getLimitToOptions() || param.getOptions().isEmpty()) { | ||
return null; | ||
} | ||
|
||
// Option values are a string, so we can do a simple compare | ||
if (!param.getOptions().stream().map(o -> o.getValue()).anyMatch(v -> v.equals(value.toString()))) { | ||
MessageKey messageKey = MessageKey.OPTIONS_VIOLATED; | ||
return new ConfigValidationMessage(param.getName(), messageKey.defaultMessage, messageKey.key); | ||
} | ||
return null; | ||
} | ||
} |
This file contains 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 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