noir-clojure / lib-noir

A set of libraries for ring apps, including stateful sessions.
Eclipse Public License 1.0
483 stars 47 forks source link

Is there a way to clear validation errors? #91

Closed xavi closed 10 years ago

xavi commented 10 years ago

I'm thinking of something like Rails' ActiveModel::Errors#clear method http://api.rubyonrails.org/classes/ActiveModel/Errors.html#method-i-clear

The reason I would like something like this is because of the following use case...

I have some code that imports a set of items from a CSV file. For each item, a function is called that validates the item and if it's ok it adds it to the database, otherwise it sets validation errors using lib-noir. At the end of the import process I would like to provide a report with any errors associated to the corresponding item. The problem is that the errors on an item are added up to the errors on all the previous items, so it's not possible to associate each error with the corresponding item.

If there were a function to clear the error messages, it could be called before calling the function that validates an item, and so, after running that function, I would know that any errors returned by noir.validation/get-errors would correspond to the last item and not any other.

yogthos commented 10 years ago

So, basically you'd just like to add something like?

(defn clear-errors! []
  (reset! *errors* {}))
xavi commented 10 years ago

Yes. I know that this could be added to the application code instead, but then that code would be coupled to the internals of lib-noir, which could change in a future version and break that code.

Raynes commented 10 years ago

He didn't tell you to add it to your own application. :tongue:

@yogthos This seems like a reasonable addition just because of the latter half of his last message 'but then that code would be coupled to the internals of lib-noir, which could change in a future version and break that code.'.

yogthos commented 10 years ago

I'm ok making the change, just wanted to confirm that I understood it correctly.

yogthos commented 10 years ago

I also notice that validation ns doesn't follow the ! convention in set-error so I'm going to keep that consistent and call it clear-errors. At some point, maybe 0.8.0 release could make a change to the API to make sure any mutation functions are marked appropriately.

@xavi
I just pushed out 0.7.8 with the change, if it looks good to you then we can close it.

xavi commented 10 years ago

Currently the docstring says "removes any errors set via set-error". I suggest to remove the reference to set-error, as errors can also be set by rule, and clear-errors obviously removes those too. So, I think that just "Removes any errors." would be more appropriate.

Apart from this detail, I've already tried the new clear-errors and it works great. Thanks!

yogthos commented 10 years ago

Right, I forgot about the rule setting errors. Docs are updated and closing.