Code4PuertoRico / paravotar

Una herramienta para practicar tu voto con la papeleta de Puerto Rico.
https://paravotar.org
MIT License
18 stars 8 forks source link

Ballot Validator library #172

Closed Jpadilla1 closed 3 years ago

Jpadilla1 commented 3 years ago

Is your feature request related to a problem? Please describe. We require a ballot validator library where we can provide feedback to the user if the selections they have made are valid or not.

Describe the solution you'd like I have a proposed solution in this PR #171

Additional context The version linked here #171 can be described as a rule based approach. Where we have a validate function that accepts one of 3 Ballots and a BallotType. The return value is an object describing the status of the validation and the allowed & denied outcomes.

The way the rules work is by using a RuleGroup which has an array of rule instances to run against the ballot. Some rules are shared in between ballots and each rule only validates 1 thing. Example, the NoEmptyBallotRule checks if the ballot is empty. If it is the rule returns a deny outcome which means the ballot in invalid.

Rules of writing a rule:

Example output:

{
  "status": "failure",
  "outcomes": {
    "allowed": [
      {
        "outcome": "allow",
        "ruleName": "NoMoreThanOnePartySelectedRule"
      }
    ],
    "denied": [
      {
        "outcome": "deny",
        "ruleName": "NoEmptyBallotRule"
      }
    ]
  }
}

CC/ @code4puertorico/paravotar

froi commented 3 years ago

This looks good and @eluciano11 has given his 👍🏼 in #171.

I do have one question. Are you looking to implement a rules engine? If so https://github.com/froi/simple-rules-engine/ might help you with some ideas, boilerplate, or just a a dependency to use.

Jpadilla1 commented 3 years ago

lol yeah this is pretty much a rules engine

Jpadilla1 commented 3 years ago

For clarity we are moving forward with this approach #171