-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #6 from erik1066/develop
Updated example apps
- Loading branch information
Showing
5 changed files
with
261 additions
and
27 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
using RapidCsv; | ||
|
||
string csvContent = @"NAME,AGE,DOB,PHONE,STATUS | ||
John,23,1/1/2012,555-555-5555,actv | ||
Mary,34,1/1/1990,555-555-5555,inac | ||
Jane,25,1/1/2010,555-555-5555,actv | ||
Hana,55,1/1/1970,555-555-555X,unkn"; | ||
|
||
string validationProfile = @"{ | ||
""$schema"": ""rapid-csv/validator-config-schema.json"", | ||
""name"": ""Acme Bookstore Customer Records"", | ||
""description"": ""Validation profile for the CSV records of our Acme bookstore customers"", | ||
""filename"": ""abc123.csv"", | ||
""separator"": "","", | ||
""has_header"": true, | ||
""columns"": [ | ||
{ | ||
""name"": ""NAME"", | ||
""description"": ""The customer's name"", | ||
""ordinal"": 1, | ||
""type"": ""string"", | ||
""max"": 25, | ||
""min"": 0, | ||
""required"": false, | ||
""null_or_empty"": true, | ||
""format"": null, | ||
""regex"": null | ||
}, | ||
{ | ||
""name"": ""AGE"", | ||
""description"": ""The customer's age"", | ||
""ordinal"": 2, | ||
""type"": ""integer"", | ||
""max"": 125, | ||
""min"": 7, | ||
""required"": false, | ||
""null_or_empty"": true, | ||
""format"": null, | ||
""regex"": null | ||
}, | ||
{ | ||
""name"": ""DOB"", | ||
""description"": ""The customer's date of birth"", | ||
""ordinal"": 3, | ||
""type"": ""string"", | ||
""required"": false, | ||
""null_or_empty"": true, | ||
""format"": ""m/d/yyyy"", | ||
""regex"": null | ||
}, | ||
{ | ||
""name"": ""PHONE"", | ||
""description"": ""The customer's phone number"", | ||
""ordinal"": 4, | ||
""type"": ""string"", | ||
""required"": false, | ||
""null_or_empty"": true, | ||
""format"": null, | ||
""regex"": ""^(\\+\\d{1,2}\\s)?\\(?\\d{3}\\)?[\\s.-]\\d{3}[\\s.-]\\d{4}$"" | ||
}, | ||
{ | ||
""name"": ""STATUS"", | ||
""description"": ""Customer status"", | ||
""ordinal"": 5, | ||
""type"": ""enum"", | ||
""values"": [ ""actv"", ""inac"" ], | ||
""required"": false, | ||
""null_or_empty"": true, | ||
""format"": null, | ||
""regex"": null | ||
} | ||
] | ||
}"; | ||
|
||
CsvValidator validator = new CsvValidator(); | ||
var options = new ValidationOptions() | ||
{ | ||
Separator = ',', | ||
HasHeaderRow = true, | ||
Quote = '\"', | ||
ValidationProfile = validationProfile | ||
}; | ||
|
||
Stream content = GenerateStreamFromString(csvContent); | ||
ValidationResult result = validator.Validate(content: content, options: options); | ||
|
||
Console.WriteLine($"Valid File = {result.IsValid}"); | ||
Console.WriteLine($" Data Rows = {result.DataRowCount}"); | ||
Console.WriteLine($" Elapsed time (ms) = {result.ElapsedMilliseconds.ToString("N0")}ms"); | ||
Console.WriteLine($" Columns = {result.FieldCount}"); | ||
Console.WriteLine($" Error count = {result.ErrorCount}"); | ||
Console.WriteLine($" Warning count = {result.WarningCount}"); | ||
|
||
if (result.Headers.Count != 0) | ||
{ | ||
Console.WriteLine(" Headers = "); | ||
for (int i = 0; i < result.Headers.Count; i++) | ||
{ | ||
Console.WriteLine($" Column {i + 1} = {result.Headers[i]}"); | ||
} | ||
} | ||
|
||
if (result.Messages.Count != 0) | ||
{ | ||
Console.WriteLine(" Validation Messages = "); | ||
for (int i = 0; i < result.Messages.Count; i++) | ||
{ | ||
var msg = result.Messages[i]; | ||
Console.WriteLine($" Message {i + 1} = "); | ||
Console.WriteLine($" Severity : {msg.Severity}"); | ||
Console.WriteLine($" Code : {msg.Code}"); | ||
Console.WriteLine($" Row : {msg.Row}"); | ||
if (msg.Character >=0) Console.WriteLine($" Character : {msg.Character}"); | ||
Console.WriteLine($" Field Num : {msg.FieldNumber}"); | ||
Console.WriteLine($" Field Name : {msg.FieldName}"); | ||
Console.WriteLine($" Err Content: {msg.Content}"); | ||
} | ||
} | ||
|
||
static Stream GenerateStreamFromString(string s) | ||
{ | ||
var stream = new MemoryStream(); | ||
var writer = new StreamWriter(stream); | ||
writer.Write(s); | ||
writer.Flush(); | ||
stream.Position = 0; | ||
return stream; | ||
} |
15 changes: 15 additions & 0 deletions
15
examples/demo-console-content-validation/demo-console-content-validation.csproj
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,15 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<ItemGroup> | ||
<PackageReference Include="RapidCsv" Version="0.0.1" /> | ||
</ItemGroup> | ||
|
||
<PropertyGroup> | ||
<OutputType>Exe</OutputType> | ||
<TargetFramework>net8.0</TargetFramework> | ||
<RootNamespace>demo_console_content_validation</RootNamespace> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
</Project> |
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 was deleted.
Oops, something went wrong.