Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace uri_template gem with regular expression #128

Merged
merged 1 commit into from
May 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ PATH
hana (~> 1.3)
regexp_parser (~> 2.0)
simpleidn (~> 0.2)
uri_template (~> 0.7)

GEM
remote: https://rubygems.org/
Expand All @@ -27,7 +26,6 @@ GEM
unf_ext
unf (0.1.4-java)
unf_ext (0.0.8.2)
uri_template (0.7.0)

PLATFORMS
java
Expand Down
1 change: 0 additions & 1 deletion json_schemer.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,4 @@ Gem::Specification.new do |spec|
spec.add_runtime_dependency "hana", "~> 1.3"
spec.add_runtime_dependency "regexp_parser", "~> 2.0"
spec.add_runtime_dependency "simpleidn", "~> 0.2"
spec.add_runtime_dependency "uri_template", "~> 0.7"
end
2 changes: 1 addition & 1 deletion lib/json_schemer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,10 +12,10 @@
require 'hana'
require 'regexp_parser'
require 'simpleidn'
require 'uri_template'

require 'json_schemer/version'
require 'json_schemer/format/hostname'
require 'json_schemer/format/uri_template'
require 'json_schemer/format'
require 'json_schemer/errors'
require 'json_schemer/cached_resolver'
Expand Down
8 changes: 1 addition & 7 deletions lib/json_schemer/format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
module JSONSchemer
module Format
include Hostname
include URITemplate

# this is no good
EMAIL_REGEX = /\A[^@\s]+@([\p{L}\d-]+\.)+[\p{L}\d\-]{2,}\z/i.freeze
Expand Down Expand Up @@ -115,13 +116,6 @@ def iri_escape(data)
end.force_encoding(Encoding::US_ASCII)
end

def valid_uri_template?(data)
URITemplate.new(data)
true
rescue URITemplate::Invalid
false
end

def valid_json_pointer?(data)
JSON_POINTER_REGEX.match?(data)
end
Expand Down
34 changes: 34 additions & 0 deletions lib/json_schemer/format/uri_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# frozen_string_literal: true
module JSONSchemer
module Format
module URITemplate
# https://datatracker.ietf.org/doc/html/rfc6570
PCT_ENCODED = '%\h{2}' # pct-encoded = "%" HEXDIG HEXDIG
EXPLODE = '\*' # explode = "*"
MAX_LENGTH = '[1-9]\d{0,3}' # max-length = %x31-39 0*3DIGIT ; positive integer < 10000
PREFIX = ":#{MAX_LENGTH}" # prefix = ":" max-length
MODIFIER_LEVEL4 = "#{PREFIX}|#{EXPLODE}" # modifier-level4 = prefix / explode
VARCHAR = "(\\w|#{PCT_ENCODED})" # varchar = ALPHA / DIGIT / "_" / pct-encoded
VARNAME = "#{VARCHAR}(\\.?#{VARCHAR})*" # varname = varchar *( ["."] varchar )
VARSPEC = "#{VARNAME}(#{MODIFIER_LEVEL4})?" # varspec = varname [ modifier-level4 ]
VARIABLE_LIST = "#{VARSPEC}(,#{VARSPEC})*" # variable-list = varspec *( "," varspec )
OPERATOR = '[+#./;?&=,!@|]' # operator = op-level2 / op-level3 / op-reserve
# op-level2 = "+" / "#"
# op-level3 = "." / "/" / ";" / "?" / "&"
# op-reserve = "=" / "," / "!" / "@" / "|"
EXPRESSION = "{#{OPERATOR}?#{VARIABLE_LIST}}" # expression = "{" [ operator ] variable-list "}"
LITERALS = "[^\\x00-\\x20\\x7F\"%'<>\\\\^`{|}]|#{PCT_ENCODED}" # literals = %x21 / %x23-24 / %x26 / %x28-3B / %x3D / %x3F-5B
# / %x5D / %x5F / %x61-7A / %x7E / ucschar / iprivate
# / pct-encoded
# ; any Unicode character except: CTL, SP,
# ; DQUOTE, "'", "%" (aside from pct-encoded),
# ; "<", ">", "\", "^", "`", "{", "|", "}"
URI_TEMPLATE = "(#{LITERALS}|#{EXPRESSION})*" # URI-Template = *( literals / expression )
URI_TEMPLATE_REGEX = /\A#{URI_TEMPLATE}\z/

