1

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?

2

Smart data structures and dumb code works a lot better than the other way around. -- Eric S. Raymond, The Cathedral and the Bazaar

You can use XML or JSON with schemas. Validation rules should be machine-readable first, and human-readable if possible. Consider not inventing your own format and using XML Schema or JSON Schema instead. Switching to XML/JSON should not be very difficult, since there are a lot of libs available.

What you have now is basically some form of RPC based on CSV format. Maybe you should look into JSON-RPC or something similar.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.