This repository has been archived by the owner on Feb 9, 2022. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add schema for matching regular expressions
Change-Id: Ib0e977fbb74ce468c923eba8d9f6a75d32175c6a
- Loading branch information
mpage
committed
May 3, 2012
1 parent
ee8ef23
commit f0f924c
Showing
6 changed files
with
63 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters