| | 1 | | using ValidateLib.ErrorsAndWarnings.Errors; |
| | 2 | | using ValidateLib.Metadata.Descriptors; |
| | 3 | | using ValidateLib.TabularData.AnnotatedTabularDataModel; |
| | 4 | | using ValidateLib.TabularData.Datatypes; |
| | 5 | |
|
| | 6 | | namespace ValidateLib.TabularData.Validation.ValidationRules |
| | 7 | | { |
| | 8 | | public class CellValueConstraintValidationRule : ICellValidationRule |
| | 9 | | { |
| | 10 | | public void ValidateCell(Cell annotatedCell) |
| | 11 | | { |
| | 12 | | // this means that the cell is in an column which does not have a descriptor |
| 1 | 13 | | if (annotatedCell.column is null) |
| 1 | 14 | | return; |
| 1 | 15 | | DatatypeDescriptor datatypeDescriptor = annotatedCell.column!.datatype!; |
| | 16 | |
|
| 1 | 17 | | if (datatypeDescriptor._base is null || datatypeDescriptor._base._value is null) |
| 0 | 18 | | return; |
| | 19 | |
|
| 1 | 20 | | var dt = DatatypeFactory.GetDatatype(null, datatypeDescriptor._base!._value!, null); |
| 1 | 21 | | if (dt is not IValue) |
| 1 | 22 | | return; |
| | 23 | |
|
| 1 | 24 | | switch (annotatedCell.cellType) |
| | 25 | | { |
| | 26 | | case CellType.NULL: |
| 1 | 27 | | bool areMet = AreValueConstraintsMet(null, datatypeDescriptor); |
| 1 | 28 | | if (!areMet) |
| 0 | 29 | | annotatedCell.errors.Add(ErrorFactory.GetDatatypeValueConstraintVE(annotatedCell)); |
| 0 | 30 | | break; |
| | 31 | | case CellType.SIMPLE_VALUE: |
| 1 | 32 | | areMet = AreValueConstraintsMet(annotatedCell.value, datatypeDescriptor); |
| 1 | 33 | | if (!areMet) |
| 1 | 34 | | annotatedCell.errors.Add(ErrorFactory.GetDatatypeValueConstraintVE(annotatedCell)); |
| 1 | 35 | | break; |
| | 36 | | case CellType.LIST: |
| 0 | 37 | | foreach (var value in annotatedCell.cellValues) |
| | 38 | | { |
| 0 | 39 | | areMet = AreValueConstraintsMet(value, datatypeDescriptor); |
| 0 | 40 | | if (!areMet) |
| 0 | 41 | | annotatedCell.errors.Add(ErrorFactory.GetDatatypeValueConstraintVE(annotatedCell)); |
| | 42 | | } |
| | 43 | | break; |
| | 44 | | } |
| 1 | 45 | | } |
| | 46 | |
|
| | 47 | | bool AreValueConstraintsMet(BaseDT? dt, DatatypeDescriptor datatypeDescriptor) |
| | 48 | | { |
| 1 | 49 | | bool areMet = true; |
| 1 | 50 | | if (datatypeDescriptor.minimum is not null && datatypeDescriptor.minimum._value is not null) |
| | 51 | | { |
| 1 | 52 | | var minimum = DatatypeFactory.GetDatatype(datatypeDescriptor.minimum._value, datatypeDescriptor._base!._ |
| 1 | 53 | | areMet &= minimum.CompareTo(dt) <= 0; |
| | 54 | | } |
| 1 | 55 | | if (datatypeDescriptor.minInclusive is not null && datatypeDescriptor.minInclusive._value is not null) |
| | 56 | | { |
| 1 | 57 | | var minInclusive = DatatypeFactory.GetDatatype(datatypeDescriptor.minInclusive._value, datatypeDescripto |
| 1 | 58 | | areMet &= minInclusive.CompareTo(dt) <= 0; |
| | 59 | | } |
| 1 | 60 | | if (datatypeDescriptor.minExclusive is not null && datatypeDescriptor.minExclusive._value is not null) |
| | 61 | | { |
| 1 | 62 | | var minExclusive = DatatypeFactory.GetDatatype(datatypeDescriptor.minExclusive._value, datatypeDescripto |
| 1 | 63 | | areMet &= minExclusive.CompareTo(dt) < 0; |
| | 64 | | } |
| | 65 | |
|
| 1 | 66 | | if (datatypeDescriptor.maximum is not null && datatypeDescriptor.maximum._value is not null) |
| | 67 | | { |
| 1 | 68 | | var maximum = DatatypeFactory.GetDatatype(datatypeDescriptor.maximum._value, datatypeDescriptor._base!._ |
| 1 | 69 | | areMet &= maximum.CompareTo(dt) >= 0; |
| | 70 | | } |
| 1 | 71 | | if (datatypeDescriptor.maxInclusive is not null && datatypeDescriptor.maxInclusive._value is not null) |
| | 72 | | { |
| 1 | 73 | | var maxInclusive = DatatypeFactory.GetDatatype(datatypeDescriptor.maxInclusive._value, datatypeDescripto |
| 1 | 74 | | areMet &= maxInclusive.CompareTo(dt) >= 0; |
| | 75 | | } |
| 1 | 76 | | if (datatypeDescriptor.maxExclusive is not null && datatypeDescriptor.maxExclusive._value is not null) |
| | 77 | | { |
| 1 | 78 | | var maxExclusive = DatatypeFactory.GetDatatype(datatypeDescriptor.maxExclusive._value, datatypeDescripto |
| 1 | 79 | | areMet &= maxExclusive.CompareTo(dt) > 0; |
| | 80 | | } |
| 1 | 81 | | return areMet; |
| | 82 | | } |
| | 83 | | } |
| | 84 | | } |