-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRx.rb
658 lines (531 loc) · 17.9 KB
/
Rx.rb
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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
class Rx
def self.schema(schema)
Rx.new(:load_core => true).make_schema(schema)
end
def initialize(opt={})
@type_registry = {}
@prefix = {
'' => 'tag:codesimply.com,2008:rx/core/',
'.meta' => 'tag:codesimply.com,2008:rx/meta/',
}
if opt[:load_core] then
Type::Core.core_types.each { |t| register_type(t) }
end
end
def register_type(type)
uri = type.uri
if @type_registry.has_key?(uri) then
raise Rx::Exception.new(
"attempted to register already-known type #{uri}"
)
end
@type_registry[ uri ] = type
end
def learn_type(uri, schema)
if @type_registry.has_key?(uri) then
raise Rx::Exception.new(
"attempted to learn type for already-registered uri #{uri}"
)
end
# make sure schema is valid
# should this be in a begin/rescue?
make_schema(schema)
@type_registry[ uri ] = { 'schema' => schema }
end
def expand_uri(name)
if name.match(/\A\w+:/) then; return name; end;
match = name.match(/\A\/(.*?)\/(.+)\z/)
if ! match then
raise Rx::Exception.new("couldn't understand Rx type name: #{name}")
end
if ! @prefix.has_key?(match[1]) then
raise Rx::Exception.new("unknown prefix '#{match[1]}' in name 'name'")
end
return @prefix[ match[1] ] + match[2]
end
def add_prefix(name, base)
if @prefix.has_key?(name) then
throw Rx::Exception.new("the prefix '#{name}' is already registered")
end
@prefix[name] = base
end
def make_schema(schema)
schema = { 'type' => schema } if schema.instance_of?(String)
if not (schema.instance_of?(Hash) and schema['type']) then
raise Rx::Exception.new('invalid type')
end
uri = expand_uri(schema['type'])
if ! @type_registry.has_key?(uri) then
raise Rx::Exception.new('unknown type')
end
type_class = @type_registry[uri]
if type_class.instance_of?(Hash) then
if schema.keys != [ 'type' ] then
raise Rx::Exception.new('composed type does not take check arguments')
end
return make_schema(type_class['schema'])
else
return type_class.new(schema, self)
end
end
class Helper; end;
class Helper::Range
def initialize(arg)
@range = { }
arg.each_pair { |key,value|
if not ['min', 'max', 'min-ex', 'max-ex'].index(key) then
raise Rx::Exception.new("illegal argument for Rx::Helper::Range")
end
@range[ key ] = value
}
end
def check(value)
return false if ! @range['min' ].nil? and value < @range['min' ]
return false if ! @range['min-ex'].nil? and value <= @range['min-ex']
return false if ! @range['max-ex'].nil? and value >= @range['max-ex']
return false if ! @range['max' ].nil? and value > @range['max' ]
return true
end
end
class Exception < StandardError
end
class ValidationError < StandardError
attr_accessor :path
def initialize(message, path)
@message = message
@path = path
end
def path
@path ||= ""
end
def message
"#{@message} (#{@path})"
end
def inspect
"#{@message} (#{@path})"
end
def to_s
inspect
end
end
class Type
def initialize(param, rx)
assert_valid_params(param)
end
def uri; self.class.uri; end
def assert_valid_params(param)
param.each_key { |k|
unless self.allowed_param?(k) then
raise Rx::Exception.new("unknown parameter #{k} for #{uri}")
end
}
end
module NoParams
def initialize(param, rx)
return if param.keys.length == 0
return if param.keys == [ 'type' ]
raise Rx::Exception.new('this type is not parameterized')
end
end
class Type::Core < Type
class << self
def uri
return 'tag:codesimply.com,2008:rx/core/' + subname
end
end
def check(value)
begin
check!(value)
true
rescue ValidationError
false
end
end
class All < Type::Core
@@allowed_param = { 'of' => true, 'type' => true }
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
if ! param.has_key?('of') then
raise Rx::Exception.new("no 'of' parameter provided for #{uri}")
end
if param['of'].length == 0 then
raise Rx::Exception.new("no schemata provided for 'of' in #{uri}")
end
@alts = [ ]
param['of'].each { |alt| @alts.push(rx.make_schema(alt)) }
end
class << self; def subname; return 'all'; end; end
def check!(value)
@alts.each do |alt|
begin
alt.check!(value)
rescue ValidationError => e
e.path = "/all" + e.path
raise e
end
end
return true
end
end
class Any < Type::Core
@@allowed_param = { 'of' => true, 'type' => true }
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
if param['of'] then
if param['of'].length == 0 then
raise Rx::Exception.new(
"no alternatives provided for 'of' in #{uri}"
)
end
@alts = [ ]
param['of'].each { |alt| @alts.push(rx.make_schema(alt)) }
end
end
class << self; def subname; return 'any'; end; end
def check!(value)
return true unless @alts
@alts.each do |alt|
begin
return true if alt.check!(value)
rescue ValidationError
end
end
raise ValidationError.new("expected one to match", "/any")
end
end
class Arr < Type::Core
class << self; def subname; return 'arr'; end; end
@@allowed_param = { 'contents' => true, 'length' => true, 'type' => true }
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
unless param['contents'] then
raise Rx::Exception.new("no contents schema given for #{uri}")
end
@contents_schema = rx.make_schema( param['contents'] )
if param['length'] then
@length_range = Rx::Helper::Range.new( param['length'] )
end
end
def check!(value)
unless value.instance_of?(Array)
raise ValidationError.new("expected array got #{value.class}", "/arr")
end
if @length_range
unless @length_range.check(value.length)
raise ValidationError.new("expected array with #{@length_range} elements, got #{value.length}", "/arr")
end
end
if @contents_schema then
value.each do |v|
begin
@contents_schema.check!(v)
rescue ValidationError => e
e.path = "/arr" + e.path
raise e
end
end
end
return true
end
end
class Bool < Type::Core
class << self; def subname; return 'bool'; end; end
include Type::NoParams
def check!(value)
unless value.instance_of?(TrueClass) or value.instance_of?(FalseClass)
raise ValidationError.new("expected bool got #{value.inspect}", "/bool")
end
true
end
end
class Fail < Type::Core
class << self; def subname; return 'fail'; end; end
include Type::NoParams
def check(value); return false; end
def check!(value); raise ValidationError.new("explicit fail", "/fail"); end
end
#
# Added by dan - 81030
class Date < Type::Core
class << self; def subname; return 'date'; end; end
include Type::NoParams
def check!(value)
unless value.instance_of?(::Date)
raise ValidationError("expected Date got #{value.inspect}", "/date")
end
true
end
end
class Def < Type::Core
class << self; def subname; return 'def'; end; end
include Type::NoParams
def check!(value); raise ValidationError.new("def failed", "/def") unless ! value.nil?; end
end
class Map < Type::Core
class << self; def subname; return 'map'; end; end
@@allowed_param = { 'values' => true, 'type' => true }
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
unless param['values'] then
raise Rx::Exception.new("no values schema given for #{uri}")
end
@value_schema = rx.make_schema(param['values'])
end
def check!(value)
unless value.instance_of?(Hash) or value.class.to_s == "HashWithIndifferentAccess"
raise ValidationError.new("expected map got #{value.inspect}", "/map")
end
if @value_schema
value.each_value do |v|
begin
@value_schema.check!(v)
rescue ValidationError => e
e.path = "/map" + e.path
raise e
end
end
end
return true
end
end
class Nil < Type::Core
class << self; def subname; return 'nil'; end; end
include Type::NoParams
def check!(value); raise ValidationError.new("expected nil got #{value.inspect}", "/nil") unless value.nil?; true; end
end
class Num < Type::Core
class << self; def subname; return 'num'; end; end
@@allowed_param = { 'range' => true, 'type' => true, 'value' => true }
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
if param.has_key?('value') then
if ! param['value'].kind_of?(Numeric) then
raise Rx::Exception.new("invalid value parameter for #{uri}")
end
@value = param['value']
end
if param['range'] then
@value_range = Rx::Helper::Range.new( param['range'] )
end
end
def check!(value)
if not value.kind_of?(Numeric)
raise ValidationError.new("expected Numeric got #{value.inspect}", "/#{self.class.subname}")
end
if @value_range and not @value_range.check(value)
raise ValidationError.new("expected Numeric in range #{@value_range} got #{value.inspect}", "/#{self.class.subname}")
end
if @value and value != @value
raise ValidationError.new("expected Numeric to equal #{@value} got #{value.inspect}", "/#{self.class.subname}")
end
true
end
end
class Int < Type::Core::Num
class << self; def subname; return 'int'; end; end
def initialize(param, rx)
super
if @value and @value % 1 != 0 then
raise Rx::Exception.new("invalid value parameter for #{uri}")
end
end
def check!(value)
super
unless value % 1 == 0
raise ValidationError.new("expected Integer got #{value.inspect}", "/int")
end
return true
end
end
class One < Type::Core
class << self; def subname; return 'one'; end; end
include Type::NoParams
def check!(value)
unless [ Numeric, String, TrueClass, FalseClass ].any? { |cls| value.kind_of?(cls) }
raise ValidationError.new("expected One got #{value.inspect}", "/one")
end
end
end
class Rec < Type::Core
class << self; def subname; return 'rec'; end; end
@@allowed_param = {
'type' => true,
'rest' => true,
'required' => true,
'optional' => true,
}
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
@field = { }
@rest_schema = rx.make_schema(param['rest']) if param['rest']
[ 'optional', 'required' ].each { |type|
next unless param[type]
param[type].keys.each { |field|
if @field[field] then
raise Rx::Exception.new("#{field} in both required and optional")
end
@field[field] = {
:required => (type == 'required'),
:schema => rx.make_schema(param[type][field]),
}
}
}
end
def check!(value)
unless value.instance_of?(Hash) or value.class.to_s == "HashWithIndifferentAccess"
raise ValidationError.new("expected Hash got #{value.class}", "/rec")
end
rest = [ ]
value.each do |field, field_value|
unless @field[field] then
rest.push(field)
next
end
begin
@field[field][:schema].check!(field_value)
rescue ValidationError => e
e.path = "/rec:'#{field}'"
raise e
end
end
@field.select { |k,v| @field[k][:required] }.each do |pair|
unless value.has_key?(pair[0])
raise ValidationError.new("expected Hash to have key: '#{pair[0]}', only had #{value.keys.inspect}", "/rec")
end
end
if rest.length > 0 then
unless @rest_schema
raise ValidationError.new("Hash had extra keys: #{rest.inspect}", "/rec")
end
rest_hash = { }
rest.each { |field| rest_hash[field] = value[field] }
begin
@rest_schema.check!(rest_hash)
rescue ValidationError => e
e.path = "/rec"
raise e
end
end
return true
end
end
class Seq < Type::Core
class << self; def subname; return 'seq'; end; end
@@allowed_param = { 'tail' => true, 'contents' => true, 'type' => true }
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
unless param['contents'] and param['contents'].kind_of?(Array) then
raise Rx::Exception.new("missing or invalid contents for #{uri}")
end
@content_schemata = param['contents'].map { |s| rx.make_schema(s) }
if param['tail'] then
@tail_schema = rx.make_schema(param['tail'])
end
end
def check!(value)
unless value.instance_of?(Array)
raise ValidationError.new("expected Array got #{value.inspect}", "/seq")
end
if value.length < @content_schemata.length
raise ValidationError.new("expected Array to have at least #{@content_schemata.length} elements, had #{value.length}", "/seq")
end
@content_schemata.each_index { |i|
begin
@content_schemata[i].check!(value[i])
rescue ValidationError => e
e.path = "/seq" + e.path
raise e
end
}
if value.length > @content_schemata.length then
unless @tail_schema
raise ValidationError.new("expected tail_schema", "/seq")
end
begin
@tail_schema.check!(value[
@content_schemata.length,
value.length - @content_schemata.length
])
rescue ValidationError => e
e.path = "/seq" + e.path
raise e
end
end
return true
end
end
class Str < Type::Core
class << self; def subname; return 'str'; end; end
@@allowed_param = { 'type' => true, 'value' => true, 'length' => true }
def allowed_param?(p); return @@allowed_param[p]; end
def initialize(param, rx)
super
if param['length'] then
@length_range = Rx::Helper::Range.new( param['length'] )
end
if param.has_key?('value') then
if ! param['value'].instance_of?(String) then
raise Rx::Exception.new("invalid value parameter for #{uri}")
end
@value = param['value']
end
end
def check!(value)
unless value.instance_of?(String)
raise ValidationError.new("expected String got #{value.inspect}", "/str")
end
if @length_range
unless @length_range.check(value.length)
raise ValidationError.new("expected string with #{@length_range} characters, got #{value.length}", "/str")
end
end
if @value and value != @value
raise ValidationError.new("expected #{@value.inspect} got #{value.inspect}", "/str")
end
return true
end
end
#
# Added by dan - 81106
class Time < Type::Core
class << self; def subname; return 'time'; end; end
include Type::NoParams
def check!(value)
unless value.instance_of?(::Time)
raise ValidationError.new("expected Time got #{value.inspect}", "/time")
end
true
end
end
class << self
def core_types
return [
Type::Core::All,
Type::Core::Any,
Type::Core::Arr,
Type::Core::Bool,
Type::Core::Date,
Type::Core::Def,
Type::Core::Fail,
Type::Core::Int,
Type::Core::Map,
Type::Core::Nil,
Type::Core::Num,
Type::Core::One,
Type::Core::Rec,
Type::Core::Seq,
Type::Core::Str,
Type::Core::Time
]
end
end
end
end
end