Skip to content

Commit

Permalink
Merge branch 'develop'
Browse files Browse the repository at this point in the history
  • Loading branch information
cxxxr committed Sep 6, 2023
2 parents eebdc32 + 0c89888 commit 7904e51
Show file tree
Hide file tree
Showing 17 changed files with 476 additions and 318 deletions.
6 changes: 5 additions & 1 deletion apispec.asd
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,16 @@
:author "Eitaro Fukamachi"
:license "BSD 3-Clause"
:description "API request / response validations"
:depends-on ("apispec/main")
:depends-on ("openapi-parser"
"apispec/main")
:pathname "src"
:in-order-to ((test-op (test-op "apispec/tests"))))

(register-system-packages "lack-request" '(#:lack.request))
(register-system-packages "lack-response" '(#:lack.response))
(asdf:register-system-packages "openapi-parser" '(#:openapi-parser
#:openapi-parser/schema
#:openapi-parser/schema/3/interface))

(defsystem "apispec/tests"
:depends-on ("apispec"
Expand Down
4 changes: 2 additions & 2 deletions src/classes/encoding/parse.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
#:object
#:array-items
#:coerce-data
#:*coerce-integer-string-to-boolean*)
#:*coerce-string-to-boolean*)
(:shadowing-import-from #:apispec/classes/schema
#:byte
#:number
Expand Down Expand Up @@ -83,7 +83,7 @@
(let ((*multipart-force-stream* nil))
(parse-body value content-type))
(declare (ignore parsed-headers))
(let ((*coerce-integer-string-to-boolean*
(let ((*coerce-string-to-boolean*
(starts-with-subseq "application/x-www-form-urlencoded" (string-downcase content-type))))
(if (and (starts-with-subseq "application/x-www-form-urlencoded" (string-downcase content-type))
(encoding-style encoding))
Expand Down
4 changes: 3 additions & 1 deletion src/classes/operation.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,9 @@
(deprecated :type boolean
:initarg :deprecated
:initform nil
:reader operation-deprecated-p)))
:reader operation-deprecated-p)
(%schema :initarg :%schema
:reader operation-schema)))

(defstruct (apispec-request (:include request)
(:conc-name request-))
Expand Down
5 changes: 3 additions & 2 deletions src/classes/parameter/parse.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
#:apispec/classes/parameter/errors)
(:import-from #:apispec/classes/schema
#:coerce-data
#:*coerce-integer-string-to-boolean*)
#:*coerce-string-to-boolean*)
(:import-from #:apispec/complex
#:parse-complex-string
#:parse-complex-parameter)
Expand Down Expand Up @@ -43,6 +43,7 @@
((eq value *empty*)
(when (parameter-required-p parameter)
(push name missing))
#+(or)
(push (cons name nil) results))
(t
(let ((parsed-value
Expand Down Expand Up @@ -154,7 +155,7 @@
(push (cons name nil) results))
(t
(push (cons (parameter-name parameter)
(let ((*coerce-integer-string-to-boolean* t))
(let ((*coerce-string-to-boolean* t))
(handler-case
(coerce-data
(parse-complex-parameter cookies
Expand Down
21 changes: 13 additions & 8 deletions src/classes/schema/coerce.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
(:import-from #:cl-ppcre)
(:import-from #:local-time)
(:export #:coerce-data
#:*coerce-integer-string-to-boolean*))
#:*coerce-string-to-boolean*))
(in-package #:apispec/classes/schema/coerce)

(defgeneric coerce-data (value schema)
Expand Down Expand Up @@ -124,16 +124,19 @@
(local-time::invalid-timestring ()
(error 'schema-coercion-failed :value value :schema schema))))

(defvar *coerce-integer-string-to-boolean* nil)
(defvar *coerce-string-to-boolean* nil)

(defmethod coerce-data (value (schema boolean))
(typecase value
(cl:string
(unless *coerce-integer-string-to-boolean*
(error 'schema-coercion-failed :value value :schema schema))
(cond
((equal value "1") t)
((equal value "0") nil)
((and *coerce-string-to-boolean* (equal value "1")) t)
((and *coerce-string-to-boolean* (equal value "0")) nil)
((and *coerce-string-to-boolean* (equal value "true")) t)
((and *coerce-string-to-boolean* (equal value "false")) nil)
((and *coerce-string-to-boolean* (equal value "")
(schema-has-default-p schema))
(schema-default schema))
(t (error 'schema-coercion-failed :value value :schema schema))))
(cl:boolean value)
(otherwise
Expand All @@ -150,9 +153,11 @@
(defmethod coerce-data (value (schema array))
(let ((value
(handler-case
(coerce value 'vector)
(typecase value
(cl:string (error 'schema-coercion-failed :value value :schema schema))
(t (coerce value 'vector)))
(type-error ()
(error 'schema-coercion-failed :valeu value :schema schema)))))
(error 'schema-coercion-failed :value value :schema schema)))))
(cond ((array-items schema)
(map 'vector
(lambda (item)
Expand Down
4 changes: 4 additions & 0 deletions src/classes/schema/core.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
#:date-time
#:email
#:uuid
#:json

#:boolean

Expand Down Expand Up @@ -245,6 +246,9 @@
(defclass uuid (string)
((format :initform "uuid")))

(defclass json (string)
((format :initform "json")))

(defclass boolean (schema)
((type :initform "boolean")))

Expand Down
1 change: 1 addition & 0 deletions src/classes/schema/errors.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
#:schema-object-error-value
#:schema-object-error-schema
#:schema-validation-failed
#:schema-multiple-error
#:schema-oneof-error
#:schema-anyof-error))
(in-package #:apispec/classes/schema/errors)
Expand Down
13 changes: 11 additions & 2 deletions src/classes/schema/validate.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,8 @@
(:import-from #:apispec/utils
#:association-list
#:email-format-p
#:uuid-format-p)
#:uuid-format-p
#:json-string-p)
(:import-from #:cl-ppcre)
(:import-from #:local-time)
(:export #:schema-validation-failed
Expand Down Expand Up @@ -136,6 +137,13 @@
(error 'schema-validation-failed
:value value
:schema schema))

(when (and (equal "json" (schema-format schema))
(not (json-string-p value)))
(error 'schema-validation-failed
:value value
:schema schema))

t)

(defmethod validate-data (value (schema binary))
Expand Down Expand Up @@ -194,7 +202,8 @@
;; Array Type

(defmethod validate-data (value (schema array))
(unless (arrayp value)
(unless (and (not (stringp value))
(arrayp value))
(error 'schema-validation-failed
:value value
:schema schema
Expand Down
8 changes: 4 additions & 4 deletions src/complex.lisp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#:property-type
#:schema
#:coerce-data
#:*coerce-integer-string-to-boolean*)
#:*coerce-string-to-boolean*)
(:shadowing-import-from #:apispec/classes/schema
#:array)
(:import-from #:cl-ppcre)
Expand Down Expand Up @@ -93,7 +93,7 @@
(values (ppcre:scan-to-strings "(?<=\\.)([^\\.]+)" value)))))

(defun parse-form-value (parameters name &key as explode)
(let ((*coerce-integer-string-to-boolean* t))
(let ((*coerce-string-to-boolean* t))
(coerce-data
(typecase as
(array
Expand Down Expand Up @@ -189,9 +189,9 @@
(error "Unexpected style: ~S" style))))

(defun parse-complex-parameter (alist name style explode schema)
(assert (association-list-p alist 'string 'string))
(assert (association-list-p alist 'string '(or null string)))
(check-type schema schema)
(let ((*coerce-integer-string-to-boolean* (string= style "form")))
(let ((*coerce-string-to-boolean* (string= style "form")))
(coerce-data
(cond
((equal style "form")
Expand Down
Loading

0 comments on commit 7904e51

Please sign in to comment.