Skip to content
Browse files

Merge branch 'master' of git://github.com/rjbs/rx

Conflicts:
	js/rx/coretypes.js
	js/rx/test/loader.js
	js/rx/test/runner.js
	php/Rx.php
	php/rx-test.php
	python/Rx.py
	python/rx-test.py
	ruby/Rx.rb
	ruby/rx-test.rb
  • Loading branch information...
2 parents 24b9e06 + a23ae0f commit 05c03cb09cd69e17e95b882297ba886b29ae6ddc Ronald J Kimball committed Apr 5, 2012
View
1 .gitignore
@@ -5,4 +5,5 @@ php/Test.php
python/*.pyc
perl/spec
perl/.build
+perl/Data-Rx-*
*~
View
6 js/rx/coretypes.js
@@ -123,13 +123,12 @@ Rx.CoreType.numType.prototype.check = function (v) {
};
Rx.CoreType.strType = function (opt) {
- if (! Rx.Util._x_subset_keys_y(opt, {type: true, length: true, value: true }))
+ if (! Rx.Util._x_subset_keys_y(opt, {type: true, value: true, length: true }))
throw new Rx.Error('unknown argument for str type');
if (typeof(opt.value) != "undefined")
if (opt.value.constructor != String)
throw new Rx.Error('invalid value parameter for str type');
this.value = opt.value;
-
if (opt.length) {
this.length_check = new Rx.Util.RangeChecker( opt.length );
}
@@ -139,6 +138,9 @@ Rx.CoreType.strType.prototype.check = function (v) {
if (! ((typeof(v) == 'string') || (v instanceof String))) return false;
if (this.length_check && ! this.length_check.check(v.length)) return false;
if (this.value != null && v != this.value) return false;
+ if (this.length_check && ! this.length_check.check(v.length)) {
+ return false;
+ }
return true;
};
View
2 js/rx/test/runner.js
@@ -85,7 +85,7 @@ for (i in schemaToTest) {
var testDesc = (expect ? 'VALID : ' : 'INVALID: ')
+ sourceName + '/' + sourceEntry
+ ' against ' + schemaName;
-
+
// JavaScript needs logical xor! -- rjbs, 2008-07-31
if ((valid && !expect) || (!valid && expect)) {
fail(testDesc);
View
2 perl/Changes
@@ -2,6 +2,8 @@ Revision history for Data-Rx
{{$NEXT}}
+0.200000 2012-04-01 14:33:26 Europe/Paris
+
0.100110 2010-01-11 22:42:19 America/New_York
do not require JSON::XS
View
10 perl/dist.ini
@@ -3,8 +3,6 @@ author = Ricardo SIGNES <rjbs@cpan.org>
license = Perl_5
copyright_holder = Ricardo SIGNES
-version = 2 ; placeholder
-
[Prereqs]
Carp = 0
File::Find::Rule = 0
@@ -19,9 +17,15 @@ autodie = 0
[@Filter]
bundle = @RJBS
-remove = Repository
+remove = Git::Tag
remove = Git::NextVersion
+[Git::Tag]
+tag_format = perl-%v
+
+[Git::NextVersion]
+version_regexp = ^perl-(.+)$
+
[GatherDir]
root = ../spec
prefix = spec
View
2 perl/lib/Data/Rx.pm
@@ -130,7 +130,7 @@ sub make_schema {
}
$checker = $self->make_schema($handler->{'schema'});
} else {
- $checker = $handler->new_checker($type, $schema_arg, $self);
+ $checker = $handler->new_checker($schema_arg, $self, $type);
}
return $checker;
View
6 perl/lib/Data/Rx/CoreType.pm
@@ -9,7 +9,7 @@ use Data::Rx::Failure;
use Data::Rx::Failures;
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak "$class does not take check arguments" if %$arg;
bless { type => $type, rx => $rx } => $class;
}
@@ -22,7 +22,7 @@ sub check {
my ($self, $value) = @_;
local $@;
- return 1 if eval { $self->validate($value); };
+ return 1 if eval { $self->assert_valid($value); };
my $failures = $@;
if (eval { $failures->isa('Data::Rx::Failures') }) {
@@ -76,7 +76,7 @@ sub _subchecks {
my ($value, $checker, $context) = @$subcheck;
- next if eval { $checker->validate($value) };
+ next if eval { $checker->assert_valid($value) };
my $failures = $@;
Carp::confess($failures)
View
8 perl/lib/Data/Rx/CoreType/all.pm
@@ -7,26 +7,26 @@ use base 'Data::Rx::CoreType';
use Scalar::Util ();
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new")
unless Data::Rx::Util->_x_subset_keys_y($arg, { of => 1});
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
Carp::croak("no 'of' parameter given to //all") unless exists $arg->{of};
my $of = $arg->{of};
Carp::croak("invalid 'of' argument to //all") unless
defined $of and Scalar::Util::reftype $of eq 'ARRAY' and @$of;
-
+
$self->{of} = [ map {; $rx->make_schema($_) } @$of ];
return $self;
}
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
my @subchecks;
View
8 perl/lib/Data/Rx/CoreType/any.pm
@@ -7,12 +7,12 @@ use base 'Data::Rx::CoreType';
use Scalar::Util ();
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new")
unless Data::Rx::Util->_x_subset_keys_y($arg, { of => 1});
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
if (my $of = $arg->{of}) {
Carp::croak("invalid 'of' argument to //any") unless
@@ -24,15 +24,15 @@ sub new_checker {
return $self;
}
-sub validate {
+sub assert_valid {
return 1 unless $_[0]->{of};
my ($self, $value) = @_;
my @failures;
for my $i (0 .. $#{ $self->{of} }) {
my $check = $self->{of}[ $i ];
- return 1 if eval { $check->validate($value) };
+ return 1 if eval { $check->assert_valid($value) };
my $failure = $@;
$failure->contextualize({
View
6 perl/lib/Data/Rx/CoreType/arr.pm
@@ -9,7 +9,7 @@ use Scalar::Util ();
sub subname { 'arr' }
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new")
unless Data::Rx::Util->_x_subset_keys_y($arg, {length=>1, contents=>1,
@@ -18,7 +18,7 @@ sub new_checker {
Carp::croak("no contents schema given")
unless $arg->{contents} and (ref $arg->{contents} || 'HASH' eq 'HASH');
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
my $content_check = $rx->make_schema($arg->{contents});
@@ -32,7 +32,7 @@ sub new_checker {
return $self;
}
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
unless (! Scalar::Util::blessed($value) and ref $value eq 'ARRAY') {
View
2 perl/lib/Data/Rx/CoreType/bool.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::bool;
use base 'Data::Rx::CoreType';
# ABSTRACT: the Rx //bool type
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
return 1 if (
View
2 perl/lib/Data/Rx/CoreType/def.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::def;
use base 'Data::Rx::CoreType';
# ABSTRACT: the Rx //def type
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
return 1 if defined $value;
View
2 perl/lib/Data/Rx/CoreType/fail.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::fail;
use base 'Data::Rx::CoreType';
# ABSTRACT: the Rx //fail type
-sub validate {
+sub assert_valid {
$_[0]->fail({
error => [ qw(fail) ],
message => "matching reached an always-fail check",
View
6 perl/lib/Data/Rx/CoreType/map.pm
@@ -9,12 +9,12 @@ use Scalar::Util ();
sub subname { 'map' }
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new") unless
Data::Rx::Util->_x_subset_keys_y($arg, { values => 1 });
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
Carp::croak("no values constraint given") unless $arg->{values};
@@ -23,7 +23,7 @@ sub new_checker {
return $self;
}
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
unless (! Scalar::Util::blessed($value) and ref $value eq 'HASH') {
View
2 perl/lib/Data/Rx/CoreType/nil.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::nil;
use base 'Data::Rx::CoreType';
# ABSTRACT: the Rx //nil type
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
return 1 if ! defined $value;
View
6 perl/lib/Data/Rx/CoreType/num.pm
@@ -5,12 +5,12 @@ use base 'Data::Rx::CoreType';
# ABSTRACT: the Rx //num type
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new")
unless Data::Rx::Util->_x_subset_keys_y($arg, { range => 1, value => 1});
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
$self->{range_check} = Data::Rx::Util->_make_range_check($arg->{range})
if $arg->{range};
@@ -64,7 +64,7 @@ sub _value_is_of_type {
return $value =~ $_NUM_RE;
}
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
$self->__type_fail($value) unless defined $value and length $value;
View
2 perl/lib/Data/Rx/CoreType/one.pm
@@ -4,7 +4,7 @@ package Data::Rx::CoreType::one;
use base 'Data::Rx::CoreType';
# ABSTRACT: the Rx //one type
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
if (! defined $value) {
View
6 perl/lib/Data/Rx/CoreType/rec.pm
@@ -9,7 +9,7 @@ use Scalar::Util ();
sub subname { 'rec' }
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new") unless
Data::Rx::Util->_x_subset_keys_y($arg, {
@@ -18,7 +18,7 @@ sub new_checker {
optional => 1,
});
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
my $content_schema = {};
@@ -42,7 +42,7 @@ sub new_checker {
return $self;
}
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
unless (! Scalar::Util::blessed($value) and ref $value eq 'HASH') {
View
6 perl/lib/Data/Rx/CoreType/seq.pm
@@ -9,15 +9,15 @@ use Scalar::Util ();
sub subname { 'seq' }
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new")
unless Data::Rx::Util->_x_subset_keys_y($arg, {contents=>1,tail=>1});
Carp::croak("no contents array given")
unless $arg->{contents} and (ref $arg->{contents} eq 'ARRAY');
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
my @content_schemata = map { $rx->make_schema($_) }
@{ $arg->{contents} };
@@ -31,7 +31,7 @@ sub new_checker {
return $self;
}
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
unless (! Scalar::Util::blessed($value) and ref $value eq 'ARRAY') {
View
6 perl/lib/Data/Rx/CoreType/str.pm
@@ -7,7 +7,7 @@ use base 'Data::Rx::CoreType';
use Data::Rx::Util;
sub new_checker {
- my ($class, $type, $arg, $rx) = @_;
+ my ($class, $arg, $rx, $type) = @_;
Carp::croak("unknown arguments to new")
unless Data::Rx::Util->_x_subset_keys_y($arg, { length => 1, value => 1});
@@ -26,7 +26,7 @@ sub new_checker {
}
}
- my $self = $class->SUPER::new_checker($type, {}, $rx);
+ my $self = $class->SUPER::new_checker({}, $rx, $type);
$self->{length_check} = Data::Rx::Util->_make_range_check($arg->{length})
if $arg->{length};
@@ -36,7 +36,7 @@ sub new_checker {
return $self;
}
-sub validate {
+sub assert_valid {
my ($self, $value) = @_;
unless (defined $value) {
View
4 perl/t/lib/Test/RxTester.pm
@@ -83,7 +83,7 @@ sub assert_pass {
my $desc = "$schema_desc should ACCEPT $input_desc";
try {
- $schema->validate($input);
+ $schema->assert_valid($input);
Test::More::pass("$desc");
} catch {
my $fails = $_;
@@ -102,7 +102,7 @@ sub assert_fail {
my $desc = "$schema_desc should REJECT $input_desc";
try {
- $schema->validate($input);
+ $schema->assert_valid($input);
Test::More::fail($desc);
if ($want_struct) {
Test::More::fail("$desc, failures struct");
View
6 php/Rx.php
@@ -368,10 +368,12 @@ class RxCoretypeStr {
function check($value) {
if (! is_string($value)) return false;
- if ($this->length_checker)
- if (! $this->length_checker->check(strlen($value))) return false;
if ($this->fixed_value !== null and $value != $this->fixed_value)
return false;
+
+ if ($this->length_checker)
+ if (! $this->length_checker->check(strlen($value))) return false;
+
return true;
}
View
20 python/Rx.py
@@ -13,7 +13,7 @@ def make_range_check(opt):
range = { }
for entry in opt.keys():
if entry not in ('min', 'max', 'min-ex', 'max-ex'):
- raise Error("illegal argument to make_range_check")
+ raise ValueError("illegal argument to make_range_check")
range[entry] = opt[entry]
@@ -45,10 +45,13 @@ def expand_uri(self, type_name):
m = re.match('^/([-._a-z0-9]*)/([-._a-z0-9]+)$', type_name)
- if not m: raise Error("couldn't understand type name '%s'" % type_name)
+ if not m:
+ raise ValueError("couldn't understand type name '%s'" % type_name)
if not self.prefix_registry.get(m.group(1)):
- raise Error("unknown prefix '%s' in type name '%s'" % (m.group(1), type_name))
+ raise ValueError(
+ "unknown prefix '%s' in type name '%s'" % (m.group(1), type_name)
+ )
return '%s%s' % (self.prefix_registry[ m.group(1) ], m.group(2))
@@ -62,7 +65,7 @@ def register_type(self, t):
t_uri = t.uri()
if self.type_registry.get(t_uri, None):
- raise Error("type already registered for %s" % t_uri)
+ raise ValueError("type already registered for %s" % t_uri)
self.type_registry[t_uri] = t
@@ -82,7 +85,7 @@ def make_schema(self, schema):
if not type(schema) is dict:
raise Error('invalid schema argument to make_schema')
-
+
uri = self.expand_uri(schema["type"])
if not self.type_registry.get(uri): raise Error("unknown type %s" % uri)
@@ -141,7 +144,7 @@ def __init__(self, schema, rx):
def check(self, value):
if self.alts is None: return True
-
+
for alt in self.alts:
if alt.check(value): return True
@@ -156,7 +159,7 @@ def __init__(self, schema, rx):
if not set(schema.keys()).issubset(set(('type', 'contents', 'length'))):
raise Error('unknown parameter for //arr')
-
+
if not schema.get('contents'):
raise Error('no contents provided for //arr')
@@ -317,7 +320,7 @@ def check(self, value):
if len(unknown) and not self.rest_schema: return False
- for field in self.required.keys():
+ for field in self.required.keys():
if not value.has_key(field): return False
if not self.required[field].check( value[field] ): return False
@@ -389,6 +392,7 @@ def check(self, value):
if not type(value) in (str, unicode): return False
if self.length and not self.length(len(value)): return False
if (not self.value is None) and value != self.value: return False
+ if self.length and not self.length(len(value)): return False
return True
core_types = [
View
7 python/rx-test.py
@@ -39,7 +39,7 @@ def normalize(entries, test_data):
leaf_name = '/'.join(parts[1:])
leaf_name = re.sub('\.json$', '', leaf_name)
-
+
filetype = parts.pop(0)
if filetype == 'schemata':
@@ -57,7 +57,7 @@ def normalize(entries, test_data):
boxed_data = json.loads("[ %s ]" % payload[entry])
test_data[ leaf_name ][ entry ] = boxed_data[0]
else:
- raise Exception("weird file in data dir: %s" % filename)
+ raise StandardError("weird file in data dir: %s" % filename)
schema_names = test_schemata.keys()
schema_names.sort()
@@ -80,7 +80,6 @@ def normalize(entries, test_data):
if schema_test_spec['composedtype'].get("invalid", False):
ok(0, "BAD COMPOSED TYPE: schemata %s" % schema_name)
- continue
if schema_test_spec['composedtype'].get("prefix", False):
rx.add_prefix(schema_test_spec['composedtype']['prefix'][0],
@@ -99,7 +98,7 @@ def normalize(entries, test_data):
ok(0, "BAD SCHEMA: schemata %s" % schema_name)
continue
- if not schema: raise Exception("got no schema obj for valid input")
+ if not schema: raise StandardError("got no schema obj for valid input")
for pf in [ 'pass', 'fail' ]:
for source in schema_test_spec.get(pf, []):
View
43 ruby/Rx.rb
@@ -3,7 +3,7 @@ class Rx
def self.schema(schema)
Rx.new(:load_core => true).make_schema(schema)
end
-
+
def initialize(opt={})
@type_registry = {}
@prefix = {
@@ -16,7 +16,6 @@ def initialize(opt={})
end
end
-
def register_type(type)
uri = type.uri
@@ -90,10 +89,10 @@ def make_schema(schema)
return type_class.new(schema, self)
end
end
-
+
class Helper; end;
class Helper::Range
-
+
def initialize(arg)
@range = { }
@@ -125,11 +124,11 @@ def initialize(message, path)
@message = message
@path = path
end
-
+
def path
@path ||= ""
end
-
+
def message
"#{@message} (#{@path})"
end
@@ -142,7 +141,7 @@ def to_s
inspect
end
end
-
+
class Type
def initialize(param, rx)
assert_valid_params(param)
@@ -205,7 +204,7 @@ def initialize(param, rx)
class << self; def subname; return 'all'; end; end
def check!(value)
- @alts.each do |alt|
+ @alts.each do |alt|
begin
alt.check!(value)
rescue ValidationError => e
@@ -247,7 +246,7 @@ def check!(value)
rescue ValidationError
end
end
-
+
raise ValidationError.new("expected one to match", "/any")
end
end
@@ -272,7 +271,7 @@ def initialize(param, rx)
end
end
- def check!(value)
+ def check!(value)
unless value.instance_of?(Array)
raise ValidationError.new("expected array got #{value.class}", "/arr")
end
@@ -284,7 +283,7 @@ def check!(value)
end
if @contents_schema then
- value.each do |v|
+ value.each do |v|
begin
@contents_schema.check!(v)
rescue ValidationError => e
@@ -306,7 +305,7 @@ class << self; def subname; return 'bool'; end; end
def check!(value)
unless value.instance_of?(TrueClass) or value.instance_of?(FalseClass)
raise ValidationError.new("expected bool got #{value.inspect}", "/bool")
- end
+ end
true
end
end
@@ -317,14 +316,14 @@ class << self; def subname; return 'fail'; end; end
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")
@@ -360,7 +359,7 @@ def check!(value)
end
if @value_schema
- value.each_value do |v|
+ value.each_value do |v|
begin
@value_schema.check!(v)
rescue ValidationError => e
@@ -586,6 +585,10 @@ 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}")
@@ -603,11 +606,13 @@ 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 of length #{@length_range}, got #{value.length}", "/str")
+ 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
@@ -619,9 +624,9 @@ def check!(value)
# 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")
View
21 ruby/rx-test.rb
@@ -50,9 +50,17 @@ def normalize(entries, test_data)
class TAP_Emitter
attr_reader :i
+ attr_reader :failures
+
+ def initialize()
+ @failures = 0
+ end
def ok(bool, desc)
@i === nil ? @i = 1 : @i += 1
+ if ! bool then
+ @failures += 1
+ end
printf("%s %s - %s\n", bool ? 'ok' : 'not ok', @i, desc);
end
end
@@ -138,12 +146,12 @@ def ok(bool, desc)
}
entries.each_pair { |entry, want|
- result = begin
- schema.check!(test_data[source][entry])
- true
- rescue Rx::ValidationError => e
- false
- end
+ result = begin
+ schema.check!(test_data[source][entry])
+ true
+ rescue Rx::ValidationError => e
+ false
+ end
ok = (pf == 'pass' and result) || (pf == 'fail' and !result)
desc = sprintf "%s: %s-%s against %s",
@@ -156,3 +164,4 @@ def ok(bool, desc)
}
puts "1..#{tap.i}"
+exit(tap.failures > 0 ? 1 : 0)
View
10 sf-demo.pl
@@ -1,6 +1,7 @@
#!perl
use strict;
use warnings;
+use 5.10.1;
use lib 'perl/lib';
use Data::Rx;
@@ -38,15 +39,12 @@
undef,
{
foo => 1,
- bar => 2,
+ bar => 2.2,
baz => [ 3, 4, 5, 6.2, 7 ],
},
];
-eval { $schema->validate($input); };
+eval { $schema->assert_valid($input); };
my $fail = $@;
+say $fail;
-print Dump($fail->struct);
-
-print "PATH TO VALUE: " . join(q{ }, $fail->path_to_value) . "\n";
-print "PATH TO CHECK: " . join(q{ }, $fail->path_to_check) . "\n";

0 comments on commit 05c03cb

Please sign in to comment.
Something went wrong with that request. Please try again.