diff options
author | colin <colin@cicadas.surf> | 2023-11-30 07:21:49 -0800 |
---|---|---|
committer | colin <colin@cicadas.surf> | 2023-11-30 07:21:49 -0800 |
commit | 40b881dc4740463c038c329b5a472ae0a98eb0a3 (patch) | |
tree | 96fe401a4c984be7ebb776c4e4402376c3c3b259 /terrafirma.lisp | |
parent | ca9bf0df7aa54019416f0d6f0196a38e3aac672a (diff) |
macrolet of validate
Diffstat (limited to 'terrafirma.lisp')
-rw-r--r-- | terrafirma.lisp | 31 |
1 files changed, 15 insertions, 16 deletions
diff --git a/terrafirma.lisp b/terrafirma.lisp index 16938ee..62d063c 100644 --- a/terrafirma.lisp +++ b/terrafirma.lisp @@ -46,26 +46,25 @@ (defvar *type*) (defvar *instance*) -(defmacro validate (check msg &rest args) - (let ((suberr (gensym))) - `(prog1 t - (let (,suberr) - (assert (handler-case ,check (validation-error (sub) (setf ,suberr sub) nil)) - () 'terrafirma::validation-error - :type terrafirma::*type* - :instance terrafirma::*instance* - :reason (format nil ,msg ,@args) - :suberror ,suberr))))) - - (defmacro defvalidator ((var type &key name) &body body) (assert (and var (symbolp var)) (var) "VAR must be a symbol.") (let ((validator-name (cond ((and name (symbolp name)) name) - ((symbolp type) (intern (format nil "VALIDATE-~a" type))) + ((symbolp type) (intern (format nil "VALIDATED-~a" type))) (t (error "Validator Name: Either TYPE must be a symbol or a NAME must be provided."))))) `(defun ,validator-name (,var) - (let ((terrafirma::*type* ',type) - (terrafirma::*instance* ',var)) - ,@body)))) + (macrolet ((validate (check msg &rest args) + (let ((suberr (gensym))) + `(prog1 t + (let (,suberr) + (assert (handler-case ,check (validation-error (sub) (setf ,suberr sub) nil)) + () 'terrafirma::validation-error + :type terrafirma::*type* + :instance terrafirma::*instance* + :reason (format nil ,msg ,@args) + :suberror ,suberr)))))) + + (let ((terrafirma::*type* ',type) + (terrafirma::*instance* ,var)) + ,@body))))) |