Last active
June 13, 2024 13:33
-
-
Save timoschinkel/fe409ce4e019138778d4f0d9d1879e1e to your computer and use it in GitHub Desktop.
Validating an email address using different methods
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
// Using AWS Cognito via TypeScript | |
import { AdminCreateUserCommand, AdminDeleteUserCommand, CognitoIdentityProviderClient } from '@aws-sdk/client-cognito-identity-provider'; | |
const addresses = [ | |
// valid | |
'name@example.com', | |
'name.name@example.com', | |
'"name..name"@example.com', | |
'name@localhost', | |
'nåme@example.com', | |
'aA0!#$%&\'*+-/=?^_`{|}~@example.com', | |
// invalid | |
'name.example.com', | |
'.name@example.com', | |
'name.@example.com', | |
'name..name@example.com', | |
'<name>@example.com', | |
// invalid host names | |
'name@-example.com', | |
'name@example-.com', | |
'name@example.com-', | |
]; | |
const cognito = new CognitoIdentityProviderClient({ | |
region: 'eu-west-1' | |
}); | |
const userPoolId = process.env.USER_POOL_ID; | |
addresses.forEach(async (address) => { | |
await cognito.send(new AdminCreateUserCommand({ | |
UserPoolId: userPoolId, | |
Username: address, | |
MessageAction: 'SUPPRESS', | |
})).then(async () => { | |
console.log(`${address} is valid`); | |
// clean up | |
await cognito.send(new AdminDeleteUserCommand({ | |
UserPoolId: userPoolId, | |
Username: address | |
})).catch((error) => { | |
console.log(`Unable to clean up ${address} - ${error}`); | |
}); | |
}).catch((error) => { | |
console.log(`${address} is NOT valid - ${error}`); | |
}); | |
}); |
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
// Using Model Annotations in .NET 6 | |
using System.ComponentModel.DataAnnotations; | |
using System; | |
var addresses = new string[] { | |
// valid | |
"name@example.com", | |
"name.name@example.com", | |
"\"name..name\"@example.com", | |
"name@localhost", | |
"nåme@example.com", | |
"aA0!#$%&'*+-/=?^_`{|}~@example.com", | |
// invalid | |
"name.example.com", | |
".name@example.com", | |
"name.@example.com", | |
"name..name@example.com", | |
"<name>@example.com", | |
// invalid host names | |
"name@-example.com", | |
"name@example-.com", | |
"name@example.com-", | |
}; | |
foreach (var address in addresses) | |
{ | |
var obj = new DomainObject { EmailAddress = address }; | |
try | |
{ | |
Validator.ValidateObject(obj, new ValidationContext(obj, null, null), true); | |
Console.WriteLine($"{address} is valid"); | |
} | |
catch (ValidationException exception) | |
{ | |
Console.WriteLine($"{address} is NOT valid - {exception.Message}"); | |
} | |
} | |
class DomainObject | |
{ | |
[EmailAddress] | |
public string? EmailAddress { get; set; } | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment