Skip to content
This repository has been archived by the owner on Feb 9, 2022. It is now read-only.

Commit

Permalink
Add schema for matching regular expressions
Browse files Browse the repository at this point in the history
Change-Id: Ib0e977fbb74ce468c923eba8d9f6a75d32175c6a
  • Loading branch information
mpage committed May 3, 2012
1 parent ee8ef23 commit f0f924c
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 1 deletion.
2 changes: 2 additions & 0 deletions README.md
Expand Up @@ -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
Expand Down Expand Up @@ -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

Expand Down
1 change: 1 addition & 0 deletions lib/membrane/schema.rb
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions 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
3 changes: 2 additions & 1 deletion lib/membrane/schema_parser.rb
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions 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
10 changes: 10 additions & 0 deletions spec/schema_parser_spec.rb
Expand Up @@ -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 }

Expand Down

0 comments on commit f0f924c

Please sign in to comment.