Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #131 from RST-J/extract_formats
Extract formats into separate classes
- Loading branch information
Showing
17 changed files
with
241 additions
and
162 deletions.
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,39 @@ | ||
require 'json-schema/attribute' | ||
require 'uri' | ||
module JSON | ||
class Schema | ||
class DateTimeFormat < FormatAttribute | ||
def self.validate(current_schema, data, fragments, processor, validator, options = {}) | ||
case current_schema.schema['format'] | ||
|
||
# Timestamp in restricted ISO-8601 YYYY-MM-DDThh:mm:ssZ with optional decimal fraction of the second | ||
when 'date-time' | ||
if data.is_a?(String) | ||
error_message = "The property '#{build_fragment(fragments)}' must be a date/time in the ISO-8601 format of YYYY-MM-DDThh:mm:ssZ or YYYY-MM-DDThh:mm:ss.ssZ" | ||
r = Regexp.new('^\d\d\d\d-\d\d-\d\dT(\d\d):(\d\d):(\d\d)([\.,]\d+)?(Z|[+-](\d\d)(:?\d\d)?)?$') | ||
if (m = r.match(data)) | ||
parts = data.split("T") | ||
begin | ||
Date.parse(parts[0]) | ||
rescue Exception | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
begin | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[1].to_i > 23 | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[2].to_i > 59 | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[3].to_i > 59 | ||
rescue Exception | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
else | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
require 'json-schema/attribute' | ||
require 'uri' | ||
module JSON | ||
class Schema | ||
class DateFormat < FormatAttribute | ||
def self.validate(current_schema, data, fragments, processor, validator, options = {}) | ||
if data.is_a?(String) | ||
error_message = "The property '#{build_fragment(fragments)}' must be a date in the format of YYYY-MM-DD" | ||
r = Regexp.new('^\d\d\d\d-\d\d-\d\d$') | ||
if (r.match(data)) | ||
begin | ||
Date.parse(data) | ||
rescue Exception | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
else | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'json-schema/attribute' | ||
require 'uri' | ||
module JSON | ||
class Schema | ||
class IP4Format < FormatAttribute | ||
def self.validate(current_schema, data, fragments, processor, validator, options = {}) | ||
if data.is_a?(String) | ||
error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv4 address" | ||
r = Regexp.new('^(\d+){1,3}\.(\d+){1,3}\.(\d+){1,3}\.(\d+){1,3}$') | ||
if (m = r.match(data)) | ||
1.upto(4) do |x| | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[x].to_i > 255 | ||
end | ||
else | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
require 'json-schema/attribute' | ||
require 'uri' | ||
module JSON | ||
class Schema | ||
class IP6Format < FormatAttribute | ||
def self.validate(current_schema, data, fragments, processor, validator, options = {}) | ||
if data.is_a?(String) | ||
error_message = "The property '#{build_fragment(fragments)}' must be a valid IPv6 address" | ||
r = Regexp.new('^[a-f0-9:]+$') | ||
if (r.match(data)) | ||
# All characters are valid, now validate structure | ||
parts = data.split(":") | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if parts.length > 8 | ||
condensed_zeros = false | ||
parts.each do |part| | ||
if part.length == 0 | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if condensed_zeros | ||
condensed_zeros = true | ||
end | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if part.length > 4 | ||
end | ||
else | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
require 'json-schema/attribute' | ||
require 'uri' | ||
module JSON | ||
class Schema | ||
class TimeFormat < FormatAttribute | ||
def self.validate(current_schema, data, fragments, processor, validator, options = {}) | ||
if data.is_a?(String) | ||
error_message = "The property '#{build_fragment(fragments)}' must be a time in the format of hh:mm:ss" | ||
r = Regexp.new('^(\d\d):(\d\d):(\d\d)$') | ||
if (m = r.match(data)) | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[1].to_i > 23 | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[2].to_i > 59 | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) and return if m[3].to_i > 59 | ||
else | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
return | ||
end | ||
end | ||
end | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
require 'json-schema/attribute' | ||
require 'uri' | ||
module JSON | ||
class Schema | ||
class UriFormat < FormatAttribute | ||
def self.validate(current_schema, data, fragments, processor, validator, options = {}) | ||
if data.is_a?(String) | ||
error_message = "The property '#{build_fragment(fragments)}' must be a valid URI" | ||
begin | ||
URI.parse(URI.escape(data)) | ||
rescue URI::InvalidURIError | ||
validation_error(processor, error_message, fragments, current_schema, self, options[:record_errors]) | ||
end | ||
end | ||
end | ||
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
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
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
Oops, something went wrong.