def valid_uri_template?(data)
URI_TEMPLATE_REGEX.match?(data)
end
end
end
end
4 changes: 2 additions & 2 deletions lib/json_schemer/schema/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def merge(
end

JSON_POINTER_TOKEN_ESCAPE_CHARS = { '~' => '~0', '/' => '~1' }
JSON_POINTER_TOKEN_ESCAPE_REGEXP = Regexp.union(JSON_POINTER_TOKEN_ESCAPE_CHARS.keys)
JSON_POINTER_TOKEN_ESCAPE_REGEX = Regexp.union(JSON_POINTER_TOKEN_ESCAPE_CHARS.keys)

class << self
def draft_name
Expand Down Expand Up @@ -624,7 +624,7 @@ def safe_strict_decode64(data)
end

def escape_json_pointer_token(token)
token.gsub(JSON_POINTER_TOKEN_ESCAPE_REGEXP, JSON_POINTER_TOKEN_ESCAPE_CHARS)
token.gsub(JSON_POINTER_TOKEN_ESCAPE_REGEX, JSON_POINTER_TOKEN_ESCAPE_CHARS)
end

def join_uri(a, b)
Expand Down
247 changes: 247 additions & 0 deletions test/uri_template_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,247 @@
require 'test_helper'

class PrettyErrorsTest < Minitest::Test
def test_uri_template_format
schema = JSONSchemer.schema({ 'type' => 'string', 'format' => 'uri-template' })

[
'{justright:9999}',
'{#has.lots.of.dots}',
'{an_underscore}',
'{=reserved}',
'{,reserved}',
'{!reserved}',
'{@reserved}',
'{|reserved}',
'{percent%20encoded}',
'{UPCASED}',
'literal%20encoded',
"literal🖥️unicode"
].each do |data|
assert(schema.valid?(data), data)
end

[
'{toobig:10000}',
'{#.starts.with.dot}',
'{#ends.with.dot.}',
'{a-dash}',
'{doubleexplode**}',
'{percent%unencoded}',
'{not percent encoded}',
'{hangingprefix:}',
'{prefixandexplode:3*',
'{explodeandprefix*:3',
'{varchar🖥️unicode}',
'{{double curly}}',
'{hanging curlopen',
'hanging curlyclose}',
'literal space',
'literal"dquote',
"literal'squote",
'literal%percent',
'literal<lessthan',
'literal>greaterthan',
'literal\backslash',
'literal^caret',
'literal`backtick',
'literal{curlyopen',
'literal}curlyclose',
'literal|bar'
].each do |data|
refute(schema.valid?(data), data)
end

# examples from https://datatracker.ietf.org/doc/html/rfc6570
[
'http://example.com/~{username}/',
'http://example.com/dictionary/{term:1}/{term}',
'http://example.com/search{?q,lang}',
'http://www.example.com/foo{?query,number}',
'{var}',
'{hello}',
'{+var}',
'{+hello}',
'{+path}/here',
'here?ref={+path}',
'X{#var}',
'X{#hello}',
'map?{x,y}',
'{x,hello,y}',
'{+x,hello,y}',
'{#x,hello,y}',
'{#path,x}/here',
'X{.var}',
'X{.x,y}',
'{/var}',
'{/var,x}/here',
'{;x,y}',
'{;x,y,empty}',
'{?x,y}',
'{?x,y,empty}',
'?fixed=yes{&x}',
'{&x,y,empty}',
'{var:3}',
'{var:30}',
'{list}',
'{list*}',
'{keys}',
'{keys*}',
'{+path:6}/here',
'{+list}',
'{+list*}',
'{+keys}',
'{+keys*}',
'{#path:6}/here',
'{#list}',
'{#list*}',
'{#keys}',
'{#keys*}',
'X{.var:3}',
'X{.list}',
'X{.list*}',
'X{.keys}',
'X{.keys*}',
'{/var:1,var}',
'{/list}',
'{/list*}',
'{/list*,path:4}',
'{/keys}',
'{/keys*}',
'{;hello:5}',
'{;list}',
'{;list*}',
'{;keys}',
'{;keys*}',
'{?var:3}',
'{?list}',
'{?list*}',
'{?keys}',
'{?keys*}',
'{&var:3}',
'{&list}',
'{&list*}',
'{&keys}',
'{&keys*}',
'find{?year*}',
'www{.dom*}',
'{count}',
'{count*}',
'{/count}',
'{/count*}',
'{;count}',
'{;count*}',
'{?count}',
'{?count*}',
'{&count*}',
'{var}',
'{hello}',
'{half}',
'O{empty}X',
'O{undef}X',
'{x,y}',
'{x,hello,y}',
'?{x,empty}',
'?{x,undef}',
'?{undef,y}',
'{var:3}',
'{var:30}',
'{list}',
'{list*}',
'{keys}',
'{keys*}',
'{+var}',
'{+hello}',
'{+half}',
'{base}index',
'{+base}index',
'O{+empty}X',
'O{+undef}X',
'{+path}/here',
'here?ref={+path}',
'up{+path}{var}/here',
'{+x,hello,y}',
'{+path,x}/here',
'{+path:6}/here',
'{+list}',
'{+list*}',
'{+keys}',
'{+keys*}',
'{#var}',
'{#hello}',
'{#half}',
'foo{#empty}',
'foo{#undef}',
'{#x,hello,y}',
'{#path,x}/here',
'{#path:6}/here',
'{#list}',
'{#list*}',
'{#keys}',
'{#keys*}',
'{.who}',
'{.who,who}',
'{.half,who}',
'www{.dom*}',
'X{.var}',
'X{.empty}',
'X{.undef}',
'X{.var:3}',
'X{.list}',
'X{.list*}',
'X{.keys}',
'X{.keys*}',
'X{.empty_keys}',
'X{.empty_keys*}',
'{/who}',
'{/who,who}',
'{/half,who}',
'{/who,dub}',
'{/var}',
'{/var,empty}',
'{/var,undef}',
'{/var,x}/here',
'{/var:1,var}',
'{/list}',
'{/list*}',
'{/list*,path:4}',
'{/keys}',
'{/keys*}',
'{;who}',
'{;half}',
'{;empty}',
'{;v,empty,who}',
'{;v,bar,who}',
'{;x,y}',
'{;x,y,empty}',
'{;x,y,undef}',
'{;hello:5}',
'{;list}',
'{;list*}',
'{;keys}',
'{;keys*}',
'{?who}',
'{?half}',
'{?x,y}',
'{?x,y,empty}',
'{?x,y,undef}',
'{?var:3}',
'{?list}',
'{?list*}',
'{?keys}',
'{?keys*}',
'{&who}',
'{&half}',
'?fixed=yes{&x}',
'{&x,y,empty}',
'{&x,y,undef}',
'{&var:3}',
'{&list}',
'{&list*}',
'{&keys}',
'{&keys*}'
].each do |data|
assert(schema.valid?(data), data)
end
end
end