Skip to content

Commit 4877802

Browse files
committed
Allow validators that use cl types
1 parent 0d0ac6c commit 4877802

File tree

1 file changed

+66
-2
lines changed

1 file changed

+66
-2
lines changed

validation.lisp

+66-2
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@
2828

2929
(defun validate-with-schema (schema data
3030
&key
31-
(format :json)
3231
(collect-errors *collect-validation-errors*)
3332
(error-p *signal-validation-errors*))
3433
"Validate input using schema.
@@ -38,7 +37,7 @@ Input can be a string or an association list.
3837
Args:
3938
- schema (symbol or schema): The schema
4039
- data (alist): The data to validate.
41-
- format (keyword): The data format
40+
- format (keyword): The data format.
4241
- collect-errors (boolean): If true, collect all the validation errors. If false, return the first validation error found. Default: true.
4342
- error-p (boolean): If true, when validation errors are found, a validation error is signaled. If false, the validation errors are returned as the function result and no error is signaled."
4443
(let ((*collect-validation-errors* collect-errors)
@@ -137,7 +136,25 @@ Args:
137136
(schema-validate (second schema) val))
138137
data))
139138

139+
(defmethod %schema-validate ((schema-type (eql 'list)) schema data &optional attribute)
140+
(when (not (listp data))
141+
(validation-error "~A: ~A is not of type ~A"
142+
(or (attribute-external-name attribute)
143+
(attribute-name attribute))
144+
attribute
145+
(attribute-type attribute)))
146+
(every (lambda (val)
147+
(schema-validate (second schema) val))
148+
data))
149+
140150
(defmethod %schema-validate ((schema-type (eql :option)) schema data &optional attribute)
151+
(declare (ignore attribute))
152+
(when (not (member data (cdr schema) :test 'equalp))
153+
(validation-error "~s : should be one of ~s" data (cdr schema)))
154+
t)
155+
156+
(defmethod %schema-validate ((schema-type (eql 'member)) schema data &optional attribute)
157+
(declare (ignore attribute))
141158
(when (not (member data (cdr schema) :test 'equalp))
142159
(validation-error "~s : should be one of ~s" data (cdr schema)))
143160
t)
@@ -149,27 +166,55 @@ Args:
149166
(attribute-name attribute))
150167
data)))
151168

169+
(defmethod %schema-validate ((schema-type (eql 'string)) schema data &optional attribute)
170+
(when (not (stringp data))
171+
(validation-error "~A: ~A is not a string"
172+
(or (attribute-external-name attribute)
173+
(attribute-name attribute))
174+
data)))
175+
152176
(defmethod %schema-validate ((schema-type (eql :boolean)) schema data &optional attribute)
153177
(when (not (typep data 'boolean))
154178
(validation-error "~A: ~A is not a boolean"
155179
(or (attribute-external-name attribute)
156180
(attribute-name attribute))
157181
data)))
158182

183+
(defmethod %schema-validate ((schema-type (eql 'boolean)) schema data &optional attribute)
184+
(when (not (typep data 'boolean))
185+
(validation-error "~A: ~A is not a boolean"
186+
(or (attribute-external-name attribute)
187+
(attribute-name attribute))
188+
data)))
189+
159190
(defmethod %schema-validate ((schema-type (eql :integer)) schema data &optional attribute)
160191
(when (not (integerp data))
161192
(validation-error "~A: ~A is not a number"
162193
(or (attribute-external-name attribute)
163194
(attribute-name attribute))
164195
data)))
165196

197+
(defmethod %schema-validate ((schema-type (eql 'integer)) schema data &optional attribute)
198+
(when (not (integerp data))
199+
(validation-error "~A: ~A is not a number"
200+
(or (attribute-external-name attribute)
201+
(attribute-name attribute))
202+
data)))
203+
166204
(defmethod %schema-validate ((schema-type (eql :float)) schema data &optional attribute)
167205
(when (not (floatp data))
168206
(validation-error "~A: ~A is not a float"
169207
(or (attribute-external-name attribute)
170208
(attribute-name attribute))
171209
data)))
172210

211+
(defmethod %schema-validate ((schema-type (eql 'float)) schema data &optional attribute)
212+
(when (not (floatp data))
213+
(validation-error "~A: ~A is not a float"
214+
(or (attribute-external-name attribute)
215+
(attribute-name attribute))
216+
data)))
217+
173218
(defmethod %schema-validate ((schema-type (eql :timestamp)) schema data &optional attribute)
174219
(when (not
175220
(or (typep data 'local-time:timestamp)
@@ -207,9 +252,28 @@ Args:
207252
(attribute-name attribute))
208253
data)))
209254

255+
(defmethod %schema-validate ((schema-type (eql 'local-time:timestamp)) schema data &optional attribute)
256+
(when (not
257+
(or (typep data 'local-time:timestamp)
258+
(and (stringp data)
259+
(or (ignore-errors (local-time:parse-timestring data
260+
:allow-missing-timezone-part t))
261+
(chronicity:parse data)))))
262+
(validation-error "~A: ~A is not a valid timestamp"
263+
(or (attribute-external-name attribute)
264+
(attribute-name attribute))
265+
data)))
266+
210267
(defmethod %schema-validate ((schema-type (eql :keyword)) schema data &optional attribute)
211268
(when (not (stringp data))
212269
(validation-error "~A: ~A is not a keyword"
213270
(or (attribute-external-name attribute)
214271
(attribute-name attribute))
215272
data)))
273+
274+
(defmethod %schema-validate ((schema-type (eql 'keyword)) schema data &optional attribute)
275+
(when (not (stringp data))
276+
(validation-error "~A: ~A is not a keyword"
277+
(or (attribute-external-name attribute)
278+
(attribute-name attribute))
279+
data)))

0 commit comments

Comments
 (0)