Let\'s say I have the following class declaration:
(defclass foo-class ()
((bar :initarg :bar
:type list)))
When I create an instance
The sanity-clause library just merged a feature for this yesterday.
Sanity clause is a data validation/contract library. You might use it for configuration data, validating an api response, or documents from a datastore. In a dynamically typed langauge, it helps you define clearly defined areas of doubt and uncertainty. We should love our users, but we should never blindly trust their inputs.
To make use of it, you define schemas, which can be property lists with symbols for keys and instances of :class:
sanity-clause.field:field
So:
(defclass person ()
((favorite-dog :type symbol
:field-type :member
:members (:wedge :walter)
:initarg :favorite-dog
:required t)
(age :type (integer 0)
:initarg :age
:required t)
(potato :type string
:initarg :potato
:required t))
(:metaclass sanity-clause.metaclass:validated-metaclass))
;; bad dog:
(make-instance 'person :favorite-dog :nope)
; Evaluation aborted on Error converting value for field #:
Value "NOPE" couldn't be found in set (WEDGE WALTER)
;; bad age:
(make-instance 'person :age -1 :favorite-dog :walter)
; Evaluation aborted on Error validating value -1 in field #:
* Value -1 didn't satisfy condition "must be larger than 0"
;; missing potato:
(make-instance 'person :age 7 :favorite-dog :walter)
; Evaluation aborted on A value for field POTATO is required but none was provided..
;; all OK:
(make-instance 'person :age 1 :favorite-dog :walter :potato "patate")
#