Last active
November 13, 2018 09:33
-
-
Save ljuti/67e77797d5127b90d5fea56a2220c2be to your computer and use it in GitHub Desktop.
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
require "CrSerializer" | |
register_assertion CrSerializer::Assertions::Matches, [ :matcher ] | |
register_assertion CrSerializer::Assertions::ValidURI, [] of Symbol | |
register_assertion CrSerializer::Assertions::ExpectValid, [] of Symbol | |
register_assertion CrSerializer::Assertions::ValidEmail, [] of Symbol | |
register_assertion CrSerializer::Assertions::IFTTT, [ :property, :ruleset ] of Symbol | |
module CrSerializer::Assertions | |
class MatchesAssertion(ActualValueType) < CrSerializer::Assertions::Assertion | |
def initialize( | |
field : String, | |
message : String?, | |
@actual : ActualValueType, | |
@matcher : Regex | |
) | |
super field, message | |
end | |
def valid? : Bool | |
act : ActualValueType = @actual | |
return true if act.nil? | |
!(@actual =~ @matcher).nil? | |
end | |
end | |
class ValidURIAssertion(ActualValueType) < CrSerializer::Assertions::Assertion | |
def initialize( | |
field : String, | |
message : String?, | |
@actual : ActualValueType | |
) | |
super field, message | |
end | |
def valid? : Bool | |
act : ActualValueType = @actual | |
return true if act.nil? | |
!act.host.nil? | |
end | |
end | |
class ExpectValidAssertion(ActualValueType) < CrSerializer::Assertions::Assertion | |
def initialize( | |
field : String, | |
message : String?, | |
@actual : ActualValueType | |
) | |
super field, message | |
end | |
def valid? : Bool | |
act : ActualValueType = @actual | |
return true if act.nil? | |
act.validator.valid? | |
end | |
end | |
class ValidEmailAssertion(ActualValueType) < CrSerializer::Assertions::Assertion | |
EMAIL_REGEX = /\A([\w+\-].?)+@[a-z\d\-]+(\.[a-z]+)*\.[a-z]+\z/i | |
def initialize( | |
field : String, | |
message : String?, | |
@actual : ActualValueType, | |
@match : Bool = true | |
) | |
super field, message | |
end | |
def valid? | |
@match = true if @match.nil? | |
act : String? = @actual | |
return true if act.nil? | |
matched : Int32? = (act =~ EMAIL_REGEX) | |
@match ? !matched.nil? : matched.nil? | |
end | |
end | |
# Example: | |
# | |
# @[CrSerializer::Assertions::IFTTT(property: @name, ruleset: { "john" => ["doe", "deere"], "jill" => ["jones"]}) | |
class IFTTTAssertion(ActualValueType) < CrSerializer::Assertions::Assertion | |
def initialize( | |
field : String, | |
message : String?, | |
@actual : ActualValueType, | |
@property : String?, | |
@ruleset : Hash(String, Array(String)) | |
) | |
super field, message | |
end | |
def valid? | |
valid_values : Array(String) = @ruleset[@property] | |
act : String? = @actual | |
return true if act.nil? | |
valid_values.includes?(act) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment