{"id":20904655,"url":"https://github.com/sage/validation_profiler","last_synced_at":"2025-10-13T04:13:42.958Z","repository":{"id":59159090,"uuid":"75098585","full_name":"Sage/validation_profiler","owner":"Sage","description":null,"archived":false,"fork":false,"pushed_at":"2023-06-28T09:39:45.000Z","size":67,"stargazers_count":1,"open_issues_count":0,"forks_count":1,"subscribers_count":23,"default_branch":"master","last_synced_at":"2025-10-02T01:20:44.302Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"Ruby","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/Sage.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"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,"dei":null,"publiccode":null,"codemeta":null,"zenodo":null}},"created_at":"2016-11-29T16:10:21.000Z","updated_at":"2023-06-27T12:31:09.000Z","dependencies_parsed_at":"2025-05-13T05:40:58.131Z","dependency_job_id":null,"html_url":"https://github.com/Sage/validation_profiler","commit_stats":null,"previous_names":[],"tags_count":12,"template":false,"template_full_name":null,"purl":"pkg:github/Sage/validation_profiler","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fvalidation_profiler","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fvalidation_profiler/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fvalidation_profiler/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fvalidation_profiler/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/Sage","download_url":"https://codeload.github.com/Sage/validation_profiler/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/Sage%2Fvalidation_profiler/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":279002342,"owners_count":26083356,"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","status":"online","status_checked_at":"2025-10-09T02:00:07.460Z","response_time":59,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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":[],"created_at":"2024-11-18T13:18:23.789Z","updated_at":"2025-10-13T04:13:42.939Z","avatar_url":"https://github.com/Sage.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ValidationProfiler\n[![RSpec](https://github.com/Sage/validation_profiler/actions/workflows/rspec.yml/badge.svg)](https://github.com/Sage/validation_profiler/actions/workflows/rspec.yml)\n[![Maintainability](https://api.codeclimate.com/v1/badges/563268781aecd347a9df/maintainability)](https://codeclimate.com/github/Sage/validation_profiler/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/563268781aecd347a9df/test_coverage)](https://codeclimate.com/github/Sage/validation_profiler/test_coverage)\n[![Gem Version](https://badge.fury.io/rb/validation_profiler.svg)](https://badge.fury.io/rb/validation_profiler)\n\nWelcome to ValidationProfiler. This is a validation framework that allows you to seperate validation logic away from your objects and into validation profiles that can be re-used and changed without affecting your objects.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'validation_profiler'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install validation_profiler\n\n## Usage\n\nFirst you need to create a validation profile to hold the validation logic you want to apply, validation profiles must inherit from the ValidationProfile base class or another validation profile.\n\n```ruby\nclass SignUpValidationProfile\n  extend ValidationProfile\n  .....\nend\n```\n\nThen you specify validation rules that should be checked when this profile is validated against an object.\n\n```ruby\nclass SignUpValidationProfile\n    extend ValidationProfiler\n\n  validates :age, :min, { value: 18 }\n  validates :email, :email\n  .....\nend\n```\n\nWhen specifying a validation rule you need to specify the following arguments:\n\n - Field name\n - Rule key\n - Hash containing any options required for the validation rule\n\nSo if we take another look at the first validation rule we specified in the **SignUpValidationProfile** above:\n\n\tField name = :age\n\tRule key = :min\n\tAttributes Hash = { value: 18 }\n\nThis validation statement will be interpreted as:\n*\"The field :age must have a minimum value of 18\"*\n\nTo use a validation profile you need to make a call to the **ValidationManager** class, and pass the object you want to validate along with the profile you want to use for the validation:\n\n```ruby\n# create the validation manager\nmanager = ValidationProfiler::Manager.new\n\n# call the validate method and pass the object and profile\nresult = manager.validate(user, profile)\n```\n\nCalls to the validate method will return a **ValidationProfiler::ManagerResult** that will detail the results of the validation.\n\nA **ValidationProfiler::ManagerResult** has the following attributes:\n\n - #outcome = [Boolean] overall outcome of the validation (passed or failed)\n - #errors = [Array] containing details of each field error that occurred during validation.\n\nEach item in the errors array has the following attributes:\n\n - #field = The name of the field that this error occurred for.\n - #message = A message that describes the validation error\n\n## Validation Rules\n\n**RequiredValidationRule**\n\nThis rule is used to specify a field must contain a value:\n\n```ruby\nvalidates :name, :required\n```\n\nAttributes:\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n\n----------\n\n#\n**LengthValidationRule**\n\nThis rule is used to specify a [String] or [Array] must be of a certain length:\n\n```ruby\nvalidates :name, :length, { min: 5, max: 10 }\n```\n\nAttributes:\n\n - **:min** [Numeric]\n This is used to specify the minimum length of the field value.\n\n - **:max** [Numeric]\n This is used to specify the maximum length of the field value.\n\n \u003e **:min** \u0026 **:max** can be included together or independently providing at least 1 is specified.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n\n----------\n\n#\n**MinValidationRule**\n\nThis rule is used to specify a minimum value a [DateTime] or [Numeric] field must have.\n\n```ruby\nvalidates :age, :min, { value: 18 }\n```\n\nAttributes:\n\n - **:value** [Numeric/DateTime]\n This is used to specify the minimum value of the field.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n\t This is used to specify if this rule should only be executed when the field contains a value.\n\u003e **True** always executes, **False** only executes when the field contains a value)*\n\n----------\n\n#\n**MaxValidationRule**\n\nThis rule is used to specify a maximum value a [DateTime] or [Numeric] field must have.\n\n```ruby\nvalidates :age, :max, { value: 25 }\n```\n\nAttributes:\n\n - **:value** [Numeric/DateTime]\n This is used to specify the maximum value of the field.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed the field contains a value.\n\u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**EmailValidationRule**\n\nThis rule is used to specify a field value must contain a valid email address.\n\n```ruby\nvalidates :email_address, :email, { multiple: true }\n```\n\nAttributes:\n \n - **:multiple** [Boolean] [Default=False] [Optional]\n This is used to allow multiple email addresses to be entered. Addresses should be separated by a comma(,) or semicolon(;)\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n\u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**RegexValidationRule**\n\nThis rule is used to specify a regex pattern that a field value must validate against.\n\n```ruby\nvalidates :email, :regex, { regex: /^[^@]+@[^@]+\\.[^@]+$/ }\n```\n\nAttributes:\n\n - **:regex** [Regex]\nThis is used to specify the regex pattern.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n\u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**MatchValidationRule**\n\nThis rule is used to specify a field value must match the value of another field.\n\n```ruby\nvalidates :confirm_password, :match, { field: :password }\n```\n\nAttributes:\n\n - **:field** [Symbol]\nThis is used to specify the name of the other field this field's value must match.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**ConditionValidationRule**\n\nThis rule is used to specify a condition statement.\n\ne.g.\n\n\u003e format:\n\u003e\n\u003e When [:condition_field] [:condition_expression] [:condition_value] then [:field] [:field_expression] [:field_value]\n\u003e\n\u003ecould be read as:\n\u003e\n\u003e When :age \u003e= 18 then :accept == true\n\n```ruby\nvalidates :accept, :condition, { condition_field: :age, condition_expression: '\u003e=', condition_value: 18, field_expression: '==', field_value: true }\n```\n\nAttributes:\n\n - **:condition_field** [Symbol]\nThis is used to specify the name of the condition field.\n\n - **:condition_expression** [String]\nThis is used to specify the expression to use between the condition_field and the condition_value.\n\n\u003e **Supported expression types:**\n\u003e '=='\n\u003e '\u003e'\n\u003e '\u003e='\n\u003e '\u003c'\n\u003e '\u003c='\n\u003e '!='\n\n\n - **:condition_value** [String/Numeric/DateTime/nil]\nThis is used to specify the value to use for the condition statement.\n\n - **:field_expression** [String]\nThis is used to specify the expression to use between the field's value and the field_value attribute.\n\n - **:field_value** [String/Numeric/DateTime/nil]\nThis is used to specify the value to use for the field statement.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n---------\n\n#\n**NotAllowedValidationRule**\n\nThis rule is used to specify a field must not contain a value:\n\n```ruby\nvalidates :hidden, :not_allowed\n```\n\n Attributes:\n\n  - **:message** [String] [Optional]\n  This is used to allow a custom error message to be specified.\n\n---------\n\n#\n**ListValidationRule**\n\nThis rule is used to specify a field value must be within a specified list of accepted values:\n\n```ruby\nvalidates :name, :list, { list: ['dog','cat','rabbit'] }\n```\n\nAttributes:\n\n - **:list** [Array]\n This is used to specify the list of values that are acceptable for this field.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**ChildValidationRule**\n\nThis rule is used to specify a field with a child object should validate against another validation profile:\n\n```ruby\nvalidates :address, :child, { profile: AddressValidationProfile }\n```\n\nAttributes:\n\n - **:profile** [Class]\n This is used to specify the validation profile to use for the nested child object\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n\n------------\n\n#\n**IntegerValidationRule**\n\nThis rule is used to specify a field must be a valid Integer\n\n```ruby\nvalidates :age, :int, { required: false }\n```\n\nAttributes:\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**DecimalValidationRule**\n\nThis rule is used to specify a field must be a valid Decimal\n\n```ruby\nvalidates :amount, :decimal, { required: false }\n```\n\nAttributes:\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**DateValidationRule**\n\nThis rule is used to specify a field must be a valid Date\n\n```ruby\nvalidates :dob, :date, { required: false }\n```\n\nAttributes:\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**TimeValidationRule**\n\nThis rule is used to specify a field must be a valid Time. Value can be either full datetime '12-Mar-2016 12:30:10' or seconds since epoch 1476344603.\n\n```ruby\nvalidates :updated, :time, { required: false }\n```\n\nAttributes:\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n#\n**GuidValidationRule**\n\nThis rule is used to specify a field must be a valid Guid.\n\n```ruby\nvalidates :id, :guid, { hyphens: true, brackets: true, required: true }\n```\n\nAttributes:\n\n - **:hyphens** [Boolean] [Default=false] [Optional]\n  This is used to allow a the guid value to contain hyphens.\n\n - **:brackets** [Boolean] [Default=false] [Optional]\n This is used to allow a the guid value to contain brackets. Both ( \u0026 { brackets are supported.\n\n - **:message** [String] [Optional]\n This is used to allow a custom error message to be specified.\n\n - **:required** [Boolean] [Default=True] [Optional]\n This is used to specify if this rule should only be executed when the field contains a value.\n \u003e **True** always executes, **False** only executes when the field contains a value)\n\n----------\n\n##Custom Validation Rules\n\nTo create a custom validation rule you must create a class that inherits from the **ValidationRule** base class and implement the #error_message and #validate methods, see the **RequiredValidationRule** below as an example:\n\n```ruby\nclass RequiredValidationRule \u003c ValidationRule\n  #implement this method to return the error message when\n  #this rule fails validation\n  def error_message(field, attributes = {})\n    #check if custom message was specified\n    if attributes[:message] == nil\n      #return default method\n      \"#{field} is not valid\"\n    else\n      #return custom message\n      attributes[:message]\n    end\n  end\n\n  def validate(obj, field, attributes = {})\n    #attempt to get the field value from the object\n    field_value = get_field_value(obj, field)\n\n    if field_value == nil\n      return false\n    end\n\n    return !field_value.empty?\n  end\nend\n```\n\nThe **ValidationRule** base class provides the `#get_field_value(obj, field)` method to cater for fetching the field value from the object to perform the validation against.\n\nThis rule can be added to the ValidationProfiler::Manager, e.g.:\n\n```ruby\nvalidation_manager = ValidationProfiler::Manager.new\nvalidation_manager.add_rule(:custom_required, RequiredValidationRule)\n```\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. You can also run `bin/console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/sage/validation_profiler. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThis gem is available as open source under the terms of the\n[MIT licence](LICENSE).\n\nCopyright (c) 2018 Sage Group Plc. All rights reserved.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Fvalidation_profiler","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsage%2Fvalidation_profiler","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsage%2Fvalidation_profiler/lists"}