MarkOates / blast

0 stars 0 forks source link

Introduce validators into quintessence #38

Open MarkOates opened 11 months ago

MarkOates commented 11 months ago

in quintessence:

 validate:
  - class: VectorValidator<std::string>
    init_with: '{ "who", "ha" }'
    validate_with:
      - contains("who")
      - min_token_count(2)
      - tokens_not_empty()

would produce:

{
   VectorValidator<std::string> validator({ "who", "ha" });
   std::vector<std::string> all_validator_errors;

   std::vector<std::string> validation_1 = validator.contains("who");
   all_validator_errors(all_validator_errors.end(), validation_1.begin(), validation_1.end());

   std::vector<std::string> validation_2 = validator.min_token_count(2);
   all_validator_errors(all_validator_errors.end(), validation_2.begin(), validation_2.end());

   std::vector<std::string> validation_3 = validator.tokens_not_empty();
   all_validator_errors(all_validator_errors.end(), validation_3.begin(), validation_3.end());

   if (!all_validator_errors.empty())
   {
      std::stringstream error_message;
      error_message << "ClassName::method: error: Following validations failed: ";
      ... list out all_validator_errors ...
      throw std::runtime_error(error_message);
   }
}
MarkOates commented 11 months ago

Most importantly the validator class is designed to have very user-friendly error messages that include the the data that didn't match validation. It would also implicitly know the class and method which so it could be included in the error message.

MarkOates commented 11 months ago

Also consider introducing custom quintessence specifications so an end user could add in the "validate" element in their own projects without it having to become a major part of the standard quintessence schema.

MarkOates commented 11 months ago

You could have a standard validator with functions and remove the need for defining a class. For example:

validate:
  - not_nullptr(a_pointer)
  - vector_contains(tokens, "who")
  - min_token_count(tokens, 2)
  - tokens_not_empty(tokens)

A class would be implicitly included:

class StandardValicator that has the methods std::vector<std::string> vector_contains(...) ..., etc.

You may need to validate in stages

validate_in_groups:
  - validations:
     - not_nullptr(a_pointer)
  - validations:
    - vector_contains(a_pointer->tokens, "who")
    - min_token_count(tokens, 2)
    - tokens_not_empty(tokens)