Check params
Switch branches/tags
Nothing to show
Clone or download
Permalink
Failed to load latest commit information.
src Add a name of conversions. Oct 14, 2018
tests Rewrite (WIP). Oct 9, 2018
.gitignore Initial commit. Feb 24, 2016
.travis.yml Allow failures on CMUCL because quickload couldn't be done. Oct 14, 2018
README.markdown README. Oct 14, 2018
safety-params.asd v0.3.0 Oct 14, 2018

README.markdown

Safety-Params

Build Status Coverage Status Quicklisp dist

Safety-Params is for checking values.

Upgrading Notes to v0.3.0

This library is used to be called "Sanitized-Params" which is designed to sanitize, which means it doesn't raise any errors if the specified condition is failed and omits the key-values.

However, I noticed that it is used mainly for validating values. That made me decide to revise its APIs and give 2 macros, sanitize and validate.

Additionally, I found that value-conversions are often following after a validation. For instance, a code expecting an integer but its input could be a string.

Since v0.3.0, the value-conversions features is added:

(validate
 (alist
  (satisfies "amount" (being integer)))
 '(("amount" . "108")))
;=> (("amount" . 108))  <- Implicit conversion "108" -> 108

Usage

Checking values

List

(validate
 (list-of #'integerp)
 '(1 2 3))
;=> (1 2 3)

(validate
 (list-of #'integerp)
 '(1 2 "3" 4))
;-> ASSERTION-FAILD: Assertion #<FUNCTION INTEGERP> for (1 2 "3" 4) failed

Association List

(defvar *params*
  '(("id" . "4e3c89aa-4244-4014-ae36-dfaf1dc969f5")
    ("amount" . 10)))

;; Validating key-values
(validate
 (alist
  (satisfies "id" #'stringp)
  (satisfies "amount" #'integerp))
 *params*)

;; Checking permitted key-values
(validate
 (alist
  (permits "id" "amount"))
 *params*)

;; Checking required key-values
(validate
 (alist
  (requires "id"))
 *params*)

Nested example

(validate
 (alist
  (requires "name")
  (satisfies "email" #'listp)
  (satisfies "friends"
             (list-of
              (alist
               (requires "name")
               (satisfies "family" (list-of
                                    (alist (permits "name"))))
               (satisfies "hobbies" #'listp)))))
 '(("name" . "Eitaro Fukamachi")
   ("email" . ("e.arrows@gmail.com" "another@gmail.com"))
   ("friends" . ((("name" . "Masatoshi Sano")
                  ("family" . ())
                  ("hobbies" . ("rocket" "lisp")))))))
;=> (("name" . "Eitaro Fukamachi") ("email" "e.arrows@gmail.com" "another@gmail.com")
;    ("friends" (("name" . "Masatoshi Sano") ("family") ("hobbies" "rocket" "lisp"))))

Converting values

(validate
 (list-of (being integer))
 '(1 2 "3" 4))
;=> (1 2 3 4)

Sanitizing values

(sanitize
 (list-of #'integerp)
 '(1 2 "3" 4))
;=> (1 2 4)

Author

Copyright

Copyright (c) 2016 Eitaro Fukamachi (e.arrows@gmail.com)

License

Licensed under the BSD 2-Clause License.