This repository has been archived by the owner on Apr 14, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 50
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(#1675): Update value data type validation checks
- Loading branch information
1 parent
1facc82
commit 670eb1d
Showing
8 changed files
with
113 additions
and
110 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
73 changes: 73 additions & 0 deletions
73
...lix/generator/profile/validators/profile/constraints/capabilities/ValueTypeValidator.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,73 @@ | ||
/* | ||
* Copyright 2019 Scott Logic Ltd | ||
* | ||
* 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 com.scottlogic.datahelix.generator.profile.validators.profile.constraints.capabilities; | ||
|
||
import com.scottlogic.datahelix.generator.common.profile.FieldType; | ||
import com.scottlogic.datahelix.generator.common.validators.ValidationResult; | ||
import com.scottlogic.datahelix.generator.common.validators.Validator; | ||
import com.scottlogic.datahelix.generator.common.whitelist.WeightedElement; | ||
|
||
public class ValueTypeValidator implements Validator<Object> | ||
{ | ||
private final FieldType expectedFieldType; | ||
private final String errorInfo; | ||
|
||
public ValueTypeValidator(FieldType expectedFieldType, String errorInfo) | ||
{ | ||
this.expectedFieldType = expectedFieldType; | ||
this.errorInfo = errorInfo; | ||
} | ||
|
||
@Override | ||
public final ValidationResult validate(Object value) | ||
{ | ||
if (value == null) | ||
{ | ||
return ValidationResult.failure("Values must be specified" + errorInfo); | ||
} | ||
if (value instanceof WeightedElement) | ||
{ | ||
return validate(((WeightedElement) value).element()); | ||
} | ||
|
||
switch (expectedFieldType) | ||
{ | ||
case BOOLEAN: | ||
return value instanceof Boolean | ||
? ValidationResult.success() | ||
: ValidationResult.failure(String.format("Value %s must be a boolean%s", ValidationResult.quote(value), errorInfo)); | ||
case NUMERIC: | ||
return value instanceof Number || value instanceof String && isNumber((String) value) | ||
? ValidationResult.success() | ||
: ValidationResult.failure(String.format("Value %s must be a number%s", ValidationResult.quote(value), errorInfo)); | ||
default: | ||
return value instanceof String ? ValidationResult.success() : ValidationResult.failure(String.format("Value %s must be a string%s", ValidationResult.quote(value), errorInfo)); | ||
} | ||
} | ||
|
||
private static boolean isNumber(String s) | ||
{ | ||
try | ||
{ | ||
Double.parseDouble(s); | ||
return true; | ||
} catch (NumberFormatException | NullPointerException e) | ||
{ | ||
return false; | ||
} | ||
} | ||
} |
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