forked from mmontone/schemata
-
Notifications
You must be signed in to change notification settings - Fork 0
/
validation.lisp
279 lines (247 loc) · 12.8 KB
/
validation.lisp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
(in-package :schemata)
(defvar *collect-validation-errors* nil)
(defvar *signal-validation-errors* t)
(defvar *validation-errors-collection*)
(define-condition validation-error (simple-error)
())
(define-condition validation-error-collection (validation-error)
((validation-errors :initarg :validation-errors
:initform (error "Provide the validation errors")
:accessor validation-errors))
(:report (lambda (c s)
(format s "Validation errors: ~{~A~^, ~}"
(validation-errors c)))))
(defun validation-error (message &rest args)
(cerror "Continue"
'validation-error
:format-control message
:format-arguments args))
(defun simple-condition-message (condition)
(apply #'format
(simple-condition-format-control condition)
(simple-condition-format-arguments condition)))
(defun validate-with-schema (schema data
&key
(collect-errors *collect-validation-errors*)
(error-p *signal-validation-errors*))
"Validate input using schema.
Useful for validating resource operations posted content (for :post and :put methods).
Input can be a string or an association list.
Args:
- schema (symbol or schema): The schema
- data (alist): The data to validate.
- format (keyword): The data format.
- collect-errors (boolean): If true, collect all the validation errors. If false, return the first validation error found. Default: true.
- 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."
(let ((*collect-validation-errors* collect-errors)
(*signal-validation-errors* error-p)
(*validation-errors-collection* nil))
(let ((validation-error
(handler-bind ((validation-error
(lambda (validation-error)
(cond
(collect-errors
(push validation-error *validation-errors-collection*)
(invoke-restart (find-restart 'continue)))
((not error-p)
(return-from validate-with-schema validation-error))
(t
(error validation-error))))))
(schema-validate schema data))))
(if collect-errors
*validation-errors-collection*
validation-error))))
(defgeneric schema-validate (schema data &optional attribute)
)
(defmethod schema-validate (schema data &optional attribute)
;; If present, the attribute-validator replaces completely the default schema validation. To avoid replacing it, but adding more validation use :add-validator
(flet ((schema-validator ()
;; The schema to use is either a defined a schema, or a schema discriminator (typespec, etc)
(let ((schema (or (and (symbolp schema) (find-schema schema nil)) schema)))
(%schema-validate (schema-type schema) schema data attribute))))
(if (and attribute (attribute-validator attribute))
;; The validator function receives the data to validate and an "schema validator" function
;; it can use to validate the schema
(funcall (attribute-validator attribute) data #'schema-validator)
;; else
(schema-validator))))
(defmethod schema-validate :after (schema data &optional attribute)
;; After normal validation, :add-validator is evaluated if found
(when (and attribute (attribute-add-validator attribute))
(multiple-value-bind (valid-p error-message) (funcall (attribute-add-validator attribute) data)
(when (not valid-p)
(validation-error (or error-message
(format nil "~A: is invalid"
(or (attribute-external-name attribute)
(attribute-name attribute)))))))))
(defmethod %schema-validate ((schema-type (eql :object)) schema data &optional attribute)
(declare (ignore attribute))
"Validate data using schema object. "
;; Check unknown attributes first
(unless (or *ignore-unknown-object-attributes*
(object-option :ignore-unknown-attributes schema))
(alexandria:when-let ((unknown-attributes
(set-difference (mapcar 'car data)
(mapcar 'attribute-name (object-attributes schema))
:test 'equalp
:key 'string)))
(validation-error "Attributes not part of schema: ~a" unknown-attributes)))
;; Validate each attribute of object
(loop
:for schema-attribute :in (object-attributes schema)
:for data-attribute := (assoc (string (attribute-name schema-attribute))
data
:test #'equalp
:key #'string)
:do
(cond
((and (not data-attribute)
(not (attribute-optional-p schema-attribute)))
(let ((error-msg (or (attribute-option :attribute-required-message schema-attribute)
(format nil "Attribute required: ~a"
(or (attribute-external-name schema-attribute)
(attribute-name schema-attribute))))))
(validation-error error-msg)))
((not data-attribute)
;; Nothing to validate
)
((not (and (attribute-optional-p schema-attribute)
(null data-attribute)))
(schema-validate (attribute-type schema-attribute)
(cdr data-attribute)
schema-attribute)))))
(defmethod %schema-validate ((schema-type (eql :list)) schema data &optional attribute)
(when (not (listp data))
(validation-error "~A: ~A is not of type ~A"
(or (attribute-external-name attribute)
(attribute-name attribute))
attribute
(attribute-type attribute)))
(every (lambda (val)
(schema-validate (second schema) val))
data))
(defmethod %schema-validate ((schema-type (eql 'list)) schema data &optional attribute)
(when (not (listp data))
(validation-error "~A: ~A is not of type ~A"
(or (attribute-external-name attribute)
(attribute-name attribute))
attribute
(attribute-type attribute)))
(every (lambda (val)
(schema-validate (second schema) val))
data))
(defmethod %schema-validate ((schema-type (eql :option)) schema data &optional attribute)
(declare (ignore attribute))
(when (not (member data (cdr schema) :test 'equalp))
(validation-error "~s : should be one of ~s" data (cdr schema)))
t)
(defmethod %schema-validate ((schema-type (eql 'member)) schema data &optional attribute)
(declare (ignore attribute))
(when (not (member data (cdr schema) :test 'equalp))
(validation-error "~s : should be one of ~s" data (cdr schema)))
t)
(defmethod %schema-validate ((schema-type (eql :string)) schema data &optional attribute)
(when (not (stringp data))
(validation-error "~A: ~A is not a string"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql 'string)) schema data &optional attribute)
(when (not (stringp data))
(validation-error "~A: ~A is not a string"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql :boolean)) schema data &optional attribute)
(when (not (typep data 'boolean))
(validation-error "~A: ~A is not a boolean"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql 'boolean)) schema data &optional attribute)
(when (not (typep data 'boolean))
(validation-error "~A: ~A is not a boolean"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql :integer)) schema data &optional attribute)
(when (not (integerp data))
(validation-error "~A: ~A is not a number"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql 'integer)) schema data &optional attribute)
(when (not (integerp data))
(validation-error "~A: ~A is not a number"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql :float)) schema data &optional attribute)
(when (not (floatp data))
(validation-error "~A: ~A is not a float"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql 'float)) schema data &optional attribute)
(when (not (floatp data))
(validation-error "~A: ~A is not a float"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql :timestamp)) schema data &optional attribute)
(when (not
(or (typep data 'local-time:timestamp)
(and (stringp data)
(or (ignore-errors (local-time:parse-timestring data
:allow-missing-timezone-part t))
(chronicity:parse data)))))
(validation-error "~A: ~A is not a valid timestamp"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql :datetime)) schema data &optional attribute)
(when (not
(or (typep data 'local-time:timestamp)
(and (stringp data)
(or (ignore-errors (local-time:parse-timestring data
:allow-missing-timezone-part t))
(chronicity:parse data)))))
(validation-error "~A: ~A is not a valid timestamp"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql :date)) schema data &optional attribute)
(when (not
(or (typep data 'local-time:timestamp)
(and (stringp data)
(or (ignore-errors (local-time:parse-timestring data
:allow-missing-time-part t
:allow-missing-timezone-part t))
(chronicity:parse data)))))
(validation-error "~A: ~A is not a valid date"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql 'local-time:timestamp)) schema data &optional attribute)
(when (not
(or (typep data 'local-time:timestamp)
(and (stringp data)
(or (ignore-errors (local-time:parse-timestring data
:allow-missing-timezone-part t))
(chronicity:parse data)))))
(validation-error "~A: ~A is not a valid timestamp"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql :keyword)) schema data &optional attribute)
(when (not (stringp data))
(validation-error "~A: ~A is not a keyword"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))
(defmethod %schema-validate ((schema-type (eql 'keyword)) schema data &optional attribute)
(when (not (stringp data))
(validation-error "~A: ~A is not a keyword"
(or (attribute-external-name attribute)
(attribute-name attribute))
data)))