{"id":13431204,"url":"https://github.com/safakgur/guard","last_synced_at":"2025-03-16T11:31:17.561Z","repository":{"id":53519740,"uuid":"114297755","full_name":"safakgur/guard","owner":"safakgur","description":"A high-performance, extensible argument validation library.","archived":true,"fork":false,"pushed_at":"2023-07-18T04:29:29.000Z","size":1094,"stargazers_count":710,"open_issues_count":0,"forks_count":54,"subscribers_count":24,"default_branch":"dev","last_synced_at":"2025-03-07T00:36:13.673Z","etag":null,"topics":["arguments","c-sharp","csharp","dotnet-standard","fluent","guard","guard-clauses","normalization","validation"],"latest_commit_sha":null,"homepage":"","language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/safakgur.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null}},"created_at":"2017-12-14T21:26:38.000Z","updated_at":"2025-02-13T13:16:34.000Z","dependencies_parsed_at":"2024-01-02T22:38:32.114Z","dependency_job_id":"93681c4d-ace0-4d94-bdba-2134a8a9b396","html_url":"https://github.com/safakgur/guard","commit_stats":{"total_commits":267,"total_committers":3,"mean_commits":89.0,"dds":"0.011235955056179803","last_synced_commit":"2a79a903895ded2c4dab7b29e7f830ba9f0a331e"},"previous_names":[],"tags_count":17,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safakgur%2Fguard","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safakgur%2Fguard/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safakgur%2Fguard/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/safakgur%2Fguard/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/safakgur","download_url":"https://codeload.github.com/safakgur/guard/tar.gz/refs/heads/dev","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243862770,"owners_count":20360207,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["arguments","c-sharp","csharp","dotnet-standard","fluent","guard","guard-clauses","normalization","validation"],"created_at":"2024-07-31T02:01:01.317Z","updated_at":"2025-03-16T11:31:16.884Z","avatar_url":"https://github.com/safakgur.png","language":"C#","readme":"# Guard\n\n## Warning\n\nThis project is no longer actively maintained.\n\nI apologise to everyone who depended on Guard for my three-year-long silence.\nGuard was my passion project for the longest time, helping me get better at C#, automated builds,\nVisual Studio extensibility, documentation, and how to be part of a community regardless of how\nsmall we've been. It also kickstarted my journey of creating terrible gifs.\n\nBut my life has changed - I moved to a different country, changed jobs, lost someone very dear to me,\nand started spending less and less time coding outside my work hours. I always believed I'd be back\nand release v2 with all my bright ideas, but I just didn't know when. I only noticed today that\nI've been avoiding my GitHub notifications out of guilt for over a year, and I figured it was time for closure.\n\n### Why I'm stopping this\n\n* I can no longer find the time to maintain an open-source project, as I want to spend most of my\n  free time with my family and on other hobbies.\n  \n* Guard has always been in an awkward position:\n\n  Library authors avoid adding unnecessary dependencies to their packages, and Guard didn't bring\n  enough value to be worth being a runtime dependency. I wanted to experiment with IL weaving and\n  source generators to make Guard a compile-time dependency that would replace call sites with\n  simple if-then-throw statements, but I never got the chance.\n\n  App developers didn't need Guard as their validation scenarios doesn't involve throwing argument\n  exceptions. For example, a web API would use something like DataAnnotation attributes or\n  [FluentValidation](https://fluentvalidation.net) to validate its requests. So as a library that\n  specifically targeted library authors, Guard has always been irrelevant to app developers.\n\n### What was I looking to do in v2:\n\n* New style with `using static` and `[CallerArgumentExpression]`:\n\n  ```csharp\n  // using static Dawn.Validation.Argument;\n\n  Require(firstName).NotNullOrWhiteSpace();\n  ```\n  \n* Lite validations with `[CallerArgumentExpression]`:\n\n  ```csharp\n  var ratio = 5.0;\n  Require(ratio).Range(ratio is \u003e= 0.0 and \u003c= 1.0);\n  // ArgumentOutOfRangeException\n  // * ParamName: \"ratio\"\n  // * ActualValue: 5\n  // * Message: \"Failed precondition: 'ratio is \u003e= 0.0 and \u003c= 1.0'.\"\n  ```\n\n  Hadn't yet decided whether the light validations would replace the custom ones or live in addition to them.\n\n* Type guards, e.g., `Require\u003cTImplementation\u003e().NotAbstract();`\n\n* Fixing nullable reference type issues\n\n* Changing the argument value to a ref field, which meant:\n\n  * We could even work on mutable structs without copying\n\n  * We could experiment with in-place normalisations\n\n* Dropping .NET Standard support\n\nI hope this helps anyone who was waiting for the v2. Original intro below:\n\n---\n\n![Logo](media/guard-64.png)\n\nGuard is a fluent argument validation library that is intuitive, fast and extensible.\n\n[![NuGet](https://img.shields.io/nuget/v/Dawn.Guard.svg?style=flat)](https://www.nuget.org/packages/Dawn.Guard/)\n[![Build](https://dev.azure.com/safakgur/Guard/_apis/build/status/Guard-CI?label=builds)](https://dev.azure.com/safakgur/Guard/_build/latest?definitionId=1)\n[![Coverage](https://codecov.io/gh/safakgur/guard/branch/dev/graph/badge.svg)](https://codecov.io/gh/safakgur/guard/branch/dev)  \n`$ dotnet add package Dawn.Guard` / `PM\u003e Install-Package Dawn.Guard`\n\n* [Introduction](#introduction)\n* [What's Wrong with Vanilla?](#whats-wrong-with-vanilla)\n* [Requirements](#requirements)\n* [Standard Validations](#standard-validations)\n* [Design Decisions](#design-decisions)\n* [Extensibility](#extensibility)\n* [Code Snippets](#code-snippets)\n\n## Introduction\n\nHere is a sample constructor that validates its arguments without Guard:\n\n```c#\npublic Person(string name, int age)\n{\n    if (name == null)\n        throw new ArgumentNullException(nameof(name), \"Name cannot be null.\");\n\n    if (name.Length == 0)\n        throw new ArgumentException(\"Name cannot be empty.\", nameof(name));\n\n    if (age \u003c 0)\n        throw new ArgumentOutOfRangeException(nameof(age), age, \"Age cannot be negative.\");\n\n    Name = name;\n    Age = age;\n}\n```\n\nAnd this is how we write the same constructor with Guard:\n\n```c#\nusing Dawn; // Bring Guard into scope.\n\npublic Person(string name, int age)\n{\n    Name = Guard.Argument(name, nameof(name)).NotNull().NotEmpty();\n    Age = Guard.Argument(age, nameof(age)).NotNegative();\n}\n```\n\nIf this looks like too much allocations to you, fear not. The arguments are read-only structs that\nare passed by reference. See the [design decisions](#design-decisions) for details and an\nintroduction to Guard's more advanced features.\n\n## What's Wrong with Vanilla?\n\nThere is nothing wrong with writing your own checks but when you have lots of types you need to\nvalidate, the task gets very tedious, very quickly.\n\nLet's analyze the string validation in the example without Guard:\n\n* We have an argument (name) that we need to be a non-null, non-empty string.\n* We check if it's null and throw an `ArgumentNullException` if it is.\n* We then check if it's empty and throw an `ArgumentException` if it is.\n* We specify the same parameter name for each validation.\n* We write an error message for each validation.\n* `ArgumentNullException` accepts the parameter name as its first argument and error message as its\nsecond while it's the other way around for the `ArgumentException`. An inconsistency that many of us\nsometimes find it hard to remember.\n\nIn reality, all we need to express should be the first bullet, that we want our argument non-null\nand non-empty.\n\nWith Guard, if you want to guard an argument against null, you just write `NotNull` and that's it.\nIf the argument is passed null, you'll get an `ArgumentNullException` thrown with the correct\nparameter name and a clear error message out of the box. The [standard validations](#standard-validations)\nhave fully documented, meaningful defaults that get out of your way and let you focus on your project.\n\n## Requirements\n\n**C# 7.2 or later is required.** Guard takes advantage of almost all the new features introduced in\nC# 7.2. So in order to use Guard, you need to make sure your Visual Studio is up to date and you\nhave `\u003cLangVersion\u003e7.2\u003c/LangVersion\u003e` or later added in your .csproj file.\n\n**.NET Standard 1.0** and above are supported. [Microsoft Docs][2] lists the following platform\nversions as .NET Standard 1.0 compliant but keep in mind that currently, the unit tests are only\ntargeting .NET Core 3.0.\n\n| Platform                   | Version  |\n| -------------------------- | -------- |\n| .NET Core                  | `1.0`    |\n| .NET Framework             | `4.5`    |\n| Mono                       | `4.6`    |\n| Xamarin.iOS                | `10.0`   |\n| Xamarin.Mac                | `3.0`    |\n| Xamarin.Android            | `7.0`    |\n| Universal Windows Platform | `10.0`   |\n| Windows                    | `8.0`    |\n| Windows Phone              | `8.1`    |\n| Windows Phone Silverlight  | `8.0`    |\n| Unity                      | `2018.1` |\n\n## More\n\nThe default branch (dev) is the development branch, so it may contain changes/features that are not\npublished to NuGet yet. See the [master](https://github.com/safakgur/guard/tree/master) branch for\nthe latest published version.\n\n### Standard Validations\n\n[Click here][3] for a list of the validations that are included in the library.\n\n### Design Decisions\n\n[Click here][1] for the document that explains the motives behind the Guard's API design and more\nadvanced features.\n\n### Extensibility\n\n[Click here][4] to see how to add custom validations to Guard by writing simple extension methods.\n\n### Code Snippets\n\nCode snippets can be found in the [snippets][5] folder. Currently, only the Visual Studio is\nsupported.\n\n[1]: docs/design-decisions.md\n[2]: https://docs.microsoft.com/dotnet/standard/net-standard\n[3]: docs/standard-validations.md\n[4]: docs/extensibility.md\n[5]: snippets\n","funding_links":[],"categories":["Frameworks, Libraries and Tools","框架, 库和工具","Misc","杂项"],"sub_categories":["Misc","大杂烩"],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafakgur%2Fguard","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsafakgur%2Fguard","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsafakgur%2Fguard/lists"}