Improve validation

This commit is contained in:
Jonathan Jenne 2021-11-18 13:31:14 +01:00
parent 3b07002c28
commit 3760453d82
2 changed files with 67 additions and 32 deletions

View File

@ -153,46 +153,79 @@ Namespace Documents
Where(Function(t) t.Name = oTopLevelElement.Name). Where(Function(t) t.Name = oTopLevelElement.Name).
FirstOrDefault() FirstOrDefault()
For Each oSubElement As XElement In oSubElements For Each oColumn In oTable.Columns
Dim oSchemaField = oTable.Columns. Dim oSubElement = oSubElements.
Where(Function(c) c.Name = oSubElement.Name). Where(Function(e) e.Name = oColumn.Name).
SingleOrDefault() SingleOrDefault()
Dim oRequired = oSchemaField.IsRequired If oSubElement IsNot Nothing Then
Dim oValue = oSubElement.Value.Trim() Dim oRequired = oColumn.IsRequired
Dim oValue = oSubElement.Value.Trim()
' TODO: Needed when we have time for date times ' TODO: Needed when we have time for date times
'If oSchemaField.DataType = Constants.ColumnType.Date Then 'If oSchemaField.DataType = Constants.ColumnType.Date Then
' Dim oDate = Date.ParseExact(oValue, "yyyy-MM-dd", CultureInfo.InvariantCulture) ' Dim oDate = Date.ParseExact(oValue, "yyyy-MM-dd", CultureInfo.InvariantCulture)
' oValue = oDate.ToString("d") ' oValue = oDate.ToString("d")
'End If 'End If
oFields.Add(oSubElement.Name.ToString, New DocumentRow.FieldValue With { oFields.Add(oSubElement.Name.ToString, New DocumentRow.FieldValue With {
.Original = oValue, .Original = oValue,
.Final = oValue, .Final = oValue,
.DataType = oSchemaField.DataType, .DataType = oColumn.DataType,
.Required = oRequired .Required = oRequired
}) })
Else
Dim oColumnError = DocumentRow.FieldError.None
If oColumn.Config?.IsRequired Then
oColumnError = DocumentRow.FieldError.MissingValue
End If
oFields.Add(oColumn.Name, New DocumentRow.FieldValue With {
.[Error] = oColumnError
})
End If
Next Next
' All fields in the schema are generated, 'For Each oSubElement As XElement In oSubElements
' only creating the ones with values leads to wrong visual cues when asking for ' Dim oSchemaField = oTable.Columns.
' docs/rows/fields with errors ' Where(Function(c) c.Name = oSubElement.Name).
For Each oColumn In oTable.Columns ' SingleOrDefault()
If oFields.ContainsKey(oColumn.Name) Then
Continue For
End If
Dim oColumnError = DocumentRow.FieldError.None ' Dim oRequired = oSchemaField.IsRequired
If oColumn.Config?.IsRequired Then ' Dim oValue = oSubElement.Value.Trim()
oColumnError = DocumentRow.FieldError.MissingValue
End If
oFields.Add(oColumn.Name, New DocumentRow.FieldValue With { ' ' TODO: Needed when we have time for date times
.[Error] = oColumnError ' 'If oSchemaField.DataType = Constants.ColumnType.Date Then
}) ' ' Dim oDate = Date.ParseExact(oValue, "yyyy-MM-dd", CultureInfo.InvariantCulture)
Next ' ' oValue = oDate.ToString("d")
' 'End If
' oFields.Add(oSubElement.Name.ToString, New DocumentRow.FieldValue With {
' .Original = oValue,
' .Final = oValue,
' .DataType = oSchemaField.DataType,
' .Required = oRequired
' })
'Next
'' All fields in the schema are generated,
'' only creating the ones with values leads to wrong visual cues when asking for
'' docs/rows/fields with errors
'For Each oColumn In oTable.Columns
' If oFields.ContainsKey(oColumn.Name) Then
' Continue For
' End If
' Dim oColumnError = DocumentRow.FieldError.None
' If oColumn.Config?.IsRequired Then
' oColumnError = DocumentRow.FieldError.MissingValue
' End If
' oFields.Add(oColumn.Name, New DocumentRow.FieldValue With {
' .[Error] = oColumnError
' })
'Next
' Create a DocumentRow object for each Top Level Element ' Create a DocumentRow object for each Top Level Element
Dim oRow = New DocumentRow With { Dim oRow = New DocumentRow With {

View File

@ -60,7 +60,9 @@
Public ReadOnly Property HasError As Boolean Public ReadOnly Property HasError As Boolean
Get Get
Return [Error] <> FieldError.None Or (Required = True And Final = String.Empty) Return Required = True And (
[Error] <> FieldError.None Or Final = String.Empty
)
End Get End Get
End Property End Property