| | | 1 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | | 2 | | using ValidateLib.Metadata.Descriptors; |
| | | 3 | | |
| | | 4 | | namespace ValidateLib.TabularData.Datatypes |
| | | 5 | | { |
| | | 6 | | public class BooleanDT : BaseDT |
| | | 7 | | { |
| | 1 | 8 | | bool Value { get; set; } |
| | 1 | 9 | | string[] trueValues = new string[] { "true", "1" }; |
| | 1 | 10 | | string[] falseValues = new string[] { "false", "0" }; |
| | 1 | 11 | | public BooleanDT() { } |
| | 1 | 12 | | public BooleanDT(string stringValue) : base(stringValue) |
| | | 13 | | { |
| | 1 | 14 | | if (trueValues.Contains(stringValue)) Value = true; |
| | 1 | 15 | | else if (falseValues.Contains(stringValue)) Value = false; |
| | 1 | 16 | | else ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| | | 17 | | |
| | 0 | 18 | | } |
| | | 19 | | |
| | 1 | 20 | | public BooleanDT(string stringValue, FormatDescriptor descriptor) : base(stringValue, descriptor) |
| | | 21 | | { |
| | | 22 | | //never null here |
| | 1 | 23 | | string formatPattern = descriptor.pattern!; |
| | 1 | 24 | | string[] formatParts = formatPattern.Split('|'); |
| | 1 | 25 | | string? trueVal = null; |
| | 1 | 26 | | string? falseVal = null; |
| | 1 | 27 | | if (formatParts.Length == 2) |
| | | 28 | | { |
| | 1 | 29 | | trueVal = formatParts[0]; |
| | 1 | 30 | | falseVal = formatParts[1]; |
| | | 31 | | } |
| | | 32 | | else |
| | | 33 | | { |
| | 0 | 34 | | ErrorFactory.ThrowInvalidFormatError(descriptor.pattern); |
| | | 35 | | } |
| | | 36 | | |
| | 1 | 37 | | if (stringValue == trueVal) |
| | 1 | 38 | | Value = true; |
| | 1 | 39 | | else if (stringValue == falseVal) |
| | 1 | 40 | | Value = false; |
| | | 41 | | else |
| | 1 | 42 | | ErrorFactory.ThrowDatatypeValidationError(stringValue); |
| | 0 | 43 | | } |
| | | 44 | | |
| | | 45 | | |
| | | 46 | | public override bool IsPatternValid(FormatDescriptor format) |
| | | 47 | | { |
| | 1 | 48 | | string formatPattern = format.pattern!; |
| | 1 | 49 | | string[] formatParts = formatPattern.Split('|'); |
| | 1 | 50 | | if (formatParts.Length != 2) |
| | 1 | 51 | | return false; |
| | 1 | 52 | | return true; |
| | | 53 | | } |
| | | 54 | | |
| | | 55 | | |
| | | 56 | | } |
| | | 57 | | } |