{"id":13594850,"url":"https://github.com/ardalis/GuardClauses","last_synced_at":"2025-04-09T10:32:11.288Z","repository":{"id":39592026,"uuid":"103309798","full_name":"ardalis/GuardClauses","owner":"ardalis","description":"A simple package with guard clause extensions.","archived":false,"fork":false,"pushed_at":"2025-03-03T13:06:47.000Z","size":3760,"stargazers_count":3157,"open_issues_count":13,"forks_count":277,"subscribers_count":32,"default_branch":"main","last_synced_at":"2025-04-02T20:01:27.960Z","etag":null,"topics":["clean-architecture","clean-code","design-patterns","dotnet","dotnet-core","guard","guard-clause","guard-clauses","hacktoberfest","pattern","patterns"],"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/ardalis.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":"CONTRIBUTING.md","funding":".github/FUNDING.yml","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,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"ardalis"}},"created_at":"2017-09-12T19:02:33.000Z","updated_at":"2025-04-01T09:32:33.000Z","dependencies_parsed_at":"2024-01-06T22:26:39.152Z","dependency_job_id":"ceef8ea0-66ff-4bea-a584-6700bcde8676","html_url":"https://github.com/ardalis/GuardClauses","commit_stats":{"total_commits":293,"total_committers":56,"mean_commits":5.232142857142857,"dds":0.5290102389078498,"last_synced_commit":"6186c68df5af66c7f92841ddbb1e457bb25266c5"},"previous_names":[],"tags_count":32,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardalis%2FGuardClauses","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardalis%2FGuardClauses/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardalis%2FGuardClauses/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ardalis%2FGuardClauses/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ardalis","download_url":"https://codeload.github.com/ardalis/GuardClauses/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248020592,"owners_count":21034459,"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":["clean-architecture","clean-code","design-patterns","dotnet","dotnet-core","guard","guard-clause","guard-clauses","hacktoberfest","pattern","patterns"],"created_at":"2024-08-01T16:01:39.939Z","updated_at":"2025-04-09T10:32:06.866Z","avatar_url":"https://github.com/ardalis.png","language":"C#","funding_links":["https://github.com/sponsors/ardalis"],"categories":["C#","Validation","C\\#","Architectural Patterns"],"sub_categories":["Exceptions \u0026 Validation"],"readme":"![logo](media/logotype%201024.png)\n\n[![NuGet](https://img.shields.io/nuget/v/Ardalis.GuardClauses.svg)](https://www.nuget.org/packages/Ardalis.GuardClauses)[![NuGet](https://img.shields.io/nuget/dt/Ardalis.GuardClauses.svg)](https://www.nuget.org/packages/Ardalis.GuardClauses)\n![publish Ardalis.GuardClauses to nuget](https://github.com/ardalis/GuardClauses/workflows/publish%20Ardalis.GuardClauses%20to%20nuget/badge.svg)\n\n[![Follow Ardalis](https://img.shields.io/twitter/follow/ardalis.svg?label=Follow%20@ardalis)](https://twitter.com/intent/follow?screen_name=ardalis)\n[![Follow NimblePros](https://img.shields.io/twitter/follow/nimblepros.svg?label=Follow%20@nimblepros)](https://twitter.com/intent/follow?screen_name=nimblepros)\n\n# Guard Clauses\n\nA simple extensible package with guard clause extensions.\n\nA [guard clause](https://deviq.com/design-patterns/guard-clause) is a software pattern that simplifies complex functions by \"failing fast\", checking for invalid inputs up front and immediately failing if any are found.\n\n## Give a Star! :star:\n\nIf you like or are using this project please give it a star. Thanks!\n\n## Usage\n\n```c#\npublic void ProcessOrder(Order order)\n{\n    Guard.Against.Null(order);\n\n    // process order here\n}\n\n// OR\n\npublic class Order\n{\n    private string _name;\n    private int _quantity;\n    private long _max;\n    private decimal _unitPrice;\n    private DateTime _dateCreated;\n\n    public Order(string name, int quantity, long max, decimal unitPrice, DateTime dateCreated)\n    {\n        _name = Guard.Against.NullOrWhiteSpace(name);\n        _quantity = Guard.Against.NegativeOrZero(quantity);\n        _max = Guard.Against.Zero(max);\n        _unitPrice = Guard.Against.Negative(unitPrice);\n        _dateCreated = Guard.Against.OutOfSQLDateRange(dateCreated, dateCreated);\n    }\n}\n```\n\n## Supported Guard Clauses\n\n- **Guard.Against.Null** (throws if input is null)\n- **Guard.Against.NullOrEmpty** (throws if string, guid or array input is null or empty)\n- **Guard.Against.NullOrWhiteSpace** (throws if string input is null, empty or whitespace)\n- **Guard.Against.OutOfRange** (throws if integer/DateTime/enum input is outside a provided range)\n- **Guard.Against.EnumOutOfRange** (throws if an enum value is outside a provided Enum range)\n- **Guard.Against.OutOfSQLDateRange** (throws if DateTime input is outside the valid range of SQL Server DateTime values)\n- **Guard.Against.Zero** (throws if number input is zero)\n- **Guard.Against.Expression** (use any expression you define)\n- **Guard.Against.InvalidFormat** (define allowed format with a regular expression or func)\n- **Guard.Against.NotFound** (similar to Null but for use with an id/key lookup; throws a `NotFoundException`)\n\n## Extending with your own Guard Clauses\n\nTo extend your own guards, you can do the following:\n\n```c#\n// Using the same namespace will make sure your code picks up your \n// extensions no matter where they are in your codebase.\nnamespace Ardalis.GuardClauses\n{\n    public static class FooGuard\n    {\n        public static void Foo(this IGuardClause guardClause,\n            string input, \n            [CallerArgumentExpression(\"input\")] string? parameterName = null)\n        {\n            if (input?.ToLower() == \"foo\")\n                throw new ArgumentException(\"Should not have been foo!\", parameterName);\n        }\n    }\n}\n\n// Usage\npublic void SomeMethod(string something)\n{\n    Guard.Against.Foo(something);\n    Guard.Against.Foo(something, nameof(something)); // optional - provide parameter name\n}\n```\n\n## YouTube Overview\n\n[![Ardalis.GuardClauses on YouTube](http://img.youtube.com/vi/OkE2VeRM4mE/0.jpg)](http://www.youtube.com/watch?v=OkE2VeRM4mE \"Improve Your Code with Ardalis.GuardClauses\")\n\n## Breaking Changes in v4\n\n- OutOfRange for Enums now uses `EnumOutOfRange`\n- Custom error messages now work more consistently, which may break some unit tests\n\n## Nice Visualization of Refactoring to use Guard Clauses\n\nhttps://user-images.githubusercontent.com/782127/234028498-96e206b0-9a70-4aa0-9c36-a62477ea0aa9.mp4\n\nvia [Nicolas Carlo](https://toot.legacycode.rocks/@nicoespeon/110226815487285845)\n\n## References\n\n- [Getting Started with Guard Clauses](https://blog.nimblepros.com/blogs/getting-started-with-guard-clauses/)\n- [How to write clean validation clauses in .NET](https://www.youtube.com/watch?v=Tvx6DNarqDM) (Nick Chapsas, YouTube, 9 minutes)\n- [Guard Clauses (podcast: 7 minutes)](http://www.weeklydevtips.com/004)\n- [Guard Clause](http://deviq.com/guard-clause/)\n\n## Commercial Support\n\nIf you require commercial support to include this library in your applications, contact [NimblePros](https://nimblepros.com/talk-to-us-1)\n\n## Build Notes (for maintainers)\n\n- Remember to update the PackageVersion in the csproj file and then a build on main should automatically publish the new package to nuget.org.\n- Add a release with form `1.3.2` to GitHub Releases in order for the package to actually be published to Nuget. Otherwise it will claim to have been successful but is lying to you.\n\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fardalis%2FGuardClauses","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fardalis%2FGuardClauses","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fardalis%2FGuardClauses/lists"}