Skip to content
Chris Wood edited this page Oct 3, 2019 · 3 revisions

Validators

Usage

Contains

var template = "Name: { Name : Contains('B') }";
var input = "Name: Alice, Name: Bob";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("Bob", result.First("Name"));

EndsWith

var template = "Email: { AdminEmail : EndsWith('admin.com') }";
var input = "Email: [email protected] Email: [email protected]";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("[email protected]", result.First("AdminEmail"));

IsDateTime

var template = "Date: { Date : IsDateTime('yyyy-MM-dd') }";
var input = "Date: 3rd Oct 2019 Date: 2019-10-04";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("2019-10-04", result.First("Date"));

IsDomainName

var template = "Web: { Domain : IsDomainName }";
var input = "Web: n/a Web: www.flipbit.co.uk";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("www.flipbit.co.uk", result.First("Domain"));

IsEmail

var template = "Email: { Email : IsEmail }";
var input = "Email: webmaster at host.com Email: [email protected]";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("[email protected]", result.First("Email"));

IsNotEmpty

var template = "Middle Name: { MiddleName : IsNotEmpty }";
var input = "Middle Name:  Middle Name: Charles";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("Charles", result.First("MiddleName"));

IsNot

var template = "Address: { Address : IsNot('N/A') }";
var input = "Address: N/A  Address: 10 Acacia Avenue";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("10 Acacia Avenue", result.First("Address"));

IsNumeric

var template = "Age: { Age : IsNumeric }";
var input = "Age: Ten  Age: 10";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("10", result.First("Age"));

IsPhoneNumber

var template = "Phone: { Phone : IsPhoneNumber }";
var input = "Phone: Disconnected  Phone: +44 (0) 1603 555-1234";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("+44 (0) 1603 555-1234", result.First("Phone"));

IsUrl

var template = "Server: { ServerUrl : IsUrl }";
var input = "Server: 192.168.1.1  Server: www.server.com";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("www.server.com", result.First("ServerUrl"));

MaxLength

var template = "Zip: { ZipCode : MaxLength(5) }";
var input = "Zip: 123456  Zip: 78912";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("78912", result.First("ZipCode"));

MinLength

var template = "Zip: { ZipCode : MinLength(5) }";
var input = "Zip: 123  Zip: 45678";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("45678", result.First("ZipCode"));

StartsWith

var template = "Ip: { InternalIpAddress : StartsWith('192') }";
var input = "Ip: 80.34.123.45  Ip: 192.168.1.1";

var result = new Tokenizer().Tokenize(template, input);

Assert.AreEqual("192.168.1.1", result.First("InternalIpAddress"));

Creating Custom Validators