28
28
29
29
(defun validate-with-schema (schema data
30
30
&key
31
- (format :json )
32
31
(collect-errors *collect-validation-errors* )
33
32
(error-p *signal-validation-errors* ))
34
33
" Validate input using schema.
@@ -38,7 +37,7 @@ Input can be a string or an association list.
38
37
Args:
39
38
- schema (symbol or schema): The schema
40
39
- data (alist): The data to validate.
41
- - format (keyword): The data format
40
+ - format (keyword): The data format.
42
41
- collect-errors (boolean): If true, collect all the validation errors. If false, return the first validation error found. Default: true.
43
42
- 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."
44
43
(let ((*collect-validation-errors* collect-errors)
@@ -137,7 +136,25 @@ Args:
137
136
(schema-validate (second schema) val))
138
137
data))
139
138
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
+
140
150
(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))
141
158
(when (not (member data (cdr schema) :test ' equalp))
142
159
(validation-error " ~s : should be one of ~s " data (cdr schema)))
143
160
t )
@@ -149,27 +166,55 @@ Args:
149
166
(attribute-name attribute))
150
167
data)))
151
168
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
+
152
176
(defmethod %schema-validate ((schema-type (eql :boolean )) schema data &optional attribute)
153
177
(when (not (typep data ' boolean))
154
178
(validation-error " ~A : ~A is not a boolean"
155
179
(or (attribute-external-name attribute)
156
180
(attribute-name attribute))
157
181
data)))
158
182
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
+
159
190
(defmethod %schema-validate ((schema-type (eql :integer )) schema data &optional attribute)
160
191
(when (not (integerp data))
161
192
(validation-error " ~A : ~A is not a number"
162
193
(or (attribute-external-name attribute)
163
194
(attribute-name attribute))
164
195
data)))
165
196
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
+
166
204
(defmethod %schema-validate ((schema-type (eql :float )) schema data &optional attribute)
167
205
(when (not (floatp data))
168
206
(validation-error " ~A : ~A is not a float"
169
207
(or (attribute-external-name attribute)
170
208
(attribute-name attribute))
171
209
data)))
172
210
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
+
173
218
(defmethod %schema-validate ((schema-type (eql :timestamp )) schema data &optional attribute)
174
219
(when (not
175
220
(or (typep data ' local-time:timestamp)
@@ -207,9 +252,28 @@ Args:
207
252
(attribute-name attribute))
208
253
data)))
209
254
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
+
210
267
(defmethod %schema-validate ((schema-type (eql :keyword )) schema data &optional attribute)
211
268
(when (not (stringp data))
212
269
(validation-error " ~A : ~A is not a keyword"
213
270
(or (attribute-external-name attribute)
214
271
(attribute-name attribute))
215
272
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