All Projects → safakgur → Guard

safakgur / Guard

Licence: mit
A high-performance, extensible argument validation library.

Programming Languages

csharp
926 projects

Projects that are alternatives of or similar to Guard

Fluentresults
A generalised Result object implementation for .NET/C#
Stars: ✭ 266 (-37.7%)
Mutual labels:  validation, dotnet-standard
Alexa Skills Dotnet
An Amazon Alexa Skills SDK for .NET
Stars: ✭ 412 (-3.51%)
Mutual labels:  dotnet-standard
Laravel Validation Rules
A set of useful Laravel validation rules
Stars: ✭ 374 (-12.41%)
Mutual labels:  validation
V8n
☑️ JavaScript fluent validation library
Stars: ✭ 3,858 (+803.51%)
Mutual labels:  validation
Dredd
Language-agnostic HTTP API Testing Tool
Stars: ✭ 3,770 (+782.9%)
Mutual labels:  validation
Graphql Constraint Directive
Validate GraphQL fields
Stars: ✭ 401 (-6.09%)
Mutual labels:  validation
Revalidate
Elegant and composable validations
Stars: ✭ 363 (-14.99%)
Mutual labels:  validation
Dotnet Win32 Service
Helper classes to set up and run as windows services directly on .net core. A ServiceBase alternative.
Stars: ✭ 425 (-0.47%)
Mutual labels:  dotnet-standard
Indicative
Indicative is a simple yet powerful data validator for Node.js and browsers. It makes it so simple to write async validations on nested set of data.
Stars: ✭ 412 (-3.51%)
Mutual labels:  validation
Shopifysharp
ShopifySharp is a .NET library that helps developers easily authenticate with and manage Shopify stores.
Stars: ✭ 390 (-8.67%)
Mutual labels:  dotnet-standard
Storage
💿 Storage abstractions with implementations for .NET/.NET Standard
Stars: ✭ 380 (-11.01%)
Mutual labels:  dotnet-standard
Mson
🏗️MSON Lang: Generate an app from JSON
Stars: ✭ 378 (-11.48%)
Mutual labels:  validation
Specs
Technical specifications and guidelines for implementing Frictionless Data.
Stars: ✭ 403 (-5.62%)
Mutual labels:  validation
Xamarin.forms.inputkit
CheckBox, Radio Button, Labeled Slider, Dropdowns etc.
Stars: ✭ 372 (-12.88%)
Mutual labels:  validation
Typesystem
Data validation, serialization, deserialization & form rendering. 🔢
Stars: ✭ 416 (-2.58%)
Mutual labels:  validation
Pyschemes
PySchemes is a library for validating data structures in python
Stars: ✭ 365 (-14.52%)
Mutual labels:  validation
React Hook Form
📋 React Hooks for form state management and validation (Web + React Native)
Stars: ✭ 24,831 (+5715.22%)
Mutual labels:  validation
Linqtotwitter
LINQ Provider for the Twitter API (C# Twitter Library)
Stars: ✭ 401 (-6.09%)
Mutual labels:  dotnet-standard
Graphql Query Complexity
GraphQL query complexity analysis and validation for graphql-js
Stars: ✭ 424 (-0.7%)
Mutual labels:  validation
Dss
Digital Signature Service : creation, extension and validation of advanced electronic signatures
Stars: ✭ 415 (-2.81%)
Mutual labels:  validation

Guard

Logo

Guard is a fluent argument validation library that is intuitive, fast and extensible.

NuGet Build Coverage
$ dotnet add package Dawn.Guard / PM> Install-Package Dawn.Guard

Introduction

Here is a sample constructor that validates its arguments without Guard:

public Person(string name, int age)
{
    if (name == null)
        throw new ArgumentNullException(nameof(name), "Name cannot be null.");

    if (name.Length == 0)
        throw new ArgumentException("Name cannot be empty.", nameof(name));

    if (age < 0)
        throw new ArgumentOutOfRangeException(nameof(age), age, "Age cannot be negative.");

    Name = name;
    Age = age;
}

And this is how we write the same constructor with Guard:

using Dawn; // Bring Guard into scope.

public Person(string name, int age)
{
    Name = Guard.Argument(name, nameof(name)).NotNull().NotEmpty();
    Age = Guard.Argument(age, nameof(age)).NotNegative();
}

If this looks like too much allocations to you, fear not. The arguments are read-only structs that are passed by reference. See the design decisions for details and an introduction to Guard's more advanced features.

What's Wrong with Vanilla?

There is nothing wrong with writing your own checks but when you have lots of types you need to validate, the task gets very tedious, very quickly.

Let's analyze the string validation in the example without Guard:

  • We have an argument (name) that we need to be a non-null, non-empty string.
  • We check if it's null and throw an ArgumentNullException if it is.
  • We then check if it's empty and throw an ArgumentException if it is.
  • We specify the same parameter name for each validation.
  • We write an error message for each validation.
  • ArgumentNullException accepts the parameter name as its first argument and error message as its second while it's the other way around for the ArgumentException. An inconsistency that many of us sometimes find it hard to remember.

In reality, all we need to express should be the first bullet, that we want our argument non-null and non-empty.

With Guard, if you want to guard an argument against null, you just write NotNull and that's it. If the argument is passed null, you'll get an ArgumentNullException thrown with the correct parameter name and a clear error message out of the box. The standard validations have fully documented, meaningful defaults that get out of your way and let you focus on your project.

Requirements

C# 7.2 or later is required. Guard takes advantage of almost all the new features introduced in C# 7.2. So in order to use Guard, you need to make sure your Visual Studio is up to date and you have <LangVersion>7.2</LangVersion> or later added in your .csproj file.

.NET Standard 1.0 and above are supported. Microsoft Docs lists the following platform versions as .NET Standard 1.0 compliant but keep in mind that currently, the unit tests are only targeting .NET Core 3.0.

Platform Version
.NET Core 1.0
.NET Framework 4.5
Mono 4.6
Xamarin.iOS 10.0
Xamarin.Mac 3.0
Xamarin.Android 7.0
Universal Windows Platform 10.0
Windows 8.0
Windows Phone 8.1
Windows Phone Silverlight 8.0
Unity 2018.1

More

The default branch (dev) is the development branch, so it may contain changes/features that are not published to NuGet yet. See the master branch for the latest published version.

Standard Validations

Click here for a list of the validations that are included in the library.

Design Decisions

Click here for the document that explains the motives behind the Guard's API design and more advanced features.

Extensibility

Click here to see how to add custom validations to Guard by writing simple extension methods.

Code Snippets

Code snippets can be found in the snippets folder. Currently, only the Visual Studio is supported.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].