Common input and integrity validation routines for Visual Studio and other applications
Перейти к файлу
Andrew Arnott f5ff579e93 Add automated code coverage to appveyor 2017-02-06 16:22:40 -08:00
src Add automated code coverage to appveyor 2017-02-06 16:22:40 -08:00
tools Stop downloading credential helper 2017-01-22 21:53:10 -08:00
.gitattributes Move project into subdirectory. 2015-01-29 05:45:57 -08:00
.gitignore Switch to project.json 2016-05-06 01:51:55 -07:00
CONTRIBUTING.md Add code of conduct to contrib doc 2017-01-31 20:13:39 -08:00
EnlistmentInfo.props Simplify EnlistmentInfo.props 2016-10-02 21:43:26 -07:00
EnlistmentInfo.targets Switch to ReadOnlySourceTree package 2016-05-07 23:20:26 -07:00
LICENSE Add LICENSE file 2016-09-24 07:38:10 -07:00
README.md Add automated code coverage to appveyor 2017-02-06 16:22:40 -08:00
appveyor.yml Add automated code coverage to appveyor 2017-02-06 16:22:40 -08:00
init.cmd Add init.ps1 script 2016-07-25 11:19:59 -07:00
init.ps1 Update init to install signing plugin 2016-10-03 10:44:31 -07:00
nuget.config Add appveyor.yml and adjust nuget package sources 2017-01-13 15:50:49 -08:00
version.json Build stable version 2016-12-19 20:55:02 -08:00

README.md

Microsoft.VisualStudio.Validation

NuGet package Build status codecov

This project is available as the Microsoft.VisualStudio.Validation NuGet package.

Basic input validation via the Requires class throws an ArgumentException.

Requires.NotNull(arg1, nameof(arg1));
Requires.NotNullOrEmpty(arg2, nameof(arg2));

State validation via the Verify class throws an InvalidOperationException.

Verify.Operation(condition, "some error occurred.");

Internal integrity checks via the Assumes class throws an InternalErrorException.

Assumes.True(condition, "some error");

Warning signs that should not throw exceptions via the Report class.

Report.IfNot(condition, "some error");

Code Snippets

Make writing input validation especially convenient with code snippets. Run the tools\install_snippets.cmd script within this package to copy the code snippets into your Documents\Visual Studio 201x\Code Snippets\Visual C#\My Code Snippets folder(s) and just type the first few letters of the code snippet name to get auto-completion assisted input validation.

Note that if you have Resharper installed, code snippets don't appear in auto-completion lists so you may have to press Ctrl+J after the first few letters of the code snippet name for it to become available.

Example:

private void SomeMethod(string input) {
    rnne<TAB>
}

Expands to

private void SomeMethod(string input) {
    Requires.NotNullOrEmpty(paramName, nameof(paramName));
}

And the first paramName is selected. Simply type the actual parameter name (Intellisense will auto-complete for you) and then the quoted paramName name will automatically be changed to match.

The two snippets are rnn and rnne which expand to check for null inputs or null-or-empty inputs, respectively.