I have an application that can accept CSV files to run some operations. The files look like:
CREATE USER:username,last_name,first_name,age
user1,Smith,John,23
user2,Poppins,Mary,257
There are a number of commands that can be used, each with a specific "syntax" in terms of what columns are required / authorised, the type of data in each column, valid values as well as more complex rules (e.g. 2 users can't have the same username, the username must not already be present in the database, etc.).
At the moment, each of those commands correspond to a specific class in my OO model which runs the validation with code (if (type(column) != STRING) blabla
) before executing the commands.
This is becoming cumbersome and I would like to extract the validation rules in human readable format, say:
Command=CREATE USER
Field='username', required, text, max_length=255, min_length=1,
unique, not exists in db table 'User'
Field='age', optional, number, min=0, max=120
....
My code could then become:
List<Error> errors = validate(csvFile, rulesFile);
if (errors.isEmpty()) //good to go
However the more I think about it, the more it seems like it's going to be a major headache to code and effectively a whole project in itself, possibly not worth the time spent.
What approach could I take to extract rules in human readable format without unduly complicating the validation code?