-
Notifications
You must be signed in to change notification settings - Fork 0
/
validol.py
375 lines (324 loc) · 11.8 KB
/
validol.py
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
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# Version 2, December 2004
#
# Copyright (C) 2008 Konstantin Merenkov <kmerenkov@gmail.com>
# Everyone is permitted to copy and distribute verbatim or modified
# copies of this license document, and changing it is allowed as long
# as the name is changed.
#
# DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
# TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
#
# 0. You just DO WHAT THE FUCK YOU WANT TO.
__version__ = "0.2" # XXX Not always updated :\
__author__ = "Konstantin Merenkov <kmerenkov@gmail.com>"
from itertools import imap
TYPE_UNKNOWN = 0
TYPE_VALIDATOR = 1
TYPE_LIST = 2
TYPE_REGEX = 3
TYPE_TYPE = 4
TYPE_DICTIONARY = 5
TYPE_OBJECT = 6
TYPE_TUPLE = 7
TYPE_FUNCTION = 8
ATOMIC_TYPES = set([str, unicode, int, bool, float])
class BaseValidator(object):
"""
All other validators inherit this baseclass. You want to use this class
only if you write your own validator, otherwise you should not care
about it.
Call to validate method results in NotImplementedError exception.
>>> BaseValidator().validate("foo")
Traceback (most recent call last):
...
NotImplementedError: Inherit this class and override this method.
"""
def validate(self, data):
"""
For all validators this function validates data and returns True if data
is found to be valid, False otherwise.
For this base class this function throws NotImplementedError.
"""
raise NotImplementedError("Inherit this class and override this method.")
def __repr__(self):
return str(self)
def kind_of(obj):
"""
Finds out what kind of object we have on hands.
For example, dicts, lists, and tuples have complex validation,
while str, int, float, and bool have simple validation, that looks like (!):
if (type(data) is some_type_mentioned_above):
return True
Be careful with objects, because in validol object means "I don't care"
and matches anything at all.
>>> kind_of({'a': 'b'}) == TYPE_DICTIONARY
True
>>> kind_of([1,2,3]) == TYPE_LIST
True
>>> kind_of((1,2)) == TYPE_TUPLE
True
>>> kind_of(int) == TYPE_TYPE
True
>>> kind_of(object) == TYPE_OBJECT
True
>>> import re
>>> kind_of(re.compile('foo')) == TYPE_REGEX
True
>>> kind_of(BaseValidator()) == TYPE_VALIDATOR
True
>>> kind_of(42) == TYPE_UNKNOWN
True
>>> kind_of(lambda x: "123") == TYPE_FUNCTION
True
"""
# why don't I use isinstance - it saves us big time
# dict, list, and tuple are differianted from str, unicode, int, bool, and float
# because they have special treatment and simple `==` or `is` is not enough to
# prove them valid.
obj_type = type(obj)
if obj_type is dict:
return TYPE_DICTIONARY
elif obj_type is list:
return TYPE_LIST
elif obj_type is tuple:
return TYPE_TUPLE
elif obj in ATOMIC_TYPES:
return TYPE_TYPE
elif obj is object:
return TYPE_OBJECT
elif getattr(obj, "__class__", False) and issubclass(obj.__class__, BaseValidator):
return TYPE_VALIDATOR
elif callable(obj):
return TYPE_FUNCTION
# this f##king SRE_Pattern, why can't I f##king kill it
elif getattr(obj, "match", False) and getattr(obj, "search", False):
return TYPE_REGEX
else:
return TYPE_UNKNOWN
def validate(scheme, data):
"""
Validates data against scheme. Returns True if data
found to be valid, False otherwise.
>>> validate(1, 1) # validate simple data
True
>>> validate('foo', 'bar') # two different strings are not equal
False
>>> validate({'a': int, 'b': int}, {'a': 10, 'b': 20}) # more difficult example
True
>>> validate(lambda x: x > 10, 5)
False
>>> validate(lambda x: x > 10, 20)
True
"""
return validate_common(scheme, data)
def validate_common(validator, data):
kind = kind_of(validator)
if kind == TYPE_VALIDATOR:
if validator.validate(data):
return True
elif kind == TYPE_FUNCTION:
try:
if validator(data):
return True
except:
return False
elif kind == TYPE_REGEX:
if validator.match(data):
return True
elif kind == TYPE_DICTIONARY:
return validate_hash(validator, data)
elif kind == TYPE_LIST:
return validate_list(validator, data)
elif kind == TYPE_TUPLE:
return validate_tuple(validator, data)
elif kind == TYPE_UNKNOWN:
return data == validator
elif kind == TYPE_OBJECT:
# NOTE In validol 'object' means anything,
# so it is always valid. It is like specifying .* in re
return True
elif kind == TYPE_TYPE:
return type(data) == validator
return False
def validate_tuple(validator, data):
"""
>>> validate_tuple((int, int), (1, 2))
True
>>> validate_tuple((int, str), (1, 2))
False
>>> validate_tuple((int,), 'foo')
False
"""
if type(data) is not tuple:
return False
if len(validator) != len(data):
return False
# all elements must be valid
return all(imap(validate_common, validator, data))
def validate_list(validators, data):
"""
>>> validate_list([int], range(10))
True
>>> validate_list([int], 'foo')
False
>>> validate_list([str], 'foo')
False
>>> validate_list([str], ['foo'])
True
>>> validate_list([str, str], ['foo'])
Traceback (most recent call last):
...
NotImplementedError: You cannot specify more than one validator for list at the moment.
"""
if type(data) is not list:
return False
n_validators = len(validators)
if n_validators == 0:
return len(data) == 0
elif n_validators == 1:
validator = validators[0]
return all(imap(lambda item: validate_common(validator, item), data))
elif n_validators > 1:
raise NotImplementedError("You cannot specify more than one validator for list at the moment.")
def validate_hash(validator, data):
if type(data) is not dict:
return False
if validator == data == {}:
return True
if validator == {} and data != {}:
return False
optional_validators = {}
many_validators = {}
for v_key, v_val in validator.iteritems():
if type(v_key) is Optional:
optional_validators[v_key] = v_val
else:
many_validators[v_key] = v_val
if optional_validators:
ret_with_optional, passed_optional_data_keys = validate_hash_with_optional(optional_validators,
data)
if not ret_with_optional: # optional validation has failed
return False
else:
ret_with_optional = True # we don't have optional keys, that's okay
new_data = {}
if optional_validators and passed_optional_data_keys != set():
new_data = dict(filter(lambda item: item[0] not in passed_optional_data_keys,
data.iteritems()))
else:
new_data = data
ret_with_many = validate_hash_with_many(many_validators, new_data)
return ret_with_many and ret_with_optional
def validate_hash_with_optional(validator, data):
validator = dict(validator) # copy validator because later we modify it (pop keys out)
valid_data_keys = set()
validator_count = len(validator)
used_validators_count = 0
for data_key, data_value in data.iteritems():
for validator_key, validator_value in validator.iteritems():
if validate_common(validator_key, data_key):
if validate_common(validator_value, data_value):
valid_data_keys.add(data_key)
validator.pop(validator_key) # we don't need this validator in future
used_validators_count += 1
# exhausted all optional validators, good sign
if used_validators_count == validator_count:
return (True, valid_data_keys)
break
else:
return (False, {})
return (True, valid_data_keys)
def validate_hash_with_many(validator, data):
if validator != {} and data == {}:
return False
orig_validator = validator
copy_validator = dict(validator)
for data_key, data_value in data.iteritems():
data_valid = False
for validator_key, validator_value in copy_validator.iteritems():
if validate_common(validator_key, data_key) and \
validate_common(validator_value, data_value):
if type(validator_key) is not Many:
copy_validator.pop(validator_key)
data_valid = True
break
if not data_valid:
return False
# count Many validators
declared_many_validator_count = len(filter(lambda v: type(v) is Many,
orig_validator.keys()))
# count "unused" validators (Many validators aren't marked as used)
unused_notmany_validator_count = len(filter(lambda v: v in copy_validator,
orig_validator.keys()))
# their quantity must be equal for data to be proven valid
return unused_notmany_validator_count == declared_many_validator_count
class AnyOf(BaseValidator):
"""
Validates if data matches at least one of specified schemes.
>>> AnyOf(1, 2, 3).validate(1) # 1 or 2 or 3
True
>>> AnyOf(1, 2, 3).validate(2)
True
>>> AnyOf(1, 2, 3).validate(10)
False
"""
def __init__(self, *validators):
self.validators = validators
def validate(self, data):
""" returns True if data is valid for at least one validator. """
return any(imap(lambda validator: validate_common(validator, data), self.validators))
def __str__(self):
return "<AnyOf: '%s'>" % str(self.validators)
class Many(BaseValidator):
"""
BIG FAT WARNING: Useful only for dict validation. In fact all it does is simple
1-to-1 comparison, i.e. same as validate(X, X) where X is some exact value.
Validates if one or more occurences of data match specified scheme.
>>> Many('foo').validate('foo')
True
"""
def __init__(self, data):
self.data = data
def validate(self, data):
return validate_common(self.data, data)
def __str__(self):
return "<Many: '%s'>" % str(self.data)
class Optional(BaseValidator):
"""
When used as a key for hash, validates data if data matches scheme or if key is absent from hash.
When used anywhere else, validates data if data is None or if data is valid.
>>> Optional('foo').validate(None)
True
>>> Optional('foo').validate('foo')
True
>>> Optional('foo').validate('bar')
False
"""
def __init__(self, data):
self.data = data
def validate(self, data):
return data is None or validate_common(self.data, data)
def __str__(self):
return "<Optional: '%s'>" % str(self.data)
class Text(BaseValidator):
"""
Passes on any textual data (be it str or unicode).
"""
def __init__(self):
pass
def validate(self, data):
# I could do isinstance(data, basestring) but I remember it to be slow.
# Not sure if the code below is any faster :)
return AnyOf(str, unicode).validate(data)
class Scheme(AnyOf):
"""
This class exist to make raw structure have type (type of Scheme).
Often it is useful, often it is not - depends on your needs.
Behaves exactly as AnyOf, except has different str and repr methods.
"""
def __str__(self):
return "<Scheme: '%s'>" % str(self.validators)
if __name__ == '__main__':
import doctest
doctest.testmod()