{"id":21575457,"url":"https://github.com/dzfweb/fluentspecification","last_synced_at":"2025-04-10T16:30:24.534Z","repository":{"id":92147929,"uuid":"105018162","full_name":"dzfweb/FluentSpecification","owner":"dzfweb","description":"A small validation library for .NET that uses a fluent interface and lambda expressions for building validations using Specification Pattern","archived":false,"fork":false,"pushed_at":"2017-09-27T16:53:49.000Z","size":34,"stargazers_count":10,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-04-08T17:53:18.111Z","etag":null,"topics":["specification-pattern","validations"],"latest_commit_sha":null,"homepage":null,"language":"C#","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/dzfweb.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"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}},"created_at":"2017-09-27T12:55:17.000Z","updated_at":"2023-12-23T18:23:20.000Z","dependencies_parsed_at":null,"dependency_job_id":"1d3fb431-cee7-4bfc-863f-69df0782ded1","html_url":"https://github.com/dzfweb/FluentSpecification","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzfweb%2FFluentSpecification","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzfweb%2FFluentSpecification/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzfweb%2FFluentSpecification/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/dzfweb%2FFluentSpecification/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/dzfweb","download_url":"https://codeload.github.com/dzfweb/FluentSpecification/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248252691,"owners_count":21072699,"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":["specification-pattern","validations"],"created_at":"2024-11-24T12:13:15.191Z","updated_at":"2025-04-10T16:30:24.506Z","avatar_url":"https://github.com/dzfweb.png","language":"C#","funding_links":[],"categories":[],"sub_categories":[],"readme":"# FluentSpecification\n![FluentSpecification](https://github.com/dzfweb/FluentSpecification/raw/master/ico/ico.png)\n\nA small validation library for .NET that uses a fluent interface for building validations using Specification Pattern\n\n#### In computer programming, the specification pattern is a particular software design pattern, whereby business rules can be recombined by chaining the business rules together using boolean logic. The pattern is frequently used in the context of domain-driven design.\n\n## Benefits\n- Easy to implement unit test, once you can test all specification separately;\n- Easy to understand all business validation, once is more readable than a lot of \"ifs\" in one same code block;\n- Easy to refactoring, once you can refactor only the one specification without interfere the others;\n\n\n## Instalation\n\n### Package Manager\n`Install-Package DzfWeb.FluentSpecification`\n\n### .NET CLI\n`dotnet add package DzfWeb.FluentSpecification`\n\n\n## Usage\n\n\n#### 1- Given a Entity\n```\npublic class Person\n{\n    public string FirstName { get; set; }\n    public string LastName { get; set; }\n    public string Email { get; set; }\n}\n```\n\n#### 2- Create all the specifications\nFor each business validation, create a new specification file with the validation\n```\n[SpecificationError(PersonValidation.InvalidName)]\npublic class PersonNameSpecification : Specification\u003cPerson\u003e\n{\n    public override bool IsSatisfiedBy(Person entity) =\u003e\n        !string.IsNullOrEmpty(entity.FirstName) \u0026\u0026\n        !string.IsNullOrEmpty(entity.LastName);\n}\n``` \n\n```\n[SpecificationError(PersonValidation.InvalidEmail)]\npublic class PersonEmailSpecification : Specification\u003cPerson\u003e\n{\n    public override bool IsSatisfiedBy(Person entity) =\u003e\n        !string.IsNullOrEmpty(entity.Email) \u0026\u0026 \n        new EmailAddressAttribute().IsValid(entity.Email);\n}\n```\n\n#### 3- Create a validator\nCreate a validator grouping all the specifications \n```\npublic class PersonValidator : Validator\u003cPerson\u003e\n{\n    public PersonValidator(PersonNameSpecification personNameSpecification,\n                            PersonEmailSpecification personEmailSpecification) \n        : base(personNameSpecification,\n              personEmailSpecification)\n    { }\n}\n```\n\n#### 4- Use the validator\n```\nvar result = _personValidator.IsValid(person);\n```\n\n#### 5- Get all broken specifications\n\n```\nforeach(var item in _personValidator.InvalidRules)\n{\n    //item correspond to SpecificationErrorAttribute value defined on specification file\n    Console.WriteLine(item);\n}\n```\n\n## Customize the validation\n\n#### You can filter which specifications you want to use in some cases\n```\nvar isValidToSendEmail = _personValidator\n                        .FilterRules(typeof(PersonEmailSpecification))\n                        .IsValid(person);\n\n```\n\n#### You can add parameters to be used during the validation\n```\nvar isValidToCreate = _personValidator\n                .AddParameter(\"RestrictedEmail\", \"douglas.franco@dzfweb.com.br\")\n                .IsValid(person);\n```\n\n```\n[SpecificationError(PersonValidation.InvalidEmail)]\npublic class PersonEmailSpecification : Specification\u003cPerson\u003e\n{\n    public override bool IsSatisfiedBy(Person entity) =\u003e\n        !string.IsNullOrEmpty(entity.Email) \u0026\u0026 \n        new EmailAddressAttribute().IsValid(entity.Email) \u0026\u0026\n        entity.Email != (string)Parameters.GetValueOrDefault(\"RestrictedEmail\", \"\");\n}\n```\n\n\n\n\n\n## Samples\nFor more samples, visit:\nhttps://github.com/dzfweb/FluentSpecification/blob/master/FluentSpecification/FluentSpecification.Test/PersonTest.cs","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdzfweb%2Ffluentspecification","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdzfweb%2Ffluentspecification","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdzfweb%2Ffluentspecification/lists"}