Skip to content
Browse files

Add schema for matching regular expressions

Change-Id: Ib0e977fbb74ce468c923eba8d9f6a75d32175c6a
  • Loading branch information...
1 parent ee8ef23 commit f0f924c563b38117b522a07c53be7ac50412bfc5 mpage committed
View
2 README.md
@@ -28,6 +28,7 @@ by default. The provided schemas are:
against a supplied schema.
* _Record_ - Accepts hashes with known keys. Each key has a supplied schema,
against which its value must validate.
+* _Regexp_ - Accepts strings that match a supplied regular expression.
* _Value_ - Accepts values using ```==```.
## Usage
@@ -64,6 +65,7 @@ be self-explanatory.
optional("_") => any,
"one_or_two" => enum(1, 2),
"strs_to_ints" => dict(String, Integer),
+ "foo_prefix" => /^foo/,
}
end
View
1 lib/membrane/schema.rb
@@ -6,6 +6,7 @@
require "membrane/schema/enum"
require "membrane/schema/list"
require "membrane/schema/record"
+require "membrane/schema/regexp"
require "membrane/schema/value"
module Membrane
View
29 lib/membrane/schema/regexp.rb
@@ -0,0 +1,29 @@
+require "membrane/errors"
+require "membrane/schema/base"
+
+module Membrane
+ module Schema
+ end
+end
+
+class Membrane::Schema::Regexp < Membrane::Schema::Base
+ attr_reader :regexp
+
+ def initialize(regexp)
+ @regexp = regexp
+ end
+
+ def validate(object)
+ if !object.kind_of?(String)
+ emsg = "Expected instance of String, given instance of #{object.class}"
+ raise Membrane::SchemaValidationError.new(emsg)
+ end
+
+ if !regexp.match(object)
+ emsg = "Value #{object} doesn't match regexp #{@regexp}"
+ raise Membrane::SchemaValidationError.new(emsg)
+ end
+
+ nil
+ end
+end
View
3 lib/membrane/schema_parser.rb
@@ -50,13 +50,14 @@ def do_parse(object)
parse_list(object)
when Class
Membrane::Schema::Class.new(object)
+ when Regexp
+ Membrane::Schema::Regexp.new(object)
when Dsl::DictionaryMarker
Membrane::Schema::Dictionary.new(do_parse(object.key_schema),
do_parse(object.value_schema))
when Dsl::EnumMarker
elem_schemas = object.elem_schemas.map { |s| do_parse(s) }
Membrane::Schema::Enum.new(*elem_schemas)
-
when Membrane::Schema::Base
object
else
View
19 spec/regexp_schema_spec.rb
@@ -0,0 +1,19 @@
+require "spec_helper"
+
+describe Membrane::Schema::Regexp do
+ let(:schema) { Membrane::Schema::Regexp.new(/bar/) }
+
+ describe "#validate" do
+ it "should raise an error if the validated object isn't a string" do
+ expect_validation_failure(schema, 5, /instance of String/)
+ end
+
+ it "should raise an error if the validated object doesn't match" do
+ expect_validation_failure(schema, "invalid", /match regex/)
+ end
+
+ it "should return nil if the validated object matches" do
+ schema.validate("barbar").should be_nil
+ end
+ end
+end
View
10 spec/schema_parser_spec.rb
@@ -55,6 +55,16 @@
schema.klass.should == String
end
+ it "should translate regexps into Membrane::Schema::Regexp" do
+ regexp = /foo/
+
+ schema = parser.parse { regexp }
+
+ schema.class.should == Membrane::Schema::Regexp
+
+ schema.regexp.should == regexp
+ end
+
it "should fall back to Membrane::Schema::Value" do
schema = parser.parse { 5 }

0 comments on commit f0f924c

